Mercurial > pub > Impl
annotate Lib/IMPL/Class/Property/Base.pm @ 277:6585464c4664
sync (unstable)
author | sergey |
---|---|
date | Fri, 01 Feb 2013 16:37:59 +0400 |
parents | 8a5da17d7ef9 |
children |
rev | line source |
---|---|
59
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
1 package IMPL::Class::Property::Base; |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
2 use strict; |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
3 |
230 | 4 use IMPL::Const qw(:all); |
59
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
5 |
60
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
59
diff
changeset
|
6 sub factoryParams { qw($class $name $set $get $validator) }; |
59
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
7 |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
8 my %factoryCache; |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
9 |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
10 my $accessor_get_no = 'die new IMPL::Exception(\'The property is write only\',$name,$class) unless $get;'; |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
11 my $accessor_set_no = 'die new IMPL::Exception(\'The property is read only\',$name,$class) unless $set;'; |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
12 |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
13 my $custom_accessor_get = 'unshift @_, $this and goto &$get;'; |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
14 my $custom_accessor_set = 'unshift @_, $this and goto &$set;'; |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
15 |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
16 my $validator_code = '$this->$validator(@_);'; |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
17 |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
18 my %access_code = ( |
230 | 19 ACCESS_PUBLIC , "", |
20 ACCESS_PROTECTED, "die new IMPL::Exception('Can\\'t access the protected member',\$name,\$class,scalar caller) unless UNIVERSAL::isa(scalar caller,\$class);", | |
21 ACCESS_PRIVATE, "die new IMPL::Exception('Can\\'t access the private member',\$name,\$class,scalar caller) unless caller eq \$class;" | |
59
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
22 ); |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
23 |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
24 my $virtual_call = q( |
194 | 25 my $method = $this->can($name); |
59
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
26 return $this->$method(@_) unless $method == $accessor or caller->isa($class); |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
27 ); |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
28 |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
29 my $owner_check = "die new IMPL::Exception('Set accessor is restricted to the owner',\$name,\$class,scalar caller) unless caller eq \$class;"; |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
30 |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
31 sub GenerateAccessors { |
194 | 32 my ($self,$param,@params) = @_; |
33 | |
34 my %accessors; | |
35 | |
36 if (not ref $param) { | |
230 | 37 if ($param & PROP_LIST) { |
38 $accessors{get} = ($param & PROP_GET) ? $self->GenerateGetList(@params) : undef; | |
39 $accessors{set} = ($param & PROP_SET) ? $self->GenerateSetList(@params) : undef; | |
194 | 40 } else { |
230 | 41 $accessors{get} = ($param & PROP_GET) ? $self->GenerateGet(@params) : undef; |
42 $accessors{set} = ($param & PROP_SET) ? $self->GenerateSet(@params) : undef; | |
194 | 43 } |
230 | 44 $accessors{owner} = (($param & PROP_OWNERSET) == PROP_OWNERSET) ? $owner_check : ""; |
194 | 45 } elsif (UNIVERSAL::isa($param,'HASH')) { |
46 $accessors{get} = $param->{get} ? $custom_accessor_get : undef; | |
47 $accessors{set} = $param->{set} ? $custom_accessor_set : undef; | |
48 $accessors{owner} = ""; | |
49 } else { | |
50 die new IMPL::Exception('The unsupported accessor/mutators supplied',$param); | |
51 } | |
52 | |
53 return \%accessors; | |
59
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
54 } |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
55 |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
56 sub GenerateSet { |
194 | 57 die new IMPL::Exception("Standard accessors not supported",'Set'); |
59
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
58 } |
194 | 59 |
59
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
60 sub GenerateGet { |
194 | 61 die new IMPL::Exception("Standard accessors not supported",'Get'); |
59
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
62 } |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
63 |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
64 sub GenerateGetList { |
194 | 65 die new IMPL::Exception("Standard accessors not supported",'GetList'); |
59
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
66 } |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
67 |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
68 sub GenerateSetList { |
194 | 69 my ($self) = @_; |
70 die new IMPL::Exception("Standard accessors not supported",'SetList'); | |
59
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
71 } |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
72 |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
73 sub Make { |
194 | 74 my ($self,$propInfo) = @_; |
75 | |
76 my $key = $self->MakeFactoryKey($propInfo); | |
77 | |
78 my $factoryInfo = $factoryCache{$key}; | |
79 | |
80 unless ($factoryInfo) { | |
275 | 81 my $mutators = $self->GenerateAccessors($propInfo->mutators); |
194 | 82 $factoryInfo = { |
83 factory => $self->CreateFactory( | |
275 | 84 $access_code{ $propInfo->access }, |
85 $propInfo->attributes->{validator} ? $validator_code : "", | |
194 | 86 $mutators->{owner}, |
87 $mutators->{get} || $accessor_get_no, | |
88 $mutators->{set} || $accessor_set_no | |
89 ), | |
90 mutators => $mutators | |
91 }; | |
92 $factoryCache{$key} = $factoryInfo; | |
93 } | |
94 | |
95 { | |
96 no strict 'refs'; | |
275 | 97 *{ $propInfo->class.'::'.$propInfo->name } = $factoryInfo->{factory}->($self->RemapFactoryParams($propInfo)); |
194 | 98 } |
99 | |
100 my $mutators = $factoryInfo->{mutators}; | |
101 | |
102 $propInfo->canGet( $mutators->{get} ? 1 : 0 ); | |
103 $propInfo->canSet( $mutators->{set} ? 1 : 0 ); | |
104 $propInfo->ownerSet( $mutators->{owner} ); | |
105 | |
106 1; | |
59
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
107 } |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
108 |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
109 # extract from property info: class, name, get_accessor, set_accessor, validator |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
110 sub RemapFactoryParams { |
194 | 111 my ($self,$propInfo) = @_; |
112 | |
275 | 113 my $mutators = $propInfo->mutators; |
114 my $class = $propInfo->class; | |
115 my $validator = $propInfo->attributes->{validator}; | |
194 | 116 |
117 die new IMPL::Exception('Can\'t find the specified validator',$class,$validator) if $validator and ref $validator ne 'CODE' and not $class->can($validator); | |
59
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
118 |
194 | 119 return ( |
275 | 120 $propInfo->get(qw(class name)), |
194 | 121 (ref $mutators? |
122 ($mutators->{set},$mutators->{get}) | |
123 : | |
124 (undef,undef) | |
125 ), | |
126 $validator | |
127 ); | |
59
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
128 } |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
129 |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
130 sub MakeFactoryKey { |
194 | 131 my ($self,$propInfo) = @_; |
132 | |
275 | 133 my ($access,$mutators,$validator) = ($propInfo->get(qw(access mutators)),$propInfo->attributes->{validator}); |
194 | 134 |
135 my $implementor = ref $self || $self; | |
136 | |
137 return join ('', | |
138 $implementor, | |
139 $access, | |
140 $validator ? 'v' : 'n', | |
141 ref $mutators ? | |
142 ('c' , $mutators->{get} ? 1 : 0, $mutators->{set} ? 1 : 0) | |
143 : | |
144 ('s',$mutators) | |
145 ); | |
59
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
146 } |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
147 |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
148 sub CreateFactory { |
194 | 149 my ($self,$codeAccessCheck,$codeValidator,$codeOwnerCheck,$codeGet,$codeSet) = @_; |
150 | |
151 my $strParams = join(',',$self->factoryParams); | |
152 | |
153 my $factory = <<FACTORY; | |
154 | |
59
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
155 sub { |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
156 my ($strParams) = \@_; |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
157 my \$accessor; |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
158 \$accessor = sub { |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
159 my \$this = shift; |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
160 $codeAccessCheck |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
161 if (\@_) { |
194 | 162 $codeOwnerCheck |
163 $codeValidator | |
164 $codeSet | |
59
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
165 } else { |
194 | 166 $codeGet |
59
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
167 } |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
168 } |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
169 } |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
170 FACTORY |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
171 |
194 | 172 return ( eval $factory or die new IMPL::Exception("Syntax error due compiling the factory","$@") ); |
59
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
173 } |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
174 |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
175 1; |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
176 |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
177 __END__ |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
178 |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
179 =pod |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
180 |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
181 =head1 DESCRIPTION |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
182 |
180 | 183 Базовый класс для реализации свойств. |
59
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
184 |
180 | 185 По существу свойства состоят из двух методов для установки и получения значений. Также |
186 существует несколько вариантов доступа к свойству, и метод верификации значения. Еще | |
187 свойства могут быть виртуальными. | |
59
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
188 |
180 | 189 Для создания реализатора свойств достаточно унаследовать от этого класса и описать |
190 методы для генерации кода получения и установки значения. | |
80 | 191 |
192 =head1 MEMBERS | |
193 | |
194 =over | |
195 | |
196 =item C<Make($propertyInfo)> | |
197 | |
180 | 198 Создает свойство у класса, на основе C<$propertyInfo>, описывающего свойство. C<IMPL::Class::PropertyInfo>. |
80 | 199 |
200 =back | |
59
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
201 |
180 | 202 =cut |