Mercurial > pub > Impl
annotate Lib/IMPL/Object/Abstract.pm @ 280:c6d0f889ef87
+IMPL::declare now supports meta attributes
*bugfixes related to the typeof() operator
author | cin |
---|---|
date | Wed, 06 Feb 2013 02:15:48 +0400 |
parents | 8a5da17d7ef9 |
children | 97628101b765 |
rev | line source |
---|---|
49 | 1 package IMPL::Object::Abstract; |
2 use strict; | |
3 use warnings; | |
4 | |
166 | 5 use parent qw(IMPL::Class::Meta); |
49 | 6 |
7 our $MemoryLeakProtection; | |
8 my $Cleanup = 0; | |
9 | |
10 my %cacheCTOR; | |
11 | |
12 my $t = 0; | |
13 sub cache_ctor { | |
14 my $class = shift; | |
15 | |
16 no strict 'refs'; | |
17 my @sequence; | |
18 | |
19 my $refCTORS = *{"${class}::CTOR"}{HASH}; | |
20 | |
21 foreach my $super ( @{"${class}::ISA"} ) { | |
194 | 22 my $superSequence = $cacheCTOR{$super} || cache_ctor($super); |
23 | |
24 my $mapper = $refCTORS ? $refCTORS->{$super} : undef; | |
25 if (ref $mapper eq 'CODE') { | |
26 if ($mapper == *_pass_through_mapper{CODE}) { | |
27 push @sequence,@$superSequence; | |
28 } else { | |
29 push @sequence, sub { | |
30 my $this = shift; | |
31 $this->$_($mapper->(@_)) foreach @$superSequence; | |
32 } if @$superSequence; | |
33 } | |
197
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
194
diff
changeset
|
34 } elsif ($mapper and not ref $mapper and $mapper eq '@_') { |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
194
diff
changeset
|
35 push @sequence,@$superSequence; |
194 | 36 } else { |
37 warn "Unsupported mapper type, in '$class' for the super class '$super'" if $mapper; | |
38 push @sequence, sub { | |
39 my $this = shift; | |
40 $this->$_() foreach @$superSequence; | |
41 } if @$superSequence; | |
42 } | |
49 | 43 } |
44 | |
45 push @sequence, *{"${class}::CTOR"}{CODE} if *{"${class}::CTOR"}{CODE}; | |
46 | |
47 $cacheCTOR{$class} = \@sequence; | |
48 return \@sequence; | |
49 } | |
50 | |
90 | 51 sub dump_ctor { |
194 | 52 my ($self) = @_; |
53 $self = ref $self || $self; | |
54 | |
55 warn "dumping $self .ctor"; | |
56 warn "$_" foreach @{$cacheCTOR{$self}||[]}; | |
90 | 57 } |
58 | |
49 | 59 sub callCTOR { |
60 my $self = shift; | |
61 my $class = ref $self; | |
62 | |
63 $self->$_(@_) foreach @{$cacheCTOR{$class} || cache_ctor($class)}; | |
64 } | |
65 | |
273
ad93c9f4dd93
+Added support for destructors, (special method named DTOR)
sergey
parents:
197
diff
changeset
|
66 sub _init_dtor { |
ad93c9f4dd93
+Added support for destructors, (special method named DTOR)
sergey
parents:
197
diff
changeset
|
67 my ($class) = @_; |
ad93c9f4dd93
+Added support for destructors, (special method named DTOR)
sergey
parents:
197
diff
changeset
|
68 |
ad93c9f4dd93
+Added support for destructors, (special method named DTOR)
sergey
parents:
197
diff
changeset
|
69 no strict 'refs'; |
ad93c9f4dd93
+Added support for destructors, (special method named DTOR)
sergey
parents:
197
diff
changeset
|
70 |
ad93c9f4dd93
+Added support for destructors, (special method named DTOR)
sergey
parents:
197
diff
changeset
|
71 # avoid warnings for classes without destructors |
ad93c9f4dd93
+Added support for destructors, (special method named DTOR)
sergey
parents:
197
diff
changeset
|
72 no warnings 'once'; |
ad93c9f4dd93
+Added support for destructors, (special method named DTOR)
sergey
parents:
197
diff
changeset
|
73 |
ad93c9f4dd93
+Added support for destructors, (special method named DTOR)
sergey
parents:
197
diff
changeset
|
74 my @dtors; |
ad93c9f4dd93
+Added support for destructors, (special method named DTOR)
sergey
parents:
197
diff
changeset
|
75 |
ad93c9f4dd93
+Added support for destructors, (special method named DTOR)
sergey
parents:
197
diff
changeset
|
76 my @hierarchy = ($class); |
ad93c9f4dd93
+Added support for destructors, (special method named DTOR)
sergey
parents:
197
diff
changeset
|
77 my %visited; |
ad93c9f4dd93
+Added support for destructors, (special method named DTOR)
sergey
parents:
197
diff
changeset
|
78 |
ad93c9f4dd93
+Added support for destructors, (special method named DTOR)
sergey
parents:
197
diff
changeset
|
79 while(my $subclass = shift @hierarchy) { |
ad93c9f4dd93
+Added support for destructors, (special method named DTOR)
sergey
parents:
197
diff
changeset
|
80 if(*{"${subclass}::DTOR"}{CODE}) { |
ad93c9f4dd93
+Added support for destructors, (special method named DTOR)
sergey
parents:
197
diff
changeset
|
81 push @dtors, *{"${subclass}::DTOR"}{CODE}; |
ad93c9f4dd93
+Added support for destructors, (special method named DTOR)
sergey
parents:
197
diff
changeset
|
82 } |
ad93c9f4dd93
+Added support for destructors, (special method named DTOR)
sergey
parents:
197
diff
changeset
|
83 |
ad93c9f4dd93
+Added support for destructors, (special method named DTOR)
sergey
parents:
197
diff
changeset
|
84 push @hierarchy, @{"${subclass}::ISA"}; |
ad93c9f4dd93
+Added support for destructors, (special method named DTOR)
sergey
parents:
197
diff
changeset
|
85 } |
ad93c9f4dd93
+Added support for destructors, (special method named DTOR)
sergey
parents:
197
diff
changeset
|
86 |
ad93c9f4dd93
+Added support for destructors, (special method named DTOR)
sergey
parents:
197
diff
changeset
|
87 if (@dtors) { |
ad93c9f4dd93
+Added support for destructors, (special method named DTOR)
sergey
parents:
197
diff
changeset
|
88 |
ad93c9f4dd93
+Added support for destructors, (special method named DTOR)
sergey
parents:
197
diff
changeset
|
89 return *{"${class}::callDTOR"} = sub { |
ad93c9f4dd93
+Added support for destructors, (special method named DTOR)
sergey
parents:
197
diff
changeset
|
90 my ($self) = @_; |
ad93c9f4dd93
+Added support for destructors, (special method named DTOR)
sergey
parents:
197
diff
changeset
|
91 my $selfClass = ref $self; |
ad93c9f4dd93
+Added support for destructors, (special method named DTOR)
sergey
parents:
197
diff
changeset
|
92 if ($selfClass ne $class) { |
ad93c9f4dd93
+Added support for destructors, (special method named DTOR)
sergey
parents:
197
diff
changeset
|
93 goto &{$selfClass->_init_dtor()}; |
ad93c9f4dd93
+Added support for destructors, (special method named DTOR)
sergey
parents:
197
diff
changeset
|
94 } else { |
ad93c9f4dd93
+Added support for destructors, (special method named DTOR)
sergey
parents:
197
diff
changeset
|
95 map $_->($self), @dtors; |
ad93c9f4dd93
+Added support for destructors, (special method named DTOR)
sergey
parents:
197
diff
changeset
|
96 } |
ad93c9f4dd93
+Added support for destructors, (special method named DTOR)
sergey
parents:
197
diff
changeset
|
97 } |
ad93c9f4dd93
+Added support for destructors, (special method named DTOR)
sergey
parents:
197
diff
changeset
|
98 |
ad93c9f4dd93
+Added support for destructors, (special method named DTOR)
sergey
parents:
197
diff
changeset
|
99 } else { |
ad93c9f4dd93
+Added support for destructors, (special method named DTOR)
sergey
parents:
197
diff
changeset
|
100 return *{"${class}::callDTOR"} = sub { |
ad93c9f4dd93
+Added support for destructors, (special method named DTOR)
sergey
parents:
197
diff
changeset
|
101 my $self = ref $_[0]; |
ad93c9f4dd93
+Added support for destructors, (special method named DTOR)
sergey
parents:
197
diff
changeset
|
102 |
ad93c9f4dd93
+Added support for destructors, (special method named DTOR)
sergey
parents:
197
diff
changeset
|
103 goto &{$self->_init_dtor()} unless $self eq $class; |
ad93c9f4dd93
+Added support for destructors, (special method named DTOR)
sergey
parents:
197
diff
changeset
|
104 } |
ad93c9f4dd93
+Added support for destructors, (special method named DTOR)
sergey
parents:
197
diff
changeset
|
105 } |
ad93c9f4dd93
+Added support for destructors, (special method named DTOR)
sergey
parents:
197
diff
changeset
|
106 } |
ad93c9f4dd93
+Added support for destructors, (special method named DTOR)
sergey
parents:
197
diff
changeset
|
107 |
ad93c9f4dd93
+Added support for destructors, (special method named DTOR)
sergey
parents:
197
diff
changeset
|
108 __PACKAGE__->_init_dtor(); |
ad93c9f4dd93
+Added support for destructors, (special method named DTOR)
sergey
parents:
197
diff
changeset
|
109 |
49 | 110 sub toString { |
111 my $self = shift; | |
112 | |
113 return (ref $self || $self); | |
114 } | |
115 | |
280 | 116 sub _typeof { |
194 | 117 ref $_[0] || $_[0]; |
93 | 118 } |
119 | |
49 | 120 sub isDisposed { |
121 0; | |
122 } | |
123 | |
273
ad93c9f4dd93
+Added support for destructors, (special method named DTOR)
sergey
parents:
197
diff
changeset
|
124 sub DESTROY { |
ad93c9f4dd93
+Added support for destructors, (special method named DTOR)
sergey
parents:
197
diff
changeset
|
125 shift->callDTOR(); |
ad93c9f4dd93
+Added support for destructors, (special method named DTOR)
sergey
parents:
197
diff
changeset
|
126 } |
49 | 127 |
128 sub END { | |
129 $Cleanup = 1; | |
130 } | |
131 | |
174 | 132 sub _pass_through_mapper { |
49 | 133 @_; |
134 } | |
135 | |
136 sub PassArgs { | |
174 | 137 \&_pass_through_mapper; |
49 | 138 } |
139 | |
140 sub PassThroughArgs { | |
141 my $class = shift; | |
142 $class = ref $class || $class; | |
143 no strict 'refs'; | |
144 no warnings 'once'; | |
174 | 145 ${"${class}::CTOR"}{$_} = \&_pass_through_mapper foreach @{"${class}::ISA"}; |
49 | 146 } |
147 | |
148 package self; | |
149 | |
150 our $AUTOLOAD; | |
151 sub AUTOLOAD { | |
108 | 152 goto &{caller(). substr $AUTOLOAD,6}; |
49 | 153 } |
154 | |
155 package supercall; | |
156 | |
157 our $AUTOLOAD; | |
158 sub AUTOLOAD { | |
159 my $sub; | |
160 my $methodName = substr $AUTOLOAD,11; | |
161 no strict 'refs'; | |
162 $sub = $_->can($methodName) and $sub->(@_) foreach @{caller().'::ISA'}; | |
163 } | |
164 | |
63
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
49
diff
changeset
|
165 1; |
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
49
diff
changeset
|
166 |
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
49
diff
changeset
|
167 __END__ |
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
49
diff
changeset
|
168 |
49 | 169 =pod |
63
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
49
diff
changeset
|
170 =head1 SYNOPSIS |
49 | 171 |
172 package MyBaseObject; | |
166 | 173 use parent qw(IMPL::Object::Abstract); |
49 | 174 |
175 sub new { | |
176 # own implementation of the new opeator | |
177 } | |
178 | |
179 sub surrogate { | |
180 # own implementation of the surrogate operator | |
181 } | |
182 | |
63
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
49
diff
changeset
|
183 =head1 DESCRIPTION |
49 | 184 |
180 | 185 Реализация механизма вызова конструкторов и других вспомогательных вещей, кроме операторов |
186 создания экземпляров. | |
276
8a5da17d7ef9
*IMPL::Class refactoring property definition mechanism (incomplete).
sergey
parents:
273
diff
changeset
|
187 |
49 | 188 =cut |