lucy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mar...@apache.org
Subject [lucy-commits] svn commit: r1062483 - in /incubator/lucy/trunk/clownfish: lib/Clownfish.xs lib/Clownfish/Type.pm src/CFCType.c src/CFCType.h t/100-type.t
Date Sun, 23 Jan 2011 18:10:30 GMT
Author: marvin
Date: Sun Jan 23 18:10:29 2011
New Revision: 1062483

URL: http://svn.apache.org/viewvc?rev=1062483&view=rev
Log:
Move some member variables for Clownfish::Type from Perl-space to C-space.

Modified:
    incubator/lucy/trunk/clownfish/lib/Clownfish.xs
    incubator/lucy/trunk/clownfish/lib/Clownfish/Type.pm
    incubator/lucy/trunk/clownfish/src/CFCType.c
    incubator/lucy/trunk/clownfish/src/CFCType.h
    incubator/lucy/trunk/clownfish/t/100-type.t

Modified: incubator/lucy/trunk/clownfish/lib/Clownfish.xs
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/lib/Clownfish.xs?rev=1062483&r1=1062482&r2=1062483&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/lib/Clownfish.xs (original)
+++ incubator/lucy/trunk/clownfish/lib/Clownfish.xs Sun Jan 23 18:10:29 2011
@@ -20,13 +20,45 @@
 
 #include "CFC.h"
 
+/* Rather than provide an XSUB for each accessor, we can use one multipath
+ * accessor function per class, with several Perl-space aliases.  All set
+ * functions have odd-numbered aliases, and all get functions have
+ * even-numbered aliases.  These two macros serve as bookends for the switch
+ * function.
+ */
+#define START_SET_OR_GET_SWITCH \
+    SV *retval = &PL_sv_undef; \
+    /* If called as a setter, make sure the extra arg is there. */ \
+    if (ix % 2 == 1) { \
+        if (items != 2) { croak("usage: $object->set_xxxxxx($val)"); } \
+    } \
+    else { \
+        if (items != 1) { croak("usage: $object->get_xxxxx()"); } \
+    } \
+    switch (ix) {
+
+#define END_SET_OR_GET_SWITCH \
+        default: croak("Internal error. ix: %d", ix); \
+    } \
+    if (ix % 2 == 0) { \
+        XPUSHs( sv_2mortal(retval) ); \
+        XSRETURN(1); \
+    } \
+    else { \
+        XSRETURN(0); \
+    } 
+
 MODULE = Clownfish    PACKAGE = Clownfish::Type
 
 SV*
-_new(klass)
+_new(klass, parcel, specifier, indirection, c_string)
     const char *klass;
+    SV *parcel;
+    const char *specifier;
+    int indirection;
+    const char *c_string;
 CODE:
-    CFCType *self = CFCType_new();
+    CFCType *self = CFCType_new(parcel, specifier, indirection, c_string);
     RETVAL = newSV(0);
 	sv_setref_pv(RETVAL, klass, (void*)self);
 OUTPUT: RETVAL
@@ -37,3 +69,41 @@ _destroy(self)
 PPCODE:
     CFCType_destroy(self);
 
+void
+_set_or_get(self, ...)
+    CFCType *self;
+ALIAS:
+    set_specifier   = 1
+    get_specifier   = 2
+    get_parcel      = 4
+    get_indirection = 6
+    set_c_string    = 7
+    to_c            = 8
+PPCODE:
+{
+    START_SET_OR_GET_SWITCH
+        case 1:
+            CFCType_set_specifier(self, SvPV_nolen(ST(1)));
+            break;
+        case 2: {
+                const char *specifier = CFCType_get_specifier(self);
+                retval = newSVpvn(specifier, strlen(specifier));
+            }
+            break;
+        case 4:
+            retval = newSVsv((SV*)CFCType_get_parcel(self));
+            break;
+        case 6:
+            retval = newSViv(CFCType_get_indirection(self));
+            break;
+        case 7:
+            CFCType_set_c_string(self, SvPV_nolen(ST(1)));
+        case 8: {
+                const char *c_string = CFCType_to_c(self);
+                retval = newSVpvn(c_string, strlen(c_string));
+            }
+            break;
+    END_SET_OR_GET_SWITCH
+}
+
+

Modified: incubator/lucy/trunk/clownfish/lib/Clownfish/Type.pm
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/lib/Clownfish/Type.pm?rev=1062483&r1=1062482&r2=1062483&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/lib/Clownfish/Type.pm (original)
+++ incubator/lucy/trunk/clownfish/lib/Clownfish/Type.pm Sun Jan 23 18:10:29 2011
@@ -25,10 +25,6 @@ use Carp;
 
 # Inside-out member vars.
 our %const;
