You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucy.apache.org by ma...@apache.org on 2011/02/07 05:36:15 UTC

[lucy-commits] svn commit: r1067854 - in /incubator/lucy/trunk/clownfish: lib/Clownfish.xs lib/Clownfish/Parser.pm lib/Clownfish/Type.pm lib/Clownfish/Type/Composite.pm lib/Clownfish/Type/Void.pm t/108-composite_type.t t/600-parser.t

Author: marvin
Date: Mon Feb  7 04:36:15 2011
New Revision: 1067854

URL: http://svn.apache.org/viewvc?rev=1067854&view=rev
Log:
Absorb the functionality of Clownfish::Type::Composite into Clownfish::Type.

Removed:
    incubator/lucy/trunk/clownfish/lib/Clownfish/Type/Composite.pm
Modified:
    incubator/lucy/trunk/clownfish/lib/Clownfish.xs
    incubator/lucy/trunk/clownfish/lib/Clownfish/Parser.pm
    incubator/lucy/trunk/clownfish/lib/Clownfish/Type.pm
    incubator/lucy/trunk/clownfish/lib/Clownfish/Type/Void.pm
    incubator/lucy/trunk/clownfish/t/108-composite_type.t
    incubator/lucy/trunk/clownfish/t/600-parser.t

Modified: incubator/lucy/trunk/clownfish/lib/Clownfish.xs
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/lib/Clownfish.xs?rev=1067854&r1=1067853&r2=1067854&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/lib/Clownfish.xs (original)
+++ incubator/lucy/trunk/clownfish/lib/Clownfish.xs Mon Feb  7 04:36:15 2011
@@ -443,13 +443,13 @@ CODE:
 OUTPUT: RETVAL
 
 void
-DESTROY(self)
+_destroy(self)
     CFCType *self;
 PPCODE:
     CFCType_destroy(self);
 
 int
-equals(self, other)
+_equals(self, other)
     CFCType *self;
     CFCType *other;
 CODE:

Modified: incubator/lucy/trunk/clownfish/lib/Clownfish/Parser.pm
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/lib/Clownfish/Parser.pm?rev=1067854&r1=1067853&r2=1067854&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/lib/Clownfish/Parser.pm (original)
+++ incubator/lucy/trunk/clownfish/lib/Clownfish/Parser.pm Mon Feb  7 04:36:15 2011
@@ -27,7 +27,6 @@ use Clownfish::Type::Void;
 use Clownfish::Type::VAList;
 use Clownfish::Type::Arbitrary;
 use Clownfish::Type::Object;
-use Clownfish::Type::Composite;
 use Clownfish::Variable;
 use Clownfish::DocuComment;
 use Clownfish::Function;
@@ -408,7 +407,7 @@ sub simple_or_composite_type {
                 $args{indirection}++;
             }
         }
-        return Clownfish::Type::Composite->new(%args);
+        return Clownfish::Type->new_composite(%args);
     }
 }
 

Modified: incubator/lucy/trunk/clownfish/lib/Clownfish/Type.pm
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/lib/Clownfish/Type.pm?rev=1067854&r1=1067853&r2=1067854&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/lib/Clownfish/Type.pm (original)
+++ incubator/lucy/trunk/clownfish/lib/Clownfish/Type.pm Mon Feb  7 04:36:15 2011
@@ -19,11 +19,13 @@ use warnings;
 package Clownfish::Type;
 use Clownfish;
 use Clownfish::Parcel;
-use Clownfish::Util qw( verify_args );
+use Clownfish::Util qw( verify_args a_isa_b );
 use Scalar::Util qw( blessed );
 use Carp;
 
 # Inside-out member vars.
