]> gitweb.factorcode.org Git - factor.git/commitdiff
alien.marshall: refactored unmarshalling words
authorJeremy Hughes <jedahu@gmail.com>
Tue, 21 Jul 2009 05:09:32 +0000 (17:09 +1200)
committerJeremy Hughes <jedahu@gmail.com>
Tue, 21 Jul 2009 05:10:04 +0000 (17:10 +1200)
extra/alien/marshall/marshall-docs.factor
extra/alien/marshall/marshall.factor

index 6002b0c1c3a55e4143402269d53bf8214774b255..deac9fd186b6b9677daf979ea42e1f4792e716e6 100644 (file)
@@ -327,7 +327,7 @@ HELP: out-arg-unmarshaller
     "for all types except pointers to non-const primitives."
 } ;
 
-HELP: pointer-unmarshaller
+HELP: class-unmarshaller
 { $values
     { "type" " a C type string" }
     { "quot" quotation }
index 85b157e4a02db1b631d76a03820b78e6df8f8785..deef94dc9bf0915bc0dd03d1ee28846c6abfbfa9 100644 (file)
@@ -11,7 +11,8 @@ specialized-arrays.long specialized-arrays.longlong
 specialized-arrays.short specialized-arrays.uchar
 specialized-arrays.uint specialized-arrays.ulong
 specialized-arrays.ulonglong specialized-arrays.ushort strings
-unix.utilities vocabs.parser words libc.private struct-arrays ;
+unix.utilities vocabs.parser words libc.private struct-arrays
+locals generalizations ;
 IN: alien.marshall
 
 << primitive-types [ [ "void*" = ] [ "bool" = ] bi or not ]
@@ -269,33 +270,51 @@ ALIAS: marshall-void* marshall-pointer
 : ?malloc-byte-array ( c-type -- alien )
     dup alien? [ malloc-byte-array ] unless ;
 
-: struct-unmarshaller ( type -- quot )
-    current-vocab lookup [
-        dup superclasses [ \ struct-wrapper = ] any? [
-            '[ ?malloc-byte-array _ new swap >>underlying ]
-        ] [ drop [ ] ] if
-    ] [ [ ] ] if* ;
-
-: pointer-unmarshaller ( type -- quot )
-    type-sans-pointer current-vocab lookup [
-        dup superclasses [ \ alien-wrapper = ] any? [
-            '[ _ new swap >>underlying unmarshall-cast ]
-        ] [ drop [ ] ] if
-    ] [ [ ] ] if* ;
+:: x-unmarshaller ( type type-quot wrapper-test def clean -- quot/f )
+    type type-quot call current-vocab lookup [
+        dup superclasses wrapper-test any?
+        [ def call ] [ drop clean call f ] if
+    ] [ clean call f ] if* ; inline
+
+: struct-unmarshaller ( type -- quot/f )
+    [ ] [ \ struct-wrapper = ]
+    [ '[ ?malloc-byte-array _ new swap >>underlying ] ]
+    [ ]
+    x-unmarshaller ;
+
+: class-unmarshaller ( type -- quot/f )
+    [ type-sans-pointer ] [ \ alien-wrapper = ]
+    [ '[ ?malloc-byte-array _ new swap >>underlying ] ]
+    [ ]
+    x-unmarshaller ;
+
+: template-class-unmarshaller ( type -- quot/f )
+    [ parse-c++-type [ name>> ] keep swap ] [ \ template-wrapper = ]
+    [ '[ _ _ new swap >>type swap >>underlying unmarshall-cast ] ]
+    [ drop ]
+    x-unmarshaller ;
+
+: non-primitive-unmarshaller ( type -- quot/f )
+    {
+        { [ dup template-class? ]
+          [ template-class-unmarshaller ] }
+        { [ dup pointer? ] [ class-unmarshaller ] }
+        [ struct-unmarshaller ]
+    } cond ;
 
 : unmarshaller ( type -- quot )
-    factorize-type dup primitive-unmarshaller [ nip ] [
-        dup pointer?
-        [ pointer-unmarshaller ]
-        [ struct-unmarshaller ] if
-    ] if* ;
+    factorize-type {
+        [ primitive-unmarshaller ]
+        [ non-primitive-unmarshaller ]
+        [ drop [ ] ]
+    } 1|| ;
 
 : struct-field-unmarshaller ( type -- quot )
-    factorize-type dup struct-primitive-unmarshaller [ nip ] [
-        dup pointer?
-        [ pointer-unmarshaller ]
-        [ struct-unmarshaller ] if
-    ] if* ;
+    factorize-type {
+        [ struct-primitive-unmarshaller ]
+        [ non-primitive-unmarshaller ]
+        [ drop [ ] ]
+    } 1|| ;
 
 : out-arg-unmarshaller ( type -- quot )
     dup pointer-to-non-const-primitive?