164
|
1 package IMPL::lang;
|
|
2 use strict;
|
|
3 use warnings;
|
|
4
|
165
|
5 use parent qw(Exporter);
|
164
|
6 use IMPL::_core::version;
|
173
|
7 use IMPL::clone qw(clone);
|
164
|
8
|
167
|
9 require IMPL::Class::PropertyInfo;
|
164
|
10
|
167
|
11 our @EXPORT = qw(&is);
|
|
12 our %EXPORT_TAGS = (
|
|
13 base => [
|
|
14 qw(
|
|
15 &is
|
173
|
16 &clone
|
167
|
17 )
|
|
18 ],
|
|
19 constants => [
|
|
20 qw(
|
|
21 &ACCESS_PUBLIC
|
|
22 &ACCESS_PROTECTED
|
|
23 &ACCESS_PRIVATE
|
|
24 &PROP_GET
|
|
25 &PROP_SET
|
|
26 &PROP_OWNERSET
|
|
27 &PROP_LIST
|
|
28 &PROP_ALL
|
|
29 )
|
|
30 ],
|
|
31
|
|
32 declare => [
|
|
33 qw(
|
|
34 &public
|
|
35 &protected
|
|
36 &private
|
|
37 &virtual
|
|
38 &property
|
|
39 &static
|
|
40 &property
|
177
|
41 &ctor
|
|
42 &base
|
167
|
43 )
|
|
44 ],
|
|
45 compare => [
|
|
46 qw(
|
|
47 &equals
|
|
48 &equals_s
|
168
|
49 &hashCompare
|
|
50 )
|
|
51 ],
|
|
52 hash => [
|
|
53 qw(
|
|
54 &hashApply
|
|
55 &hashMerge
|
|
56 &hashDiff
|
|
57 &hashCompare
|
176
|
58 &hashParse
|
167
|
59 )
|
|
60 ]
|
|
61 );
|
|
62
|
|
63 our @EXPORT_OK = keys %{ { map (($_,1) , map (@{$_}, values %EXPORT_TAGS) ) } };
|
|
64
|
|
65 use constant {
|
|
66 ACCESS_PUBLIC => 1,
|
|
67 ACCESS_PROTECTED => 2,
|
|
68 ACCESS_PRIVATE => 3,
|
|
69 PROP_GET => 1,
|
|
70 PROP_SET => 2,
|
|
71 PROP_OWNERSET => 10,
|
|
72 PROP_LIST => 4,
|
|
73 PROP_ALL => 3
|
|
74 };
|
164
|
75
|
|
76 sub is($$) {
|
167
|
77 eval { $_[0]->isa( $_[1] ) };
|
|
78 }
|
|
79
|
|
80 sub virtual($) {
|
|
81 $_[0]->Virtual(1);
|
|
82 $_[0];
|
|
83 }
|
|
84
|
|
85 sub public($) {
|
|
86 $_[0]->Access(ACCESS_PUBLIC);
|
|
87 $_[0]->Implement;
|
|
88 $_[0];
|
|
89 }
|
|
90
|
|
91 sub private($) {
|
|
92 $_[0]->Access(ACCESS_PRIVATE);
|
|
93 $_[0]->Implement;
|
|
94 $_[0];
|
|
95 }
|
|
96
|
|
97 sub protected($) {
|
|
98 $_[0]->Access(ACCESS_PROTECTED);
|
|
99 $_[0]->Implement;
|
|
100 $_[0];
|
164
|
101 }
|
|
102
|
167
|
103 sub property($$;$) {
|
|
104 my ( $propName, $mutators, $attributes ) = @_;
|
|
105 my $Info = new IMPL::Class::PropertyInfo(
|
|
106 {
|
|
107 Name => $propName,
|
|
108 Mutators => $mutators,
|
|
109 Class => scalar(caller),
|
|
110 Attributes => $attributes
|
|
111 }
|
|
112 );
|
|
113 return $Info;
|
|
114 }
|
|
115
|
|
116 sub static($$) {
|
|
117 my ( $name, $value ) = @_;
|
|
118 my $class = caller;
|
|
119 $class->static_accessor( $name, $value );
|
|
120 }
|
|
121
|
177
|
122 sub ctor(&;$) {
|
|
123 my ( $code, $base ) = @_;
|
|
124 no strict 'refs';
|
|
125 my $class = caller;
|
|
126
|
|
127 if ($code) {
|
|
128 *{"${class}::CTOR"} = $code;
|
|
129 }
|
|
130
|
|
131 if (ref $base eq 'HASH') {
|
|
132 %{"${class}::CTOR"} = %$base;
|
|
133 }
|
|
134 }
|
|
135
|
|
136 sub base($) {
|
|
137 return shift;
|
|
138 }
|
|
139
|
167
|
140 sub equals {
|
|
141 if (defined $_[0]) {
|
|
142 return 0 if (not defined $_[1]);
|
|
143
|
|
144 return $_[0] == $_[1];
|
|
145 } else {
|
|
146 return 0 if defined $_[1];
|
|
147
|
|
148 return 1;
|
|
149 }
|
|
150 }
|
|
151
|
|
152 sub equals_s {
|
|
153 if (defined $_[0]) {
|
|
154 return 0 if (not defined $_[1]);
|
|
155
|
|
156 return $_[0] eq $_[1];
|
|
157 } else {
|
|
158 return 0 if defined $_[1];
|
|
159
|
|
160 return 1;
|
|
161 }
|
|
162 }
|
|
163
|
168
|
164 sub hashDiff {
|
|
165 my ($src,$dst) = @_;
|
|
166
|
169
|
167 $dst = $dst ? { %$dst } : {} ;
|
|
168 $src ||= {};
|
168
|
169
|
|
170 my %result;
|
|
171
|
|
172 foreach my $key ( keys %$src ) {
|
|
173 if (exists $dst->{$key}) {
|
|
174 $result{"+$key"} = $dst->{$key} unless equals_s($dst->{$key}, $src->{$key});
|
|
175 delete $dst->{$key};
|
|
176 } else {
|
|
177 $result{"-$key"} = 1;
|
|
178 }
|
|
179 }
|
|
180
|
|
181 $result{"+$_"} = $dst->{$_} foreach keys %$dst;
|
|
182
|
|
183 return \%result;
|
|
184 }
|
|
185
|
|
186 sub hashMerge {
|
|
187 return hashApply( { %{$_[0]} }, $_[1] );
|
|
188 }
|
|
189
|
|
190 sub hashApply {
|
|
191 my ($target,$diff) = @_;
|
|
192
|
|
193 while ( my ($key,$value) = each %$diff) {
|
|
194 $key =~ /^(\+|-)?(.*)$/;
|
|
195 my $op = $1 || '+';
|
|
196 $key = $2;
|
|
197
|
|
198 if ($op eq '-') {
|
|
199 delete $target->{$key};
|
|
200 } else {
|
|
201 $target->{$key} = $value;
|
|
202 }
|
|
203 }
|
|
204
|
|
205 return $target;
|
|
206 }
|
|
207
|
|
208 sub hashCompare {
|
|
209 my ($l,$r,$cmp) = @_;
|
|
210
|
|
211 $cmp ||= \&equals_s;
|
|
212
|
|
213 return 0 unless scalar keys %$l == scalar keys %$r;
|
|
214 &$cmp($l->{$_},$r->{$_}) || return 0 foreach keys %$l;
|
|
215
|
|
216 return 1;
|
|
217 }
|
|
218
|
174
|
219 sub hashParse {
|
|
220 my ($s,$p,$d) = @_;
|
|
221
|
|
222 $p = $p ? qr/$p/ : qr/\n+/;
|
|
223 $d = $d ? qr/$d/ : qr/\s*=\s*/;
|
|
224
|
|
225 return {
|
|
226 map split($d,$_,2), split($p,$s)
|
|
227 };
|
|
228 }
|
|
229
|
|
230 sub hashSave {
|
|
231
|
|
232 }
|
|
233
|
167
|
234 1;
|