1 ###############################################################################
3 # Package: NaturalDocs::DefineMembers
5 ###############################################################################
7 # A custom Perl pragma to define member constants and accessors for use in Natural Docs objects while supporting inheritance.
9 # Each member will be defined as a numeric constant which should be used as that variable's index into the object arrayref.
10 # They will be assigned sequentially from zero, and take into account any members defined this way in parent classes. Note
11 # that you can *not* use multiple inheritance with this method.
13 # If a parameter ends in parenthesis, it will be generated as an accessor for the previous member. If it also starts with "Set",
14 # the accessor will accept a single parameter to replace the value with. If it's followed with "duparrayref", it will assume the
15 # parameter is either an arrayref or undef, and if the former, will duplicate it to set the value.
19 # > package MyPackage;
21 # > use NaturalDocs::DefineMembers 'VAR_A', 'VarA()', 'SetVarA()',
22 # > 'VAR_B', 'VarB()',
24 # > 'VAR_D', 'VarD()', 'SetVarD() duparrayref';
28 # > my ($self, $c) = @_;
29 # > $self->[VAR_C] = $c;
32 ###############################################################################
34 # This file is part of Natural Docs, which is Copyright © 2003-2010 Greg Valure
35 # Natural Docs is licensed under version 3 of the GNU Affero General Public License (AGPL)
36 # Refer to License.txt for the complete details
39 package NaturalDocs::DefineMembers;
41 sub import #(member, member, ...)
43 my ($self, @parameters) = @_;
44 my $package = caller();
47 my $parent = ${$package . '::ISA'}[0];
50 my $memberConstant = 0;
53 if (defined $parent && $parent->can('END_OF_MEMBERS'))
54 { $memberConstant = $parent->END_OF_MEMBERS(); };
56 my $code = '{ package ' . $package . ";\n";
58 foreach my $parameter (@parameters)
60 if ($parameter =~ /^(.+)\(\) *(duparrayref)?$/i)
62 my ($functionName, $pragma) = ($1, lc($2));
64 if ($functionName =~ /^Set/)
66 if ($pragma eq 'duparrayref')
69 'sub ' . $functionName . '
72 { $_[0]->[' . $lastMemberName . '] = [ @{$_[1]} ]; }
74 { $_[0]->[' . $lastMemberName . '] = undef; };
79 $code .= 'sub ' . $functionName . ' { $_[0]->[' . $lastMemberName . '] = $_[1]; };' . "\n";
84 $code .= 'sub ' . $functionName . ' { return $_[0]->[' . $lastMemberName . ']; };' . "\n";
89 $code .= 'use constant ' . $parameter . ' => ' . $memberConstant . ";\n";
91 $lastMemberName = $parameter;
95 $code .= 'use constant END_OF_MEMBERS => ' . $memberConstant . ";\n";