annotate lib/IMPL/Object/Abstract.pm @ 408:5c80e33f1218 ref20150831

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