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 2009/08/18 01:29:28 UTC

svn commit: r805209 - in /lucene/lucy/trunk/boilerplater: lib/Boilerplater/Type.pm lib/Boilerplater/Type/ lib/Boilerplater/Type/Primitive.pm t/100-type.t t/101-primitive_type.t

Author: marvin
Date: Mon Aug 17 23:29:28 2009
New Revision: 805209

URL: http://svn.apache.org/viewvc?rev=805209&view=rev
Log:
Commit LUCY-10, adding Boilerplater::Type and Boilerplater::Type::Primitive.

Added:
    lucene/lucy/trunk/boilerplater/lib/Boilerplater/Type/
    lucene/lucy/trunk/boilerplater/lib/Boilerplater/Type.pm   (with props)
    lucene/lucy/trunk/boilerplater/lib/Boilerplater/Type/Primitive.pm   (with props)
    lucene/lucy/trunk/boilerplater/t/100-type.t   (with props)
    lucene/lucy/trunk/boilerplater/t/101-primitive_type.t   (with props)

Added: lucene/lucy/trunk/boilerplater/lib/Boilerplater/Type.pm
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/boilerplater/lib/Boilerplater/Type.pm?rev=805209&view=auto
==============================================================================
--- lucene/lucy/trunk/boilerplater/lib/Boilerplater/Type.pm (added)
+++ lucene/lucy/trunk/boilerplater/lib/Boilerplater/Type.pm Mon Aug 17 23:29:28 2009
@@ -0,0 +1,177 @@
+use strict;
+use warnings;
+
+package Boilerplater::Type;
+use Boilerplater::Parcel;
+use Boilerplater::Util qw( verify_args );
+use Scalar::Util qw( blessed );
+use Carp;
+
+our %new_PARAMS = (
+    const       => undef,
+    specifier   => undef,
+    indirection => undef,
+    parcel      => undef,
+    c_string    => undef,
+);
+
+sub new {
+    my $either = shift;
+    my $package = ref($either) || $either;
+    confess( __PACKAGE__ . "is an abstract class" )
+        if $package eq __PACKAGE__;
+    verify_args( \%new_PARAMS, @_ ) or confess $@;
+    my $self = bless { %new_PARAMS, @_, }, $package;
+    if ( defined $self->{parcel} ) {
+        if ( !blessed( $self->{parcel} ) ) {
+            $self->{parcel}
+                = Boilerplater::Parcel->singleton( name => $self->{parcel} );
+        }
+        confess("Not a Boilerplater::Parcel")
+            unless $self->{parcel}->isa('Boilerplater::Parcel');
+    }
+    return $self;
+}
+
+sub get_specifier { shift->{specifier} }
+sub get_parcel    { shift->{parcel} }
+sub const         { shift->{const} }
+
+sub is_object      {0}
+sub is_primitive   {0}
+sub is_integer     {0}
+sub is_floating    {0}
+sub is_void        {0}
+sub is_composite   {0}
+sub is_string_type {0}
+
+sub equals {
+    my ( $self, $other ) = @_;
+    return 0 unless blessed($other);
+    return 0 unless $other->isa(__PACKAGE__);
+    return 1;
+}
+
+sub to_c { shift->{c_string} }
+sub set_c_string { $_[0]->{c_string} = $_[1] }
+
+1;
+
+__END__
+
+__POD__
+
+=head1 NAME
+
+Boilerplater::Type - A variable's type.
+
+=head1 METHODS
+
+=head2 new
+
+    my $type = MyType->new(
+        specifier   => 'char',    # default undef
+        indirection => undef,     # default 0
+        const       => 1,         # default undef
+        parcel      => undef,     # default undef
+        c_string    => undef,     # default undef
+    );
+
+Abstract constructor.
+
+=over
+
+=item *
+
+B<specifier> - The C name for the type, not including any indirection or array
+subscripts.  
+
+=item *
+
+B<indirection> - integer indicating level of indirection. Example: the C type
+"float**" has a specifier of "float" and indirection 2.
+
+=item *
+
+B<const> - should be true if the type is const.
+
+=item *
+
+B<parcel> - A Boilerplater::Parcel or a parcel name.
+
+=item *
+
+B<c_string> - The C representation of the type.
+
+=back
+
+=head2 equals
+
+    do_stuff() if $type->equals($other);
+
+Returns true if two Boilerplater::Type objects are equivalent.  The default
+implementation merely checks that C<$other> is a Boilerplater::Type object, so
+it should be overridden in all subclasses.
+
+=head2 to_c
+
+    # Declare variable "foo".
+    print $type->to_c . " foo;\n";
+
+Return the C representation of the type.
+
+=head2 set_c_string
+
+Set the C representation of the type.
+
+=head2 get_specifier get_parcel const
+
+Accessors.
+
+=head2 is_object is_primitive is_integer is_floating is_composite is_void
+
+    do_stuff() if $type->is_object;
+
+Shorthand for various $type->isa($package) calls.  
+
+=over
+
+=item * is_object: Boilerplater::Type::Object
+
+=item * is_primitive: Boilerplater::Type::Primitive
+
+=item * is_integer: Boilerplater::Type::Integer
+
+=item * is_floating: Boilerplater::Type::Float
+
+=item * is_void: Boilerplater::Type::Void
+
+=item * is_composite: Boilerplater::Type::Composite
+
+=back
+
+=head2 is_string_type
+
+Returns true if $type represents a Boilerplater type which holds unicode
+strings.
+
+=head1 COPYRIGHT AND LICENSE
+
+    /**
+     * Copyright 2009 The Apache Software Foundation
+     *
+     * Licensed under the Apache License, Version 2.0 (the "License");
+     * you may not use this file except in compliance with the License.
+     * You may obtain a copy of the License at
+     *
+     *     http://www.apache.org/licenses/LICENSE-2.0
+     *
+     * Unless required by applicable law or agreed to in writing, software
+     * distributed under the License is distributed on an "AS IS" BASIS,
+     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+     * implied.  See the License for the specific language governing
+     * permissions and limitations under the License.
+     */
+
+=cut
+

