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