-our %specifier;
-our %indirection;
-our %parcel;
-our %c_string;
 our %nullable;
 
 our %new_PARAMS = (
@@ -45,7 +41,6 @@ sub new {
     confess( __PACKAGE__ . "is an abstract class" )
         if $package eq __PACKAGE__;
     verify_args( \%new_PARAMS, %args ) or confess $@;
-    my $self = $package->_new();
 
     my $parcel = $args{parcel};
     if ( defined $parcel ) {
@@ -55,34 +50,27 @@ sub new {
         confess("Not a Clownfish::Parcel")
             unless $parcel->isa('Clownfish::Parcel');
     }
-    $parcel{$self} = $parcel;
 
-    $const{$self}       = $args{const};
-    $specifier{$self}   = $args{specifier};
-    $indirection{$self} = $args{indirection};
-    $c_string{$self}    = $args{c_string};
+    my $const       = $args{const}       || 0;
+    my $indirection = $args{indirection} || 0;
+    my $specifier   = $args{specifier}   || '';
+    my $c_string    = $args{c_string}    || '';
 
+    my $self = $package->_new( $parcel, $specifier, $indirection, $c_string );
+    $const{$self} = $const;
     return $self;
 }
 
 sub DESTROY {
     my $self = shift;
-    delete $parcel{$self};
     delete $const{$self};
-    delete $specifier{$self};
-    delete $indirection{$self};
-    delete $c_string{$self};
     delete $nullable{$self};
     _destroy($self);
 }
 
-sub get_specifier   { $specifier{ +shift } }
-sub get_parcel      { $parcel{ +shift } }
-sub get_indirection { $indirection{ +shift } }
 sub const           { $const{ +shift } }
 sub nullable        { $nullable{ +shift } }
 
-sub set_specifier { $specifier{ $_[0] } = $_[1] }
 sub set_nullable  { $nullable{ $_[0] }  = $_[1] }
 
 sub is_object      {0}
@@ -100,9 +88,6 @@ sub equals {
     return 1;
 }
 
-sub to_c { $c_string{ +shift } }
-sub set_c_string { $c_string{ $_[0] } = $_[1] }
-
 1;
 
 __END__

Modified: incubator/lucy/trunk/clownfish/src/CFCType.c
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/src/CFCType.c?rev=1062483&r1=1062482&r2=1062483&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/src/CFCType.c (original)
+++ incubator/lucy/trunk/clownfish/src/CFCType.c Sun Jan 23 18:10:29 2011
@@ -15,25 +15,84 @@
  */
 
 #include <stdlib.h>
+#include "EXTERN.h"
+#include "perl.h"
+#include "XSUB.h"
 
 #include "CFCType.h"
 
 struct CFCType {
     int   flags;
+    char *specifier;
+    int   indirection;
+    void *parcel;
+    char *c_string;
 };
 
 CFCType*
-CFCType_new()
+CFCType_new(void *parcel, const char *specifier, int indirection,
+            const char *c_string)
 {
     CFCType *self = (CFCType*)malloc(sizeof(CFCType));
     if (!self) { croak("malloc failed"); }
+    return CFCType_init(self, parcel, specifier, indirection, c_string);
+}
+
+CFCType*
+CFCType_init(CFCType *self, void *parcel, const char *specifier,
+             int indirection, const char *c_string)
+{
+    self->parcel      = newSVsv((SV*)parcel);
+    self->specifier   = savepv(specifier);
+    self->indirection = indirection;
+    self->c_string    = c_string ? savepv(c_string) : savepv("");
     return self;
 }
 
 void
 CFCType_destroy(CFCType *self)
 {
+    Safefree(self->specifier);
+    Safefree(self->c_string);
+    SvREFCNT_dec(self->parcel);
     free(self);
 }
 
+void
+CFCType_set_specifier(CFCType *self, const char *specifier)
+{
+    Safefree(self->specifier);
+    self->specifier = savepv(specifier);
+}
+
+const char*
+CFCType_get_specifier(CFCType *self)
+{
+    return self->specifier;
+}
+
+int
+CFCType_get_indirection(CFCType *self)
+{
+    return self->indirection;
+}
+
+void*
+CFCType_get_parcel(CFCType *self)
+{
+    return self->parcel;
+}
+
+void
+CFCType_set_c_string(CFCType *self, const char *c_string)
+{
+    Safefree(self->c_string);
+    self->c_string = savepv(c_string);
+}
+
+const char*
+CFCType_to_c(CFCType *self)
+{
+    return self->c_string;
+}
 

Modified: incubator/lucy/trunk/clownfish/src/CFCType.h
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/src/CFCType.h?rev=1062483&r1=1062482&r2=1062483&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/src/CFCType.h (original)
+++ incubator/lucy/trunk/clownfish/src/CFCType.h Sun Jan 23 18:10:29 2011
@@ -17,8 +17,31 @@
 typedef struct CFCType CFCType;
 
 CFCType*
-CFCType_new();
+CFCType_new(void *parcel, const char *specifier, int indirection,
+            const char *c_string);
+
+CFCType*
+CFCType_init(CFCType *self, void *parcel, const char *specifier,
+             int indirection, const char *c_string);
 
 void
 CFCType_destroy(CFCType *self);
 
+void
+CFCType_set_specifier(CFCType *self, const char *specifier);
+
+const char*
+CFCType_get_specifier(CFCType *self);
+
+int
+CFCType_get_indirection(CFCType *self);
+
+void*
+CFCType_get_parcel(CFCType *self);
+
+void
+CFCType_set_c_string(CFCType *self, const char *c_string);
+
+const char*
+CFCType_to_c(CFCType *self);
+

Modified: incubator/lucy/trunk/clownfish/t/100-type.t
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/t/100-type.t?rev=1062483&r1=1062482&r2=1062483&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/t/100-type.t (original)
+++ incubator/lucy/trunk/clownfish/t/100-type.t Sun Jan 23 18:10:29 2011
@@ -29,7 +29,7 @@ my $type = MyType->new( parcel => 'Neato
 is( $type->get_parcel, $neato_parcel,
     "constructor changes parcel name to Parcel singleton" );
 
-ok( !defined $type->to_c, "to_c()" );
+is( $type->to_c, '', "to_c()" );
 $type->set_c_string("mytype_t");
 is( $type->to_c, "mytype_t", "set_c_string()" );
 ok( !$type->const, "const() is off by default" );



Mime
View raw message