annotate Lib/IMPL/Object.pm @ 0:03e58a454b20

Создан репозитарий
author Sergey
date Tue, 14 Jul 2009 12:54:37 +0400
parents
children 78cd38551534
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
1 package IMPL::Object;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
2 use strict;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
3
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
4 use base qw(IMPL::Class::Meta);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
5
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
6 our $MemoryLeakProtection;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
7 my $Cleanup = 0;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
8 our $Debug;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
9 our %leaked_objects;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
10
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
11 my %cacheCTOR;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
12
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
13
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
14 sub new {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
15 my $class = shift;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
16 my $self = bless {}, ref($class) || $class;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
17
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
18 $self->$_(@_) foreach @{$cacheCTOR{ref $self} || cache_ctor(ref $self)};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
19
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
20 $self;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
21 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
22 my $t = 0;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
23 sub cache_ctor {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
24 my $class = shift;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
25
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
26 no strict 'refs';
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
27 my @sequence;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
28
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
29 my $refCTORS = *{"${class}::CTOR"}{HASH};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
30
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
31 foreach my $super ( @{"${class}::ISA"} ) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
32 my $superSequence = $cacheCTOR{$super} || cache_ctor($super);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
33
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
34 my $mapper = $refCTORS ? $refCTORS->{$super} : undef;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
35 if (ref $mapper eq 'CODE') {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
36 if ($mapper == *_pass_throgh_mapper{CODE}) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
37 push @sequence,@$superSequence;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
38 } else {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
39 push @sequence, sub {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
40 my $this = shift;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
41 $this->$_($mapper->(@_)) foreach @$superSequence;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
42 };
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
43 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
44 } else {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
45 warn "Unsupported mapper type, in '$class' for the super class '$super'" if $mapper;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
46 push @sequence, sub {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
47 my $this = shift;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
48 $this->$_() foreach @$superSequence;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
49 };
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
50 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
51 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
52
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
53 push @sequence, *{"${class}::CTOR"}{CODE} if *{"${class}::CTOR"}{CODE};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
54
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
55 $cacheCTOR{$class} = \@sequence;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
56 return \@sequence;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
57 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
58
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
59 sub callCTOR {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
60 my $self = shift;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
61 my $class = ref $self;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
62
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
63 $self->$_(@_) foreach @{$cacheCTOR{$class} || cache_ctor($class)};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
64 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
65
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
66 sub surrogate {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
67 bless {}, ref $_[0] || $_[0];
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
68 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
69
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
70 sub superCTOR {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
71 my $this = shift;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
72
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
73 warn "The mehod is deprecated, at " . caller;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
74 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
75
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
76 sub toString {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
77 my $self = shift;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
78
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
79 return (ref $self || $self);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
80 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
81
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
82 sub DESTROY {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
83 if ($MemoryLeakProtection and $Cleanup) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
84 my $this = shift;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
85 warn sprintf("Object leaks: %s of type %s %s",$this->can('ToString') ? $this->ToString : $this,ref $this,UNIVERSAL::can($this,'_dump') ? $this->_dump : '');
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
86 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
87 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
88
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
89 sub END {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
90 $Cleanup = 1;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
91 $MemoryLeakProtection = 0 unless $Debug;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
92 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
93
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
94 sub _pass_throgh_mapper {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
95 @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
96 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
97
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
98 sub PassThroughArgs {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
99 my $class = shift;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
100 $class = ref $class || $class;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
101 no strict 'refs';
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
102 no warnings 'once';
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
103 ${"${class}::CTOR"}{$_} = \&_pass_throgh_mapper foreach @{"${class}::ISA"};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
104 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
105
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
106 package self;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
107
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
108 our $AUTOLOAD;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
109 sub AUTOLOAD {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
110 goto &{caller(). substr $AUTOLOAD,4};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
111 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
112
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
113 package supercall;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
114
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
115 our $AUTOLOAD;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
116 sub AUTOLOAD {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
117 my $sub;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
118 my $methodName = substr $AUTOLOAD,11;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
119 no strict 'refs';
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
120 $sub = $_->can($methodName) and $sub->(@_) foreach @{caller().'::ISA'};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
121 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
122
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
123 =pod
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
124 =h1 SYNOPSIS
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
125
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
126 package Foo;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
127 use base qw(IMPL::Object);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
128
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
129 sub CTOR {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
130 my ($this,$arg) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
131 print "Foo: $arg\n";
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
132 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
133
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
134 package Bar;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
135 use base qw(IMPL::Object);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
136
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
137 sub CTOR {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
138 my ($this,$arg) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
139 print "Bar: $arg\n";
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
140 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
141
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
142 package Baz;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
143 use base qw(Foo Bar);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
144
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
145 our %CTOR = (
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
146 Foo => sub { my %args = @_; $args{Mazzi}; },
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
147 Bar => sub { my %args = @_; $args{Fugi}; }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
148 );
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
149
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
150 package Composite;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
151 use base qw(Baz Foo Bar);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
152
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
153 our %CTOR = (
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
154 Foo => undef,
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
155 Bar => undef
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
156 );
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
157
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
158 sub CTOR {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
159 my ($this,%args) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
160
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
161 print "Composite: $args{Text}\n";
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
162 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
163
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
164 package main;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
165
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
166 my $obj = new Composite(
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
167 Text => 'Hello World!',
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
168 Mazzi => 'Mazzi',
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
169 Fugi => 'Fugi'
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
170 );
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
171
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
172 # will print
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
173 #
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
174 # Foo: Mazzi
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
175 # Bar: Fugi
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
176 # Foo:
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
177 # Bar:
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
178 # Composite: Hello World!
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
179
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
180 =h1 Description
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
181 Áàçîâûé êëàññ äëÿ îáúåêòîâ. Ðåàëèçóåò ìíîæåñòâåííîå íàñëåäîâàíèå
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
182
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
183
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
184 =h1 Members
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
185 =cut
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
186
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
187 1;