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