| 1 | # You may distribute under the terms of either the GNU General Public License | ||||||||||||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||||||||||||
| 3 | # | ||||||||||||
| 4 | # (C) Paul Evans, 2010-2024 -- leonerd@leonerd.org.uk | ||||||||||||
| 5 | |||||||||||||
| 6 | 12 | ▶ | use v5.26; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 12 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 12 | ▶ | use Object::Pad 0.800 ':experimental(adjust_params)'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | package Tangence::Registry 0.33; | ||||||||||||
| 11 | 10 | ▶ | class Tangence::Registry :isa(Tangence::Object); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | |||||||||||||
| 13 | 12 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | |||||||||||||
| 15 | 12 | ▶ | use Tangence::Constants; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 16 | 12 | ▶ | use Tangence::Class; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 17 | 12 | ▶ | use Tangence::Property; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 18 | 12 | ▶ | use Tangence::Struct; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 19 | 12 | ▶ | use Tangence::Type; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 20 | |||||||||||||
| 21 | 12 | ▶ | use Tangence::Compiler::Parser; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 22 | |||||||||||||
| 23 | 12 | ▶ | use Scalar::Util qw( weaken ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 24 | |||||||||||||
| 25 | 12 | Tangence::Class->declare( | |||||||||||
| 26 | __PACKAGE__, | ||||||||||||
| 27 | |||||||||||||
| 28 | methods => { | ||||||||||||
| 29 | get_by_id => { | ||||||||||||
| 30 | args => [ [ id => 'int' ] ], | ||||||||||||
| 31 | ret => 'obj', | ||||||||||||
| 32 | }, | ||||||||||||
| 33 | }, | ||||||||||||
| 34 | |||||||||||||
| 35 | events => { | ||||||||||||
| 36 | object_constructed => { | ||||||||||||
| 37 | args => [ [ id => 'int' ] ], | ||||||||||||
| 38 | }, | ||||||||||||
| 39 | object_destroyed => { | ||||||||||||
| 40 | args => [ [ id => 'int' ] ], | ||||||||||||
| 41 | }, | ||||||||||||
| 42 | }, | ||||||||||||
| 43 | |||||||||||||
| 44 | props => { | ||||||||||||
| 45 | objects => { | ||||||||||||
| 46 | dim => DIM_HASH, | ||||||||||||
| 47 | type => 'str', | ||||||||||||
| 48 | } | ||||||||||||
| 49 | }, | ||||||||||||
| 50 | ); | ||||||||||||
| 51 | |||||||||||||
| 52 | =head1 NAME | ||||||||||||
| 53 | |||||||||||||
| 54 | C<Tangence::Registry> - object manager for a C<Tangence> server | ||||||||||||
| 55 | |||||||||||||
| 56 | =head1 DESCRIPTION | ||||||||||||
| 57 | |||||||||||||
| 58 | This subclass of L<Tangence::Object> acts as a container for all the exposed | ||||||||||||
| 59 | objects in a L<Tangence> server. The registry is used to create exposed | ||||||||||||
| 60 | objects, and manages their lifetime. It maintains a reference to all the | ||||||||||||
| 61 | objects it creates, so it can dispatch incoming messages from clients to them. | ||||||||||||
| 62 | |||||||||||||
| 63 | =cut | ||||||||||||
| 64 | |||||||||||||
| 65 | =head1 CONSTRUCTOR | ||||||||||||
| 66 | |||||||||||||
| 67 | =cut | ||||||||||||
| 68 | |||||||||||||
| 69 | =head2 new | ||||||||||||
| 70 | |||||||||||||
| 71 | $registry = Tangence::Registry->new; | ||||||||||||
| 72 | |||||||||||||
| 73 | Returns a new instance of a C<Tangence::Registry> object. An entire server | ||||||||||||
| 74 | requires one registry object; it will be shared among all the client | ||||||||||||
| 75 | connections to that server. | ||||||||||||
| 76 | |||||||||||||
| 77 | =cut | ||||||||||||
| 78 | |||||||||||||
| 79 | 11 | ▶ | sub BUILDARGS ( $class, %args ) | ||||||||||
|
Subroutine
BUILDARGS: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 80 | { | ||||||||||||
| 81 | return ( | ||||||||||||
| 82 | 11 | id => 0, | |||||||||||
| 83 | registry => "BOOTSTRAP", | ||||||||||||
| 84 | meta => Tangence::Class->for_perlname( $class ), | ||||||||||||
| 85 | %args, | ||||||||||||
| 86 | ); | ||||||||||||
| 87 | } | ||||||||||||
| 88 | |||||||||||||
| 89 | field $_nextid = 1; | ||||||||||||
| 90 | 12 | field @_freeids; | |||||||||||
| 91 | 12 | field %_objects; | |||||||||||
| 92 | |||||||||||||
| 93 | ADJUST :params ( | ||||||||||||
| 94 | :$tanfile | ||||||||||||
| 95 | ) { | ||||||||||||
| 96 | my $id = 0; | ||||||||||||
| 97 | weaken( $self->{registry} = $self ); | ||||||||||||
| 98 | |||||||||||||
| 99 | %_objects = ( $id => $self ); | ||||||||||||
| 100 | weaken( $_objects{$id} ); | ||||||||||||
| 101 | $self->add_prop_objects( $id => $self->describe ); | ||||||||||||
| 102 | |||||||||||||
| 103 | $self->load_tanfile( $tanfile ); | ||||||||||||
| 104 | } | ||||||||||||
| 105 | |||||||||||||
| 106 | 12 | =head1 METHODS | |||||||||||
| 107 | |||||||||||||
| 108 | =cut | ||||||||||||
| 109 | |||||||||||||
| 110 | =head2 get_by_id | ||||||||||||
| 111 | |||||||||||||
| 112 | $obj = $registry->get_by_id( $id ); | ||||||||||||
| 113 | |||||||||||||
| 114 | Returns the object with the given object ID. | ||||||||||||
| 115 | |||||||||||||
| 116 | This method is exposed to clients. | ||||||||||||
| 117 | |||||||||||||
| 118 | =cut | ||||||||||||
| 119 | |||||||||||||
| 120 | 59 | ▶ | method get_by_id ( $id ) | ||||||||||
|
Subroutine
get_by_id: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 121 | { | ||||||||||||
| 122 | 59 | return $_objects{$id}; | |||||||||||
| 123 | 12 | } | |||||||||||
| 124 | |||||||||||||
| 125 | 0 | ▶ | method method_get_by_id ( $ctx, $id ) | ||||||||||
|
Subroutine
method_get_by_id: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 126 | { | ||||||||||||
| 127 | 0 | return $self->get_by_id( $id ); | |||||||||||
| 128 | 12 | } | |||||||||||
| 129 | |||||||||||||
| 130 | =head2 construct | ||||||||||||
| 131 | |||||||||||||
| 132 | $obj = $registry->construct( $type, @args ); | ||||||||||||
| 133 | |||||||||||||
| 134 | Constructs a new exposed object of the given type, and returns it. Any | ||||||||||||
| 135 | additional arguments are passed to the object's constructor. | ||||||||||||
| 136 | |||||||||||||
| 137 | =cut | ||||||||||||
| 138 | |||||||||||||
| 139 | 14 | ▶ | method construct ( $type, @args ) | ||||||||||
|
Subroutine
construct: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||
| 140 | { | ||||||||||||
| 141 | 14 | ▶ | my $id = shift @_freeids // ( $_nextid++ ); | ||||||||||
|
Condition50%1/2
shift @_freeids // $_nextid++
MC/DC0%0/1
shift @_freeids // $_nextid++
shift @_freeids
Truth table50%1/2
A
A= shift @_freeids
| |||||||||||||
| 142 | |||||||||||||
| 143 | 14 | ▶ | Tangence::Class->for_perlname( $type ) or | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 144 | croak "Registry cannot construct a '$type' as no class definition exists"; | ||||||||||||
| 145 | |||||||||||||
| 146 | 14 | ▶ | eval { $type->can( "new" ) } or | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 147 | croak "Registry cannot construct a '$type' as it has no ->new() method"; | ||||||||||||
| 148 | |||||||||||||
| 149 | 14 | my $obj = $type->new( | |||||||||||
| 150 | registry => $self, | ||||||||||||
| 151 | id => $id, | ||||||||||||
| 152 | @args | ||||||||||||
| 153 | ); | ||||||||||||
| 154 | |||||||||||||
| 155 | 14 | $self->fire_event( "object_constructed", $id ); | |||||||||||
| 156 | |||||||||||||
| 157 | 14 | weaken( $_objects{$id} = $obj ); | |||||||||||
| 158 | 14 | $self->add_prop_objects( $id => $obj->describe ); | |||||||||||
| 159 | |||||||||||||
| 160 | 14 | return $obj; | |||||||||||
| 161 | 12 | } | |||||||||||
| 162 | |||||||||||||
| 163 | 2 | ▶ | method destroy_object ( $obj ) | ||||||||||
|
Subroutine
destroy_object: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 164 | { | ||||||||||||
| 165 | 2 | my $id = $obj->id; | |||||||||||
| 166 | |||||||||||||
| 167 | 2 | ▶ | exists $_objects{$id} or croak "Cannot destroy ID $id - does not exist"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 168 | |||||||||||||
| 169 | 2 | $self->del_prop_objects( $id ); | |||||||||||
| 170 | |||||||||||||
| 171 | 2 | $self->fire_event( "object_destroyed", $id ); | |||||||||||
| 172 | |||||||||||||
| 173 | 2 | push @_freeids, $id; # Recycle the ID | |||||||||||
| 174 | 12 | } | |||||||||||
| 175 | |||||||||||||
| 176 | =head2 load_tanfile | ||||||||||||
| 177 | |||||||||||||
| 178 | $registry->load_tanfile( $tanfile ); | ||||||||||||
| 179 | |||||||||||||
| 180 | Loads additional Tangence class and struct definitions from the given F<.tan> | ||||||||||||
| 181 | file. | ||||||||||||
| 182 | |||||||||||||
| 183 | =cut | ||||||||||||
| 184 | |||||||||||||
| 185 | 11 | ▶ | method load_tanfile ( $tanfile ) | ||||||||||
|
Subroutine
load_tanfile: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 186 | { | ||||||||||||
| 187 | # Merely constructing this has the side-effect of declaring all the classes | ||||||||||||
| 188 | 11 | Tangence::Registry::Parser->new->from_file( $tanfile ); | |||||||||||
| 189 | 12 | } | |||||||||||
| 190 | |||||||||||||
| 191 | class Tangence::Registry::Parser :isa(Tangence::Compiler::Parser) | ||||||||||||
| 192 | { | ||||||||||||
| 193 | method make_class | ||||||||||||
| 194 | 12 | ▶ | { | ||||||||||
|
Subroutine
make_class: called CC=1 SCAR=0.0
| |||||||||||||
| 195 | 12 | return Tangence::Class->make( @_ ); | |||||||||||
| 196 | 12 | } | |||||||||||
| 197 | |||||||||||||
| 198 | method make_struct | ||||||||||||
| 199 | 10 | ▶ | { | ||||||||||
|
Subroutine
make_struct: called CC=1 SCAR=0.0
| |||||||||||||
| 200 | 10 | return Tangence::Struct->make( @_ ); | |||||||||||
| 201 | 12 | } | |||||||||||
| 202 | |||||||||||||
| 203 | method make_property | ||||||||||||
| 204 | 82 | ▶ | { | ||||||||||
|
Subroutine
make_property: called CC=1 SCAR=0.0
| |||||||||||||
| 205 | 82 | return Tangence::Property->new( @_ ); | |||||||||||
| 206 | 12 | } | |||||||||||
| 207 | |||||||||||||
| 208 | method make_type | ||||||||||||
| 209 | 195 | ▶ | { | ||||||||||
|
Subroutine
make_type: called CC=1 SCAR=0.0
| |||||||||||||
| 210 | 195 | return Tangence::Type->make( @_ ); | |||||||||||
| 211 | 12 | } | |||||||||||
| 212 | } | ||||||||||||
| 213 | |||||||||||||
| 214 | 12 | =head1 AUTHOR | |||||||||||
| 215 | |||||||||||||
| 216 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||||
| 217 | |||||||||||||
| 218 | =cut | ||||||||||||
| 219 | |||||||||||||
| 220 | 12 | 0x55AA; | |||||||||||