annotate Lib/IMPL/Object/Abstract.pm @ 1:3b418b134d8c

ORM in progress
author Sergey
date Fri, 17 Jul 2009 13:30:46 +0400
parents
children 78cd38551534
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
1 package IMPL::Object::Abstract;
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
2 use strict;
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
3 use warnings;
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
4 package IMPL::Object;
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
5 use strict;
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
6
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
7 use base qw(IMPL::Class::Meta);
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
8
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
9 our $MemoryLeakProtection;
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
10 my $Cleanup = 0;
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
11 our $Debug;
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
12 our %leaked_objects;
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
13
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
14 my %cacheCTOR;
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
15
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
16
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
17 sub new {
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
18 my $class = shift;
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
19 my $self = bless {}, ref($class) || $class;
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
20
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
21 $self->$_(@_) foreach @{$cacheCTOR{ref $self} || cache_ctor(ref $self)};
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
22
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
23 $self;
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
24 }
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
25 my $t = 0;
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
26 sub cache_ctor {
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
27 my $class = shift;
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
28
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
29 no strict 'refs';
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
30 my @sequence;
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
31
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
32 my $refCTORS = *{"${class}::CTOR"}{HASH};
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
33
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
34 foreach my $super ( @{"${class}::ISA"} ) {
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
35 my $superSequence = $cacheCTOR{$super} || cache_ctor($super);
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
36
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
37 my $mapper = $refCTORS ? $refCTORS->{$super} : undef;
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
38 if (ref $mapper eq 'CODE') {
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
39 if ($mapper == *_pass_throgh_mapper{CODE}) {
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
40 push @sequence,@$superSequence;
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
41 } else {
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
42 push @sequence, sub {
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
43 my $this = shift;
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
44 $this->$_($mapper->(@_)) foreach @$superSequence;
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
45 };
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
46 }
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
47 } else {
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
48 warn "Unsupported mapper type, in '$class' for the super class '$super'" if $mapper;
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
49 push @sequence, sub {
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
50 my $this = shift;
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
51 $this->$_() foreach @$superSequence;
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
52 };
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
53 }
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
54 }
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
55
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
56 push @sequence, *{"${class}::CTOR"}{CODE} if *{"${class}::CTOR"}{CODE};
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
57
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
58 $cacheCTOR{$class} = \@sequence;
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
59 return \@sequence;
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
60 }
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
61
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
62 sub callCTOR {
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
63 my $self = shift;
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
64 my $class = ref $self;
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
65
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
66 $self->$_(@_) foreach @{$cacheCTOR{$class} || cache_ctor($class)};
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
67 }
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
68
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
69 sub surrogate {
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
70 bless {}, ref $_[0] || $_[0];
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
71 }
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
72
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
73 sub superCTOR {
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
74 my $this = shift;
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
75
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
76 warn "The mehod is deprecated, at " . caller;
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
77 }
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
78
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
79 sub toString {
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
80 my $self = shift;
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
81
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
82 return (ref $self || $self);
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
83 }
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
84
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
85 sub DESTROY {
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
86 if ($MemoryLeakProtection and $Cleanup) {
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
87 my $this = shift;
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
88 warn sprintf("Object leaks: %s of type %s %s",$this->can('ToString') ? $this->ToString : $this,ref $this,UNIVERSAL::can($this,'_dump') ? $this->_dump : '');
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
89 }
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
90 }
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
91
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
92 sub END {
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
93 $Cleanup = 1;
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
94 $MemoryLeakProtection = 0 unless $Debug;
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
95 }
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
96
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
97 sub _pass_throgh_mapper {
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
98 @_;
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
99 }
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
100
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
101 sub PassThroughArgs {
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
102 my $class = shift;
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
103 $class = ref $class || $class;
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
104 no strict 'refs';
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
105 no warnings 'once';
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
106 ${"${class}::CTOR"}{$_} = \&_pass_throgh_mapper foreach @{"${class}::ISA"};
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
107 }
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
108
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
109 package self;
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
110
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
111 our $AUTOLOAD;
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
112 sub AUTOLOAD {
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
113 goto &{caller(). substr $AUTOLOAD,4};
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
114 }
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
115
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
116 package supercall;
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
117
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
118 our $AUTOLOAD;
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
119 sub AUTOLOAD {
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
120 my $sub;
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
121 my $methodName = substr $AUTOLOAD,11;
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
122 no strict 'refs';
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
123 $sub = $_->can($methodName) and $sub->(@_) foreach @{caller().'::ISA'};
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
124 }
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
125
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
126 =pod
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
127 =h1 SYNOPSIS
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
128
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
129 package MyBaseObject;
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
130 use base qw(IMPL::Object::Abstract);
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
131
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
132 sub new {
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
133 # own implementation of the new opeator
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
134 }
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
135
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
136 sub surrogate {
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
137 # own implementation of the surrogate operator
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
138 }
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
139
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
140 =h1 DESCRIPTION
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
141
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
142 Реализация механизма вызова конструкторов и других вспомогательных вещей, кроме операторов
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
143 создания экземпляров.
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
144
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
145
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
146 1;
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
147
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
148
3b418b134d8c ORM in progress
Sergey
parents:
diff changeset
149 1;