+our %array;
+our %child;
 
 our %new_PARAMS = (
     const       => undef,
@@ -45,8 +47,6 @@ our %new_PARAMS = (
 sub new {
     my ( $either, %args ) = @_;
     my $package = ref($either) || $either;
-    confess( __PACKAGE__ . "is an abstract class" )
-        if $package eq __PACKAGE__;
     verify_args( \%new_PARAMS, %args ) or confess $@;
 
     my $flags = 0;
@@ -79,6 +79,65 @@ sub new {
         $c_string );
 }
 
+
+our %new_composite_PARAMS = (
+    child       => undef,
+    indirection => undef,
+    array       => undef,
+    nullable    => undef,
+);
+
+sub new_composite {
+    my ( $either, %args ) = @_;
+    my $array    = delete $args{array};
+    my $child    = delete $args{child};
+    my $nullable = delete $args{nullable};
+    $args{indirection} ||= 0;
+    confess("Missing required param 'child'")
+        unless a_isa_b( $child, "Clownfish::Type" );
+    verify_args( \%new_composite_PARAMS, %args ) or confess $@;
+    my $self = $either->new(
+        %args,
+        specifier => $child->get_specifier,
+        composite => 1
+    );
+    $child{$self} = $child;
+    $array{$self} = $array;
+    $self->set_nullable($nullable);
+
+    # Cache C representation.
+    # NOTE: Array postfixes are NOT included.
+    my $string = $child->to_c;
+    for ( my $i = 0; $i < $self->get_indirection; $i++ ) {
+        $string .= '*';
+    }
+    $self->set_c_string($string);
+
+    return $self;
+}
+
+sub DESTROY {
+    my $self = shift;
+    delete $array{$self};
+    delete $child{$self};
+    $self->_destroy;
+}
+
+sub get_array     { $array{ +shift } }
+sub _get_child    { $child{ +shift } }
+
+sub equals {
+    my ( $self, $other ) = @_;
+    my $child = $self->_get_child;
+    if ($child) {
+        return 0 unless $other->_get_child;
+        return 0 unless $child->equals( $other->_get_child );
+    }
+    return 0 if ( $self->get_array xor $other->get_array );
+    return 0 if ( $self->get_array and $self->get_array ne $other->get_array );
+    return $self->_equals($other);
+}
+
 1;
 
 __END__
@@ -101,7 +160,7 @@ Clownfish::Type - A variable's type.
         c_string    => undef,     # default undef
     );
 
-Abstract constructor.
+Generic constructor.
 
 =over
 
@@ -127,6 +186,37 @@ B<parcel> - A Clownfish::Parcel or a par
 
 B<c_string> - The C representation of the type.
 
+=head2 new_composite
+
+    my $type = Clownfish::Type->new_composite(
+        child       => $char_type,    # required
+        indirection => undef,         # default 0
+        array       => '[]',          # default undef,
+        const       => 1,             # default undef
+    );
+
+Constructor for a composite type which is made up of repetitions of a single,
+uniform subtype.
+
+=over
+
+=item *
+
+B<child> - The Type which the composite is comprised of.
+
+=item *
+
+B<indirection> - integer indicating level of indirection. Example: the C type
+"float**" has indirection 2.
+
+=item *
+
+B<array> - A string describing an array postfix.  
+
+=item *
+
+B<const> - should be 1 if the type is const.
+
 =back
 
 =head2 equals
@@ -146,7 +236,7 @@ Return the C representation of the type.
 
 Set the C representation of the type.
 
-=head2 get_specifier get_parcel get_indirection const nullable set_specifier set_nullable
+=head2 get_specifier get_parcel get_indirection get_array const nullable set_specifier set_nullable
 
 Accessors.
 
@@ -168,7 +258,7 @@ Shorthand for various $type->isa($packag
 
 =item * is_void: Clownfish::Type::Void
 
-=item * is_composite: Clownfish::Type::Composite
+=item * is_composite: constructed via new_composite().
 
 =back
 

Modified: incubator/lucy/trunk/clownfish/lib/Clownfish/Type/Void.pm
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/lib/Clownfish/Type/Void.pm?rev=1067854&r1=1067853&r2=1067854&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/lib/Clownfish/Type/Void.pm (original)
+++ incubator/lucy/trunk/clownfish/lib/Clownfish/Type/Void.pm Mon Feb  7 04:36:15 2011
@@ -58,8 +58,8 @@ Clownfish::Type::Void - The void Type.
 =head1 DESCRIPTION
 
 Clownfish::Type::Void is used to represent a void return type.  It is also
-used in conjuction with with L<Clownfish::Type::Composite> to support the
-C<void*> opaque pointer type.
+used in conjuction with with C<< Clownfish::Type->new_composite >> to support
+the C<void*> opaque pointer type.
 
 =head1 METHODS
 

Modified: incubator/lucy/trunk/clownfish/t/108-composite_type.t
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/t/108-composite_type.t?rev=1067854&r1=1067853&r2=1067854&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/t/108-composite_type.t (original)
+++ incubator/lucy/trunk/clownfish/t/108-composite_type.t Mon Feb  7 04:36:15 2011
@@ -43,21 +43,22 @@ my @composite_type_strings = (
 );
 
 for my $input (@composite_type_strings) {
-    isa_ok( $parser->type($input), "Clownfish::Type::Composite", $input );
+    my $type = $parser->type($input);
+    ok( $type && $type->is_composite, $input );
 }
 
-eval { my $type = Clownfish::Type::Composite->new };
+eval { my $type = Clownfish::Type->new_composite };
 like( $@, qr/child/i, "child required" );
 
 my $foo_type = Clownfish::Type::Object->new( specifier => 'Foo' );
-my $composite_type = Clownfish::Type::Composite->new(
+my $composite_type = Clownfish::Type->new_composite(
     child       => $foo_type,
     indirection => 1,
 );
 is( $composite_type->get_specifier,
     'Foo', "get_specifier delegates to child" );
 
-my $other = Clownfish::Type::Composite->new(
+my $other = Clownfish::Type->new_composite(
     child       => $foo_type,
     indirection => 1,
 );
@@ -65,7 +66,7 @@ ok( $composite_type->equals($other), "eq
 ok( $composite_type->is_composite,   "is_composite" );
 
 my $bar_type = Clownfish::Type::Object->new( specifier => 'Bar' );
-my $bar_composite = Clownfish::Type::Composite->new(
+my $bar_composite = Clownfish::Type->new_composite(
     child       => $bar_type,
     indirection => 1,
 );

Modified: incubator/lucy/trunk/clownfish/t/600-parser.t
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/t/600-parser.t?rev=1067854&r1=1067853&r2=1067854&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/t/600-parser.t (original)
+++ incubator/lucy/trunk/clownfish/t/600-parser.t Mon Feb  7 04:36:15 2011
@@ -104,8 +104,7 @@ is( $parser->scalar_constant($_), $_, "s
 my @composites = ( 'int[]', "i32_t **", "Foo **", "Foo ***", "const void *" );
 for my $composite (@composites) {
     my $parsed = $parser->type($composite);
-    isa_ok( $parsed, "Clownfish::Type::Composite",
-        "composite_type: $composite" );
+    ok( $parsed && $parsed->is_composite, "composite_type: $composite" );
 }
 
 my @object_types = ( 'Obj *', "incremented Foo*", "decremented CharBuf *" );