]> gitweb.factorcode.org Git - factor.git/commitdiff
alien.cxx: methods and virtual methods
authorJeremy Hughes <jedahu@gmail.com>
Wed, 22 Jul 2009 07:20:26 +0000 (19:20 +1200)
committerJeremy Hughes <jedahu@gmail.com>
Wed, 22 Jul 2009 07:25:13 +0000 (19:25 +1200)
extra/alien/cxx/cxx.factor
extra/alien/cxx/parser/parser.factor
extra/alien/cxx/syntax/syntax-tests.factor
extra/alien/cxx/syntax/syntax.factor

index ab7ff416fa6fb8bf2f68464b732d9b694f8557f0..9d0ee24f505f6df8c400162e8e1529189bc5f204 100644 (file)
@@ -3,7 +3,8 @@
 USING: accessors alien.c-types alien.cxx.parser alien.marshall
 alien.inline.types classes.mixin classes.tuple kernel namespaces
 assocs sequences parser classes.parser alien.marshall.syntax
-interpolate locals effects io strings ;
+interpolate locals effects io strings make vocabs.parser words
+generic fry quotations ;
 IN: alien.cxx
 
 <PRIVATE
@@ -22,9 +23,12 @@ PRIVATE>
     [ [ class-tuple-word ] [ class-mixin ] bi dup ] dip
     add-mixin-instance define-class-tuple ;
 
-:: define-c++-method ( class-name name types effect -- )
+:: define-c++-method ( class-name generic name types effect virtual -- )
+    [ name % "_" % class-name { { CHAR: : CHAR: _ } } substitute % ] "" make           :> name'
     effect [ in>> "self" suffix ] [ out>> ] bi <effect> :> effect'
-    types class-name "*" append suffix :> types'
-    effect in>> "," join :> args
-    SBUF" " dup [ I[ return self->${name}(${args});]I ] with-output-stream >string :> body
-    name types' effect' body define-c-marshalled ;
+    types class-name "*" append suffix                  :> types'
+    effect in>> "," join                                :> args
+    class-name virtual [ "#" append ] unless current-vocab lookup                  :> class
+    SBUF" " clone dup [ I[ return self->${name}(${args});]I ] with-output-stream >string :> body
+    name' types' effect' body define-c-marshalled
+    class generic create-method name' current-vocab lookup 1quotation define ;
index 84425649dab456bf70ef399249e9e0d812ec7cba..5afaab29e006efb9d10b0233632dbb75872053a7 100644 (file)
@@ -6,5 +6,5 @@ IN: alien.cxx.parser
 : parse-c++-class-definition ( -- class superclass-mixin )
     scan scan-word ;
 
-: parse-c++-method-definition ( -- class-name name types effect )
-    scan function-types-effect ;
+: parse-c++-method-definition ( -- class-name generic name types effect )
+    scan scan-word function-types-effect ;
index 4b853770c2ee18a504cd0d01b9c8a2da2e969b3d..24f685a1974cf7650637608d1e41882294805dce 100644 (file)
@@ -1,7 +1,7 @@
 ! Copyright (C) 2009 Jeremy Hughes.
 ! See http://factorcode.org/license.txt for BSD license.
 USING: tools.test alien.cxx.syntax alien.inline.syntax
-alien.marshall.syntax alien.marshall ;
+alien.marshall.syntax alien.marshall accessors kernel ;
 IN: alien.cxx.syntax.tests
 
 DELETE-C-LIBRARY: test
@@ -15,7 +15,9 @@ C-TYPEDEF: std::string string
 
 C++-CLASS: std::string c++-root
 
-C++-METHOD: std::string const-char* c_str ( )
+GENERIC: to-string ( obj -- str )
+
+C++-METHOD: std::string to-string const-char* c_str ( )
 
 CM-FUNCTION: std::string* new_string ( const-char* s )
     return new std::string(s);
@@ -25,8 +27,87 @@ CM-FUNCTION: std::string* new_string ( const-char* s )
 
 ALIAS: <std::string> new_string
 
-ALIAS: to-string c_str
-
 { 1 1 } [ new_string ] must-infer-as
-{ 1 1 } [ c_str ] must-infer-as
+{ 1 1 } [ c_str_std__string ] must-infer-as
+[ t ] [ "abc" <std::string> std::string? ] unit-test
 [ "abc" ] [ "abc" <std::string> to-string ] unit-test
+
+
+DELETE-C-LIBRARY: inheritance
+C-LIBRARY: inheritance
+
+COMPILE-AS-C++
+
+C-INCLUDE: <cstring>
+
+RAW-C:
+class alpha {
+    public:
+    alpha(const char* s) {
+        str = s;
+    };
+    const char* render() {
+        return str;
+    };
+    virtual const char* chop() {
+        return str;
+    };
+    virtual int length() {
+        return strlen(str);
+    };
+    const char* str;
+};
+
+class beta : alpha {
+    public:
+    beta(const char* s) : alpha(s + 1) { };
+    const char* render() {
+        return str + 1;
+    };
+    virtual const char* chop() {
+        return str + 2;
+    };
+};
+;
+
+C++-CLASS: alpha c++-root
+C++-CLASS: beta alpha
+
+CM-FUNCTION: alpha* new_alpha ( const-char* s )
+    return new alpha(s);
+;
+
+CM-FUNCTION: beta* new_beta ( const-char* s )
+    return new beta(s);
+;
+
+ALIAS: <alpha> new_alpha
+ALIAS: <beta> new_beta
+
+GENERIC: render ( obj -- obj )
+GENERIC: chop ( obj -- obj )
+GENERIC: length ( obj -- n )
+
+C++-METHOD: alpha render const-char* render ( )
+C++-METHOD: beta render const-char* render ( )
+C++-VIRTUAL: alpha chop const-char* chop ( )
+C++-VIRTUAL: beta chop const-char* chop ( )
+C++-VIRTUAL: alpha length int length ( )
+
+;C-LIBRARY
+
+{ 1 1 } [ render_alpha ] must-infer-as
+{ 1 1 } [ chop_beta ] must-infer-as
+{ 1 1 } [ length_alpha ] must-infer-as
+[ t ] [ "x" <alpha> alpha#? ] unit-test
+[ t ] [ "x" <alpha> alpha? ] unit-test
+[ t ] [ "x" <beta> alpha? ] unit-test
+[ f ] [ "x" <beta> alpha#? ] unit-test
+[ 5 ] [ "hello" <alpha> length ] unit-test
+[ 4 ] [ "hello" <beta> length ] unit-test
+[ "hello" ] [ "hello" <alpha> render ] unit-test
+[ "llo" ] [ "hello" <beta> render ] unit-test
+[ "ello" ] [ "hello" <beta> underlying>> \ alpha# new swap >>underlying render ] unit-test
+[ "hello" ] [ "hello" <alpha> chop ] unit-test
+[ "lo" ] [ "hello" <beta> chop ] unit-test
+[ "lo" ] [ "hello" <beta> underlying>> \ alpha# new swap >>underlying chop ] unit-test
index 59cf10e7de48d58736ac19289775bc765547aca6..66c72c1c2bac9d4f6e92122907f10b75ac95d5a3 100644 (file)
@@ -7,4 +7,7 @@ SYNTAX: C++-CLASS:
     parse-c++-class-definition define-c++-class ;
 
 SYNTAX: C++-METHOD:
-    parse-c++-method-definition define-c++-method ;
+    parse-c++-method-definition f define-c++-method ;
+
+SYNTAX: C++-VIRTUAL:
+    parse-c++-method-definition t define-c++-method ;