comparison lib/IMPL/lang.pm @ 407:c6e90e02dd17 ref20150831

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