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