Joeish W | 12 Jul 10:51 2014
Picon

How do I make the :actual-type of a define-foreign-type a (:pointer (:struct ...))

 I have a define-foreign-type, translate-to-foreign, translate-from-foreign and a defclass at bottom of this page

  These four sections of code make up my types. At the top of the code is a defcstruct that, I am trying to make a part of my types  by changing the :actual-type in the define-foreign-type to:  

   (:actual-type '(:pointer (:struct rect1))).

The :actual-type is normally :pointer and that makes it so the defcfun at the bottom of code section compiles no problem.  When I compile the code here I get an error in my defcun saying that "(:pointer (:struct rect1)) is not a cffi type".  Why is it doing this and how can I make the actual type of my define-foreign-type a (:pointer (:struct rect1))?  The reason I would like to do this is so I can convert the C data to lisp data as soon as possible. If this won't work and you have any other suggestions on how to do this professionally. pls let me know. Thank you.


;;;;Code

(cffi:defcstruct rect1
(x :int)
(y :int)
(width :int)
(height :int))
 
 
(define-foreign-type rect ()
((garbage-collect :reader garbage-collect :initform nil :initarg
:garbage-collect))
(:actual-type '(:pointer (:struct rect1)))
(:simple-parser rect))
 
 
(defclass cv-rect ()
((c-pointer :reader c-pointer :initarg :c-pointer)))
 
 
(defmethod translate-to-foreign ((lisp-value cv-rect) (c-type rect))
(values (c-pointer lisp-value) lisp-value))
 
 
(defmethod translate-from-foreign (c-pointer (c-type rect))
(let ((rectangle (make-instance 'cv-rect :c-pointer c-pointer)))
(when (garbage-collect c-type)
(tg:finalize rectangle (lambda () (del-rect c-pointer))))
rectangle))
 
 
 
(defcfun ("cv_create_Rect4" rect-4) rect
"RECT constructor."
(x :int)
(y :int)
(width :int)
(height :int))

_______________________________________________
Cffi-devel mailing list
Cffi-devel <at> common-lisp.net
http://common-lisp.net/cgi-bin/mailman/listinfo/cffi-devel
Robert P. Goldman | 16 Jun 22:09 2014

Quick documentation note

On line 610 of cffi-manual.texinfo, there's an impnote that states that
CFFI-grovel is not part of CFFI.  But later, we learn that it is.  Maybe
replace this implementation note with a cross-ref to the CFFI-grovel
chapter?

Best,
r
Joeish W | 27 May 22:29 2014
Picon

How do I wrap a C/C++ struct with a nested union

I'm trying to wrap this struct, CvDTreeSplit. Normally I just use swig to wrap them but in this case when I wrap the C++ struct, CvDTreeSplit, below, I get this error in swig:

 Warning 312: Nested union not currently supported (ignored).

So the output ends up being the defcstruct at the bottom of the page.  Not sure where to go from here. Can someone wrap this struct for me so I'll know how to wrap structs, with nested unions, containing structs and C arrays, in the future.


struct CvDTreeSplit
{
    int var_idx;
    int condensed_idx;
    int inversed;
    float quality;
    CvDTreeSplit* next;
    union
    {
        int subset[2];
        struct
        {
            float c;
            int split_point;
        }
        ord;
    };
};

(cffi:defcstruct CvDTreeSplit
    (var_idx :int)
    (condensed_idx :int)
    (inversed :int)
    (quality :float)
    (next :pointer))

_______________________________________________
Cffi-devel mailing list
Cffi-devel <at> common-lisp.net
http://common-lisp.net/cgi-bin/mailman/listinfo/cffi-devel
Anton Vodonosov | 27 May 02:56 2014
Picon

Could you make a release please?

Hi, could you make a release please?

I am very interested in commit bb629a853a to be in Quicklisp.

Quicklisp currently grabs cffi from
http http://common-lisp.net/project/cffi/releases/cffi_latest.tar.gz

Best regards,
- Anton
Joeish W | 16 May 23:33 2014
Picon

Is there anything in CFFI where I can cast an integer to a char and the result is a letter..

This is the code I'm referring to :

        char c = (char)waitKey();

        if( c == 27 )
            break;

        if( c == 'r' )
        {.....

The function waitKey, above, outputs an integer as return and it's being cast to a char to make the result a letter. Can this be done in CFFI. I have a CFFI wrapper for waitKey btw, that has an :int return
 
_______________________________________________
Cffi-devel mailing list
Cffi-devel <at> common-lisp.net
http://common-lisp.net/cgi-bin/mailman/listinfo/cffi-devel
Joeish W | 8 May 23:24 2014
Picon

What is wrong with this 2d array of pointers

I was hoping someone could look at this and help me debug it.

I'm getting:

(FOREIGN-ALLOC :POINTER :INITIAL-ELEMENT (NULL-POINTER))
is not of type
SB-SYS:SYSTEM-AREA-POINTER. when I run it.



(foreign-array-alloc #2A((

(foreign-alloc :pointer :initial-element (null-pointer))
(foreign-alloc :pointer :initial-element (null-pointer)))
((foreig n-alloc :pointer :initial-element (null-pointer))
(foreign-alloc :pointer :initial-element (null-pointer)))
((foreign-alloc :pointer :initial-element (null-pointer))
(foreign-alloc :pointer :initial-element (null-pointer)))
((foreign-alloc :pointer :initial-element (null-pointer))
(foreign-alloc :pointer :initial-element (null-pointer)))) '(:array :pointer 4 2))

 
_______________________________________________
Cffi-devel mailing list
Cffi-devel <at> common-lisp.net
http://common-lisp.net/cgi-bin/mailman/listinfo/cffi-devel
Joeish W | 7 May 10:04 2014
Picon

I can use a little professional advice on my new memory management technique(read this for a fun idea)

Probably about 30% of my functions need some sort of memory management. I wanted to use the TG(Trivial Garbage) finalizers for automatic MM but they were twice as slow as my original functions. So I decided to use 3 forms of MM in my library WITH-* macros, manual and finalizers. As for the finalizer part of my library, I wanted to call all my functions by the same name as well so I tried 2 things. First I wrapped everything in conditionals so I could call by the same name but that was a bit slow...so I decided to create a clone of my library named "GC" that had all the same functions but they were finalized. so If, while in my original package, where I normally use with-* macros because they are quickest, I needed one, I could append a "gc::" before the function I wanted to use and bam!, a finalized version of the function I wanted to use, with the same name(save for the gc::) and as fast as possible. The only caveat is If I use one finalizer I have to append a "gc::" to thee front of every function that takes that function as input. Once the branching for that function is over, I can do whatever I want with the rest of the functions in my code..Now I can tell you this is awesome and fun to use....but is there another better way

_______________________________________________
Cffi-devel mailing list
Cffi-devel <at> common-lisp.net
http://common-lisp.net/cgi-bin/mailman/listinfo/cffi-devel
Joeish W | 3 May 20:08 2014
Picon

(no subject)


have thes 3 defcfuns, for OpenCV functions (C++) which have been wrapped in C(C and C+++ function
definitions have been included), the top 2,%brisk and surf0 can both can be used as the (self
feature-detector) param in the bottom one, %feat-detect-detect. If the output of the top two and the self
param of feat-detect-detect are just :pointer, But I need to have all three of their types defined
similiarly to the (four type functions) on the bottom of the page so I can use finalizers. 

All of the finalizers and types for the three are the same, just substitute in brisk and surf for
feature-detector in the four functions at the bottom of the page and you will know what each looks like. 

 Should I just call the types for the output of brisk and surf and the input type of feat-detect-detect by
the same name, and make them all use the same 4 function 
type/finalizer combo at the bottom of the page, or is there a better way.

    ;; BRISK::BRISK(int thresh=30, int octaves=3, float patternScale=1.0f)
    ;; BRISK* cv_create_BRISK(int thresh, int octaves, float patternScale)
    (defcfun ("cv_create_BRISK" %brisk) brisk
      (thresh :int)
      (octaves :int)
      (pattern-scale :float))

    ;; SURF::SURF()
    ;; SURF* cv_create_SURF() 
    (defcfun ("cv_create_SURF" surf0) surf)

    ;; void FeatureDetector::detect(const Mat& image, vector<KeyPoint>& keypoints, const Mat&
mask=Mat() ) const
    ;; void cv_FeatureDetector_detect3(FeatureDetector* self, Mat* image, vector_KeyPoint*
keypoints, Mat* mask)
    (defcfun ("cv_FeatureDetector_detect3" %feat-detector-detect) :void
      (self feature-detector)
      (image mat)
      (keypoints (:pointer vector-keypoint))
      (mask mat))

    (define-foreign-type feature-detector ()
      ((garbage-collect  :reader garbage-collect :initform nil :initarg 
                         :garbage-collect))
      (:actual-type :pointer)
      (:simple-parser feature-detector))

    (defclass cv-feature-detector ()
      ((c-pointer :reader c-pointer :initarg :c-pointer)))

    (defmethod translate-to-foreign ((lisp-value cv-feature-detector) (c-type feature-detector))
      (c-pointer lisp-value))

    (defmethod translate-from-foreign (c-pointer (c-type feature-detector))
      (let ((feature-detector (make-instance 'cv-feature-detector :c-pointer c-pointer)))
        (when (garbage-collect c-type)
          (tg:finalize feature-detector (lambda () (del-feature-detector c-pointer))))
        feature-detector))
    ound matches of keypoints from two images."
       (%draw-matches img1 keypoints1 img2 keypoints2 matches1to2 outimg match-color
single-point-color matches-mask flags))
Joeish W | 3 May 11:45 2014
Picon

Still can't mem-aref the output of a metaobject with provided code

I do appreciate all your attention to my types GC issue so far but when Wim gave me this untested code:

(defmethod translate-to-foreign ((lisp-value cv-matrix) (c-type cv-mat))
     (values  (c-pointer lisp-value) lisp-value))

I thought it worked but I still had my mem-aref macro defined to access the c-pointer value so I didn't get a good test?

for example I'm referring to the lisp wrapper for this C function now, but it is identical to the cv-matrix one:

Scalar* cv_create_Scalar(double val0, double val1, double val2, double val3)
{
    return new Scalar(val0, val1, val2, val3);
}


I now have my lisp wrapper and types/finalizers for the above c function defined like this:

(defcfun ("cv_create_Scalar" %scalar) scalar
  (val0 :double)
  (val1 :double)
  (val2 :double)
  (val3 :double))

(define-foreign-type scalar ()
  ((garbage-collect  :reader garbage-collect :initform nil :initarg
                     :garbage-collect))
  (:actual-type :pointer)
  (:simple-parser scalar))


(defclass cv-scalar ()
  ((c-pointer :reader c-pointer :initarg :c-pointer)))


(defmethod translate-to-foreign ((lisp-value cv-scalar) (c-type scalar))
   (values  (c-pointer lisp-value) lisp-value))  <--the new version Wim gave me untested


(defmethod translate-from-foreign (c-pointer (c-type scalar))
  (let ((scalar (make-instance 'cv-scalar :c-pointer c-pointer)))
    (when (garbage-collect c-type)
      (tg:finalize scalar (lambda () (del-scalar c-pointer))))
    scalar))


When I run (defparameter a (%scalar 1d0 2d0 3d0 4d0))  and (mem-aref a :double)  I still get:

The value #<CV-SCALAR {10046BC833}>
is not of type
  SB-SYS:SYSTEM-AREA-POINTER.


but when I run  (mem-aref (c-pointer a) :double)  it works.



What do I have to change in my types so I can mem-aref  "A" normally, like this
(mem-aref a :double)...Any help is much appreciated:)
 
_______________________________________________
Cffi-devel mailing list
Cffi-devel <at> common-lisp.net
http://common-lisp.net/cgi-bin/mailman/listinfo/cffi-devel
Joeish W | 29 Apr 13:34 2014
Picon

How do I define a CFFI foreign-type so it can be used in a function that accepts a different foreign type as a parameter

I have thes 3 defcfuns, for OpenCV functions (C++) which have been wrapped in C(C and C+++ function definitions have been included), the top 2, %brisk and surf0 can both can be used as the (self feature-detector) param in the bottom one, %feat-detect-detect If the output of the top two and the self param of feat-detect-detect are just :pointer, But I need to have all three of their types defined similiarly to the ones on the bottom of the page so I can use finalizers. All of the finalizers and types for the three are the same, just substitute in brisk and surf for feature-detector in the four functions at the bottom of the page and you will know what each looks like. Should I just call the types for the output of brisk and surf and the input type offeat-detect-detect by the same name, and make them all use the same 4 function type/finalizer combo at the bottom of the page, or is there a better way.
;; BRISK::BRISK(int thresh=30, int octaves=3, float patternScale=1.0f) ;; BRISK* cv_create_BRISK(int thresh, int octaves, float patternScale) (defcfun ("cv_create_BRISK" %brisk) brisk (thresh :int) (octaves :int) (pattern-scale :float)) ;; SURF::SURF() ;; SURF* cv_create_SURF() (defcfun ("cv_create_SURF" surf0) surf) ;; void FeatureDetector::detect(const Mat& image, vector<KeyPoint>& keypoints, const Mat& mask=Mat() ) const ;; void cv_FeatureDetector_detect3(FeatureDetector* self, Mat* image, vector_KeyPoint* keypoints, Mat* mask) (defcfun ("cv_FeatureDetector_detect3" %feat-detector-detect) :void (self feature-detector) (image mat) (keypoints (:pointer vector-keypoint)) (mask mat)) (define-foreign-type feature-detector () ((garbage-collect :reader garbage-collect :initform nil :initarg :garbage-collect)) (:actual-type :pointer) (:simple-parser feature-detector)) (defclass cv-feature-detector () ((c-pointer :reader c-pointer :initarg :c-pointer))) (defmethod translate-to-foreign ((lisp-value cv-feature-detector) (c-type feature-detector)) (c-pointer lisp-value)) (defmethod translate-from-foreign (c-pointer (c-type feature-detector)) (let ((feature-detector (make-instance 'cv-feature-detector :c-pointer c-pointer))) (when (garbage-collect c-type) (tg:finalize feature-detector (lambda () (del-feature-detector c-pointer)))) feature-detector)) ound matches of keypoints from two images." (%draw-matches img1 keypoints1 img2 keypoints2 matches1to2 outimg match-color single-point-color matches-mask flags))

_______________________________________________
Cffi-devel mailing list
Cffi-devel <at> common-lisp.net
http://common-lisp.net/cgi-bin/mailman/listinfo/cffi-devel
Joeish W | 29 Apr 13:26 2014
Picon

Lists of METAOBJECTS and FOREIGN-ALLOC


I have this conditional from a cond statement I need to satisfy


    ((listp (first args))
     (c-arr-to-vector-point (first args)))


The (first args)  will be this but it should be able to have  an infinite number
of (point i j) in it.

(foreign-alloc :pointer :initial-contents  (LIST (POINT 1 2) (POINT 3 4)))


The thing is that the output of (point i j) is a metaobject e.g. #<CV-POINT {10038888E3}> . That is how I have my return for point defined. When I need to use foreign-alloc to define an array of point I have to  do it like this...


(foreign-alloc :pointer :initial-contents  (LIST (c-pointer (POINT 1 2)) (c-pointer (POINT 3 4))))

...because my defclass is this:

(defclass std-vector-point ()
  ((c-pointer :reader c-pointer :initarg :c-pointer)))

Here is my function I need to change so a when it is run I can just enter (LIST (POINT 1 2) (POINT 3 4)) for the (first args) in the above mentioned conditional block and have the %c-arr-to-vector-point function be satisfied, because it accepts a list of pointers as its input.  But I need help because I don't want the user to see the c-pointer at all let alone have to wrap each (point i j) like this (c-pointer (point i j)) that is too much typing.  How would I do this without it making my function any slower.

(defun vector-point (&rest args)
  (cond
    ((fourth args)
     (error "odd number of args to VECTOR-POINT"))
    ((null (first args))
     (%vector-point))
    ((listp (first args))
     (%c-arr-to-vector-point (foreign-alloc :pointer :initial-contents (first args))))
    ((eq :size (second args))
     (%vector-point-size (first args)))
    ((and (eq (type-of (first args)) 'std-vector-point) (second args) (not (third args)))
     (mem-aref (c-pointer
        (%vector-point-to-c-array (first args))) :pointer (second args)))
    ((and (eq (type-of (first args)) 'std-vector-point) (second args) (third args))
     (mem-aref (c-pointer
        (mem-aref (c-pointer
               (%vector-point-to-c-array (first args))) :pointer (second args)))
           :int (third args)))
    ((not (eq (type-of (first args)) 'std-vector-point))
     (error "The value ~a is not of type (STD-VECTOR-POINT)." (first args)))))



_______________________________________________
Cffi-devel mailing list
Cffi-devel <at> common-lisp.net
http://common-lisp.net/cgi-bin/mailman/listinfo/cffi-devel

Gmane