chiark / gitweb /
Added support for OR as return type
authorespen <espen>
Fri, 11 Apr 2008 18:51:57 +0000 (18:51 +0000)
committerespen <espen>
Fri, 11 Apr 2008 18:51:57 +0000 (18:51 +0000)
gffi/basic-types.lisp

index bbc1657ef9f6ac53cad84a99a3e7f8f49c20d630..5231aaf9bb0747b32fc4b00ec91c8946fa9c275c 100644 (file)
@@ -20,7 +20,7 @@
 ;; TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 ;; SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
-;; $Id: basic-types.lisp,v 1.11 2008-03-25 02:00:58 espen Exp $
+;; $Id: basic-types.lisp,v 1.12 2008-04-11 18:51:57 espen Exp $
 
 (in-package "GFFI")
 
@@ -93,6 +93,8 @@ (define-type-generic to-alien-function (type &optional copy-p)
   "Returns a function of one argument which will translate objects of the given type to alien repesentation. An optional function, taking the origional object and the alien representation as arguments, to clean up after the alien value is not needed any more may be returned as a second argument.")
 (define-type-generic from-alien-function (type &key ref)
   "Returns a function of one argument which will translate alien objects of the given type to lisp representation. REF should be :FREE, :COPY, :STATIC or :TEMP")
+(define-type-generic alien-typep-form (type alien)
+  "Returns a form evaluating to T if ALIEN is an alien representation of TYPE.")
 (define-type-generic callback-wrapper (type var arg form)
   "Creates a wrapper around FORM which binds the lisp translation of
 ARG to VAR during a C callback.")
@@ -959,6 +961,20 @@ (define-type-method to-alien-form ((type or) form &optional copy-p)
              `(,type ,(to-alien-form type 'value copy-p)))
          (rest (type-expand-to 'or type))))))
 
+(define-type-method from-alien-form ((type or) form &key ref)
+  (declare (ignore ref))
+  `(let ((alien ,form))
+     (cond
+      ,@(loop
+        for (type . rest) on (rest (type-expand-to 'or type)) 
+        collect
+        `(,(if (endp rest)
+               t
+             (alien-typep-form type 'alien))
+          ,(from-alien-form type 'alien))))))
+      
+
+
 (define-type-method to-alien-function ((type or) &optional copy-p)
   (let* ((expanded-type (type-expand-to 'or type))
         (functions (loop
@@ -1019,6 +1035,9 @@ (define-type-method reader-function ((type pointer) &key ref (inlined t))
   #'ref-pointer)
 
 
+
+;;; Null Pointer
+
 (define-type-method alien-type ((type null))
   (declare (ignore type))
   (alien-type 'pointer))
@@ -1027,6 +1046,14 @@ (define-type-method size-of ((type null) &key (inlined t))
   (assert-inlined type inlined)
   (size-of 'pointer))
 
+(define-type-method alien-typep-form ((type null) null)
+  (declare (ignore type)) 
+  `(null-pointer-p ,null))
+
+(define-type-method from-alien-form ((type null) null &key ref)
+  (declare (ignore type null ref))
+  nil)
+
 (define-type-method to-alien-form ((type null) null &optional copy-p)
   (declare (ignore type copy-p))
   `(progn ,null (make-pointer 0)))