[cffi-devel] translate-type-from-foreign
Hi all, I've noticed that in the upgrade from cffi 0.9.1 to 0.9.2 that there were a few lines removed from early-types.lisp namely : -;; Default Translators for foreign-struct-type -(defmethod translate-type-from-foreign (value (type foreign-struct-type)) - (translate-from-foreign value (name type))) - -(defmethod translate-type-to-foreign (value (type foreign-struct-type)) - (translate-to-foreign value (name type))) I've been specialising translate-to-foreign to automatically convert c structures to their equivalent lisp counterparts but without these lines this obviously no longer works. Was this removal deliberate and if so what is the preferred way to accomplish this? Cheers, Sean. -- ...Please don't assume Lisp is only useful for Animation and Graphics, AI, Bioinformatics, B2B and E-Commerce, Data Mining, EDA/Semiconductor applications, Expert Systems, Finance, Intelligent Agents, Knowledge Management, Mechanical CAD, Modeling and Simulation, Natural Language, Optimization, Research, Risk Analysis, Scheduling, Telecom, and Web Authoring just because these are the only things they happened to list. Kent Pitman.
Sean <rosssd@gmail.com> writes:
I've been specialising translate-to-foreign to automatically convert c structures to their equivalent lisp counterparts but without these lines this obviously no longer works.
Was this removal deliberate and if so what is the preferred way to accomplish this?
Hmm. I tried to track this alleged removal, unsuccessfully. :-) Are you sure you didn't add that code yourself? I can't find this code in either 0.9.0 or 0.9.1. In any case, you bring up a good point, which I've mentioned before too: <http://article.gmane.org/gmane.lisp.cffi.devel/1033> Using the cffi-newtypes branch (which I hope to merge soon, since there haven't been any complaints, unless James has any objections), you could do something like this: (defcstruct %my-struct ...) (define-foreign-type my-struct-type () () (:actual-type %my-struct) (:simple-parser my-struct)) (defmethod translate-from-foreign (value (type my-struct-type)) ...) (defmethod translate-to-foreign (value (type my-struct-type)) ...) (defmethod free-translated-object (value (type my-struct-type) param) ...) Now, perhaps defcstruct could take an option to define a type class thus avoiding the need to define a wrapper type. Perhaps something like this? (defcstruct (my-struct :class my-struct-type) ...) Any comments? -- Luís Oliveira http://student.dei.uc.pt/~lmoliv/
On Thu, 2007-04-12 at 17:40 +0100, Luis Oliveira wrote:
Hmm. I tried to track this alleged removal, unsuccessfully. :-) Are you sure you didn't add that code yourself? I can't find this code in either 0.9.0 or 0.9.1.
<cough> <cough>, good catch. I swear one day I'll learn not to change source code residing in asdf-install/site/ .
In any case, you bring up a good point, which I've mentioned before too: <http://article.gmane.org/gmane.lisp.cffi.devel/1033>
Using the cffi-newtypes branch (which I hope to merge soon, since there haven't been any complaints, unless James has any objections), you could do something like this:
(defcstruct %my-struct ...)
(define-foreign-type my-struct-type () () (:actual-type %my-struct) (:simple-parser my-struct))
(defmethod translate-from-foreign (value (type my-struct-type)) ...) (defmethod translate-to-foreign (value (type my-struct-type)) ...) (defmethod free-translated-object (value (type my-struct-type) param) ...)
Now, perhaps defcstruct could take an option to define a type class thus avoiding the need to define a wrapper type. Perhaps something like this?
(defcstruct (my-struct :class my-struct-type) ...)
Any comments?
Nice, this is exactly the kind of thing that I'm currently looking for. What does the :simple-parser slot-option do? I'm just wondering does this provide any value other than the ability to define translations on c-structs? Not that this is the wrong way to do it but it does seem like a large amount of work to enable translations. Just my 2c though. Cheers, Sean. -- ...Please don't assume Lisp is only useful for Animation and Graphics, AI, Bioinformatics, B2B and E-Commerce, Data Mining, EDA/Semiconductor applications, Expert Systems, Finance, Intelligent Agents, Knowledge Management, Mechanical CAD, Modeling and Simulation, Natural Language, Optimization, Research, Risk Analysis, Scheduling, Telecom, and Web Authoring just because these are the only things they happened to list. Kent Pitman.
Sean <rosssd@gmail.com> writes:
What does the :simple-parser slot-option do?
The (:simple-parser foo) in (define-foreign-type foo-type ...) expands to: (define-parse-method foo () (make-instance 'foo-type))
I'm just wondering does this provide any value other than the ability to define translations on c-structs? Not that this is the wrong way to do it but it does seem like a large amount of work to enable translations.
I agree that it's unfortunate that you need to define an extra type to wrap the struct type and add translations, which is why I propose a :CLASS option to defcstruct that would do that for you. The big change in cffi-newtypes is that type translators work with objects of type CFFI::FOREIGN-TYPE instead of symbols naming types. To answer your question, I do think this provides a lot of value. Inheritance is more intituite (plain CLOS) and you handle type arguments, e.g. (:string :encoding foo), which you can save as slots in your type instance, much more easily. -- Luís Oliveira http://student.dei.uc.pt/~lmoliv/
participants (2)
-
Luis Oliveira
-
Sean