annotate lib/IMPL/declare.pm @ 416:cc2cf8c0edc2 ref20150831

sync
author cin
date Thu, 29 Oct 2015 03:50:25 +0300
parents ee36115f6a34
children 3ed0c58e9da3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
1 package IMPL::declare;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
2 use strict;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
3
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
4 use Carp qw(carp);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
5 use IMPL::Class::PropertyInfo();
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
6 use IMPL::Const qw(:access);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
7 use IMPL::require();
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
8
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
9 BEGIN {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
10 *_require = *IMPL::require::_require;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
11 *_trace = *IMPL::require::_trace;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
12 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
13
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
14 sub import {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
15 my ( $self, $args ) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
16
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
17 return unless $args;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
18
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
19 die "A hash reference is required" unless ref $args eq 'HASH';
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
20
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
21 no strict 'refs';
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
22 no warnings 'once';
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
23
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
24 my $caller = caller;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
25
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
26 my $aliases = $args->{require} || {};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
27
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
28 $IMPL::require::PENDING{$caller} = 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
29 _trace("declare $caller");
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
30 $IMPL::require::level++;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
31
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
32 *{"${caller}::SELF"} = sub () {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
33 $caller;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
34 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
35
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
36 while ( my ( $alias, $class ) = each %$aliases ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
37 _trace("$alias => $class");
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
38 $IMPL::require::level++;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
39 my $c = _require($class);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
40
411
cin
parents: 407
diff changeset
41 *{"${caller}::$alias"} = sub () {
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
42 $c;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
43 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
44 $IMPL::require::level--;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
45 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
46
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
47 my $base = $args->{base} || {};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
48
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
49 my %ctor;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
50 my @isa;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
51
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
52 if ( ref $base eq 'ARRAY' ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
53 carp "Odd elements number in require"
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
54 unless scalar(@$base) % 2 == 0;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
55 while ( my ( $class, $mapper ) = splice @$base, 0, 2 ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
56 _trace("parent $class");
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
57 $IMPL::require::level++;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
58 $class = $aliases->{$class} || _require($class);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
59 $IMPL::require::level--;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
60
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
61 push @isa, $class;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
62 $ctor{$class} = $mapper;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
63 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
64 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
65 elsif ( ref $base eq 'HASH' ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
66 while ( my ( $class, $mapper ) = each %$base ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
67 _trace("parent $class");
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
68 $IMPL::require::level++;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
69 $class = $aliases->{$class} || _require($class);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
70 $IMPL::require::level--;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
71
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
72 push @isa, $class;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
73 $ctor{$class} = $mapper;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
74 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
75 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
76
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
77 %{"${caller}::CTOR"} = %ctor;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
78 push @{"${caller}::ISA"}, @isa;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
79
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
80 if ( ref( $args->{meta} ) eq 'ARRAY' ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
81 $caller->SetMeta($_) foreach @{ $args->{meta} };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
82 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
83
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
84 my $props = $args->{props} || [];
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
85
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
86 if ( $props eq 'HASH' ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
87 $props = [%$props];
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
88 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
89
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
90 die "A hash or an array reference is required in the properties list"
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
91 unless ref $props eq 'ARRAY';
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
92
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
93 carp "Odd elements number in properties declaration of $caller"
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
94 unless scalar(@$props) % 2 == 0;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
95
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
96 if (@$props) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
97 $self->_implementProps( $props, $caller );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
98 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
99
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
100 if ( $args->{_implement} ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
101 $self->_implementProps( $caller->abstractProps, $caller );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
102 $caller->abstractProps( [] );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
103 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
104
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
105 $IMPL::require::level--;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
106 delete $IMPL::require::PENDING{$caller};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
107 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
108
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
109 sub _implementProps {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
110 my ( $self, $props, $caller ) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
111
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
112 for ( my $i = 0 ; $i < @$props - 1 ; $i = $i + 2 ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
113 my ( $prop, $spec ) = @{$props}[ $i, $i + 1 ];
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
114
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
115 $caller->ClassPropertyImplementor->Implement(
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
116 $spec,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
117 {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
118 name => $prop,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
119 class => $caller,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
120 access => $prop =~ /^_/
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
121 ? ACCESS_PRIVATE
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
122 : ACCESS_PUBLIC
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
123 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
124 );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
125 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
126 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
127
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
128 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
129
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
130 __END__
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
131
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
132 =pod
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
133
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
134 =head1 NAME
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
135
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
136 C<IMPL::declare> - описывает класс
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
137
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
138 =head1 SYNOPSIS
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
139
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
140 =begin code
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
141
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
142 package My::Bar;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
143
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
144 use IMPL::declare {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
145 require => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
146 TFoo => 'My::Foo',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
147 TBox => 'My::Box'
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
148 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
149 base => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
150 TFoo => '@_',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
151 'IMPL::Object' => undef,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
152 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
153 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
154
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
155 sub CreateBox {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
156 my ($this) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
157 return TBox->new($this);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
158 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
159
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
160 =end code
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
161
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
162 Специальная ситрока C<@_> означает передачу параметров конструктора текущего класса конструктору
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
163 базового класса без изменений.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
164
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
165 =head1 DESCRIPTION
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
166
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
167 Описывает текущий пакет(модуль) как класс. В качестве параметра получает ссылку на хеш,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
168 в которой храняться метаданные для объявления класса.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
169
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
170 =head1 METADATA
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
171
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
172 =head2 C<require>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
173
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
174 Содержит ссылку на хеш с синонимами модулей, которые будут доступны в текушем модуле,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
175 аналогично использованию C<IMPL::require>. Однако, если модуль не требует загрузки при
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
176 помощи C<require> нужно использовать префикс C<'-'> в его имени
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
177
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
178 =begin code
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
179
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
180 {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
181 require => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
182 TObject => 'IMPL::Object', # will be loaded with require
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
183 TFoo => '-My:App::Data::Foo' # will not use 'require' to load module
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
184 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
185 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
186
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
187 =end code
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
188
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
189 =head2 C<base>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
190
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
191 Обисывает базове классы для текущего класса. Если данный параметр - ссылка массив, то
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
192 этот массив будет превращен в массив C<@ISA>. Если данный параметр - ссылка на хеш, то
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
193 его ключи опичавют список базовых классов, а значения - преобразование параметров для
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
194 вызова базовых конструкторов.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
195
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
196 В качестве имен базовых классов могут быть как полные имена модулей, так и назначенные
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
197 ранее псевдонимы. Использование префикса C<'-'> перед B<полным именем модуля> означает,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
198 что модуль не требуется загружать, в случае с псевдонимами, префикс C<'-'> уже был указан
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
199 при их объявлении.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
200
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
201 =begin code
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
202
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
203 {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
204 require => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
205 TFoo => '-My:App::Data::Foo' # will not use 'require' to load module
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
206 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
207 base => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
208 TFoo => '@_', # pass parameters unchanged
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
209 'My::Base::Class' => sub { name => $_[0], data => $_[1] }, # remap parameters
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
210 '-My::Extentions' => undef, # do not pass any parameters
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
211 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
212 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
213
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
214 =end code
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
215
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
216 =cut