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