Propchange: lucene/lucy/trunk/boilerplater/lib/Boilerplater/Type.pm
------------------------------------------------------------------------------
    svn:eol-style = native

Added: lucene/lucy/trunk/boilerplater/lib/Boilerplater/Type/Primitive.pm
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/boilerplater/lib/Boilerplater/Type/Primitive.pm?rev=805209&view=auto
==============================================================================
--- lucene/lucy/trunk/boilerplater/lib/Boilerplater/Type/Primitive.pm (added)
+++ lucene/lucy/trunk/boilerplater/lib/Boilerplater/Type/Primitive.pm Mon Aug 17 23:29:28 2009
@@ -0,0 +1,81 @@
+use strict;
+use warnings;
+
+package Boilerplater::Type::Primitive;
+use base qw( Boilerplater::Type );
+use Boilerplater::Util qw( verify_args );
+use Scalar::Util qw( blessed );
+use Carp;
+
+our %new_PARAMS = (
+    const     => undef,
+    specifier => undef,
+    c_string  => undef,
+);
+
+sub new {
+    my ( $either, %args ) = @_;
+    my $package = ref($either) || $either;
+    confess( __PACKAGE__ . " is abstract" ) if $package eq __PACKAGE__;
+    verify_args( \%new_PARAMS, %args ) or confess $@;
+    return bless { %new_PARAMS, %args }, $package;
+}
+
+sub is_primitive {1}
+
+sub equals {
+    my ( $self, $other ) = @_;
+    return 0 unless blessed($other);
+    return 0 unless $other->isa(__PACKAGE__);
+    return 0 unless $self->{specifier} eq $other->{specifier};
+    return 0 if ( $self->{const} xor $other->{const} );
+    return 1;
+}
+
+1;
+
+__END__
+
+__POD__
+
+=head1 NAME
+
+Boilerplater::Type::Primitive - Abstract base class for primitive types.
+
+=head1 DESCRIPTION
+
+Boilerplater::Type::Primitive serves as a common parent class for primitive
+types including L<Boilerplater::Type::Integer> and
+L<Boilerplater::Type::Float>.
+
+=head1 METHODS
+
+=head2 new
+
+    my $type = MyPrimitiveType->new(
+        const     => 1,       # default: undef
+        specifier => 'char',  # default: undef
+        c_string  => 'char',  # default: undef
+    );
+
+Abstract constructor.  See L<Boilerplater::Type> for parameter definitions.
+
+=head1 COPYRIGHT AND LICENSE
+
+    /**
+     * Copyright 2009 The Apache Software Foundation
+     *
+     * Licensed under the Apache License, Version 2.0 (the "License");
+     * you may not use this file except in compliance with the License.
+     * You may obtain a copy of the License at
+     *
+     *     http://www.apache.org/licenses/LICENSE-2.0
+     *
+     * Unless required by applicable law or agreed to in writing, software
+     * distributed under the License is distributed on an "AS IS" BASIS,
+     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+     * implied.  See the License for the specific language governing
+     * permissions and limitations under the License.
+     */
+
+=cut

