1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
|
# You may distribute under the terms of either the GNU General Public License
# or the Artistic License (the same terms as Perl itself)
#
# (C) Paul Evans, 2011-2024 -- leonerd@leonerd.org.uk
use v5.26;
use warnings;
use Object::Pad 0.800;
package Tangence::Meta::Class 0.33;
class Tangence::Meta::Class :strict(params);
use Carp;
=head1 NAME
C<Tangence::Meta::Class> - structure representing one C<Tangence> class
=head1 DESCRIPTION
This data structure object stores information about one L<Tangence> class.
Once constructed and defined, such objects are immutable.
=cut
=head1 CONSTRUCTOR
=cut
=head2 new
$class = Tangence::Meta::Class->new( name => $name )
Returns a new instance representing the given name.
=cut
field $name :param :reader;
field $defined :reader = 0;
field @superclasses;
field %methods;
field %events;
field %properties;
=head2 define
$class->define( %args )
Provides a definition for the class.
=over 8
=item methods => HASH
=item events => HASH
=item properties => HASH
Optional HASH references containing metadata about methods, events and
properties, as instances of L<Tangence::Meta::Method>,
L<Tangence::Meta::Event> or L<Tangence::Meta::Property>.
=item superclasses => ARRAY
Optional ARRAY reference containing superclasses as
C<Tangence::Meta::Class> references.
=back
=cut
method define ( %args )
{
$defined and croak "Cannot define $name twice";
$defined++;
@superclasses = @{ delete $args{superclasses} // [] };
%methods = %{ delete $args{methods} // {} };
%events = %{ delete $args{events} // {} };
%properties = %{ delete $args{properties} // {} };
}
=head1 ACCESSORS
=cut
=head2 defined
$defined = $class->defined
Returns true if a definintion for the class has been provided using C<define>.
=cut
=head2 name
$name = $class->name
Returns the name of the class
=cut
=head2 perlname
$perlname = $class->perlname
Returns the perl name of the class. This will be the Tangence name, with dots
replaced by double colons (C<::>).
=cut
method perlname
{
( my $perlname = $self->name ) =~ s{\.}{::}g; # s///rg in 5.14
return $perlname;
}
=head2 direct_superclasses
@superclasses = $class->direct_superclasses
Return the direct superclasses in a list of C<Tangence::Meta::Class>
references.
=cut
method direct_superclasses
{
$defined or croak "$name is not yet defined";
return @superclasses;
}
=head2 direct_methods
$methods = $class->direct_methods
Return the methods that this class directly defines (rather than inheriting
from superclasses) as a HASH reference mapping names to
L<Tangence::Meta::Method> instances.
=cut
method direct_methods
{
$defined or croak "$name is not yet defined";
return { %methods };
}
=head2 direct_events
$events = $class->direct_events
Return the events that this class directly defines (rather than inheriting
from superclasses) as a HASH reference mapping names to
L<Tangence::Meta::Event> instances.
=cut
method direct_events
{
$defined or croak "$name is not yet defined";
return { %events };
}
=head2 direct_properties
$properties = $class->direct_properties
Return the properties that this class directly defines (rather than inheriting
from superclasses) as a HASH reference mapping names to
L<Tangence::Meta::Property> instances.
=cut
method direct_properties
{
$defined or croak "$name is not yet defined";
return { %properties };
}
=head1 AGGREGATE ACCESSORS
The following accessors inspect the full inheritance tree of this class and
all its superclasses
=cut
=head2 superclasses
@superclasses = $class->superclasses
Return all the superclasses in a list of unique C<Tangence::Meta::Class>
references.
=cut
method superclasses
{
# This algorithm doesn't have to be particularly good, C3 or whatever.
# We're not really forming a search order, mearly uniq'ifying
my %seen;
return grep { !$seen{$_}++ } map { $_, $_->superclasses } @superclasses;
}
=head2 methods
$methods = $class->methods
Return all the methods available to this class as a HASH reference mapping
names to L<Tangence::Meta::Method> instances.
=cut
method methods
{
my %methods;
foreach ( $self, $self->superclasses ) {
my $m = $_->direct_methods;
$methods{$_} ||= $m->{$_} for keys %$m;
}
return \%methods;
}
=head2 method
$method = $class->method( $name )
Return the named method as a L<Tangence::Meta::Method> instance, or C<undef>
if no such method exists.
=cut
method method ( $name )
{
return $self->methods->{$name};
}
=head2 events
$events = $class->events
Return all the events available to this class as a HASH reference mapping
names to L<Tangence::Meta::Event> instances.
=cut
method events
{
my %events;
foreach ( $self, $self->superclasses ) {
my $e = $_->direct_events;
$events{$_} ||= $e->{$_} for keys %$e;
}
return \%events;
}
=head2 event
$event = $class->event( $name )
Return the named event as a L<Tangence::Meta::Event> instance, or C<undef> if
no such event exists.
=cut
method event ( $name )
{
return $self->events->{$name};
}
=head2 properties
$properties = $class->properties
Return all the properties available to this class as a HASH reference mapping
names to L<Tangence::Meta::Property> instances.
=cut
method properties
{
my %properties;
foreach ( $self, $self->superclasses ) {
my $p = $_->direct_properties;
$properties{$_} ||= $p->{$_} for keys %$p;
}
return \%properties;
}
=head2 property
$property = $class->property( $name )
Return the named property as a L<Tangence::Meta::Property> instance, or
C<undef> if no such property exists.
=cut
method property ( $name )
{
return $self->properties->{$name};
}
=head1 AUTHOR
Paul Evans <leonerd@leonerd.org.uk>
=cut
0x55AA;
|