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