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