Propchange: lucene/lucy/trunk/boilerplater/lib/Boilerplater/Type/Primitive.pm
------------------------------------------------------------------------------
    svn:eol-style = native

Added: lucene/lucy/trunk/boilerplater/t/100-type.t
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/boilerplater/t/100-type.t?rev=805209&view=auto
==============================================================================
--- lucene/lucy/trunk/boilerplater/t/100-type.t (added)
+++ lucene/lucy/trunk/boilerplater/t/100-type.t Mon Aug 17 23:29:28 2009
@@ -0,0 +1,31 @@
+use strict;
+use warnings;
+
+package MyType;
+use base qw( Boilerplater::Type );
+
+package main;
+use Test::More tests => 12;
+use Boilerplater::Parcel;
+
+my $boil_parcel = Boilerplater::Parcel->singleton( name => 'Boil' );
+
+my $type = MyType->new( parcel => 'Boil', specifier => 'mytype_t' );
+is( $type->get_parcel, $boil_parcel,
+    "constructor changes parcel name to Parcel singleton" );
+
+ok( !defined $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" );
+is( $type->get_specifier, "mytype_t", "get_specifier()" );
+
+ok( !$type->is_object,      "is_object() false by default" );
+ok( !$type->is_integer,     "is_integer() false by default" );
+ok( !$type->is_floating,    "is_floating() false by default" );
+ok( !$type->is_void,        "is_void() false by default" );
+ok( !$type->is_composite,   "is_composite() false by default" );
+ok( !$type->is_string_type, "is_string_type() false by default" );
+
+ok( $type->equals( MyType->new ), "equals() depends solely on class" );
+

Propchange: lucene/lucy/trunk/boilerplater/t/100-type.t
------------------------------------------------------------------------------
    svn:eol-style = native

Added: lucene/lucy/trunk/boilerplater/t/101-primitive_type.t
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/boilerplater/t/101-primitive_type.t?rev=805209&view=auto
==============================================================================
--- lucene/lucy/trunk/boilerplater/t/101-primitive_type.t (added)
+++ lucene/lucy/trunk/boilerplater/t/101-primitive_type.t Mon Aug 17 23:29:28 2009
@@ -0,0 +1,21 @@
+use strict;
+use warnings;
+
+package MyPrimitiveType;
+use base qw( Boilerplater::Type::Primitive );
+
+package main;
+use Test::More tests => 4;
+
+my $type = MyPrimitiveType->new( specifier => 'hump_t' );
+ok( $type->is_primitive, "is_primitive" );
+
+my $other = MyPrimitiveType->new( specifier => 'hump_t' );
+ok( $type->equals($other), "equals()" );
+
+$other = MyPrimitiveType->new( specifier => 'dump_t' );
+ok( !$type->equals($other), "equals() spoiled by specifier" );
+
+$other = MyPrimitiveType->new( specifier => 'hump_t', const => 1 );
+ok( !$type->equals($other), "equals() spoiled by const" );
+

Propchange: lucene/lucy/trunk/boilerplater/t/101-primitive_type.t
------------------------------------------------------------------------------
    svn:eol-style = native