line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# |
2
|
|
|
|
|
|
|
# Class::StructTemplate - Replacement class for Class::Struct |
3
|
|
|
|
|
|
|
# $Id$ |
4
|
|
|
|
|
|
|
# |
5
|
|
|
|
|
|
|
# Copyright (C) 2000 by Heiko Wundram. |
6
|
|
|
|
|
|
|
# All rights reserved. |
7
|
|
|
|
|
|
|
# |
8
|
|
|
|
|
|
|
# This program is free software; you can redistribute and/or modify it under the same terms as Perl itself. |
9
|
|
|
|
|
|
|
# |
10
|
|
|
|
|
|
|
# $Log$ |
11
|
|
|
|
|
|
|
# |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
package Class::StructTemplate; |
14
|
|
|
|
|
|
|
$Class::StructTemplate::VERSION = '0.01'; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
require Exporter; |
17
|
|
|
|
|
|
|
@Class::StructTemplate::ISA = qw(Exporter); |
18
|
|
|
|
|
|
|
@Class::StructTemplate::EXPORT = qw(attributes); |
19
|
|
|
|
|
|
|
|
20
|
1
|
|
|
1
|
|
6
|
use Carp; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
916
|
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub attributes |
23
|
|
|
|
|
|
|
{ |
24
|
0
|
0
|
|
0
|
0
|
|
my $pkg = ref $_[0] ? ${ shift() } : caller(); |
|
0
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
|
26
|
0
|
0
|
|
|
|
|
if( @_ < 1 ) |
27
|
|
|
|
|
|
|
{ |
28
|
0
|
|
|
|
|
|
confess "Need at least one attribute to assign to class!"; |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
0
|
|
|
|
|
|
my $attrib; |
32
|
|
|
|
|
|
|
|
33
|
0
|
|
|
|
|
|
${"${pkg}::_ATTRIBUTES"} = [@_]; |
|
0
|
|
|
|
|
|
|
34
|
0
|
|
|
|
|
|
${"${pkg}::_max_id"} = 1; |
|
0
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
|
36
|
0
|
0
|
|
|
|
|
_define_constructor($pkg) or confess("Couldn't create constructor for $pkg!"); |
37
|
|
|
|
|
|
|
|
38
|
0
|
|
|
|
|
|
foreach $attrib (@${"${pkg}::_ATTRIBUTES"},"_id") |
|
0
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
{ |
40
|
0
|
0
|
|
|
|
|
_define_accessor($pkg,$attrib) or confess("Couldn't create accessor for $pkg!"); |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
0
|
|
|
|
|
|
return 1; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub _define_constructor |
47
|
|
|
|
|
|
|
{ |
48
|
0
|
0
|
|
0
|
|
|
if( @_ != 1 ) |
49
|
|
|
|
|
|
|
{ |
50
|
0
|
|
|
|
|
|
confess "_define_constructor is only called with one argument!"; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
0
|
|
|
|
|
|
my ($pkg) = @_; |
54
|
|
|
|
|
|
|
|
55
|
0
|
|
|
|
|
|
my $accs = qq| |
56
|
|
|
|
|
|
|
package $pkg; |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub new |
59
|
|
|
|
|
|
|
{ |
60
|
|
|
|
|
|
|
my (\$class,\%attribs) = \@_; |
61
|
|
|
|
|
|
|
\$class = ref \$class ? ref \$class : \$class; |
62
|
|
|
|
|
|
|
bless( my \$self = {}, \$class ); |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
\$self->set_attributes(\%attribs,"_id"=>\$self->_next_id()); |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
\$self->{"_created"} = 0; |
67
|
|
|
|
|
|
|
\$self->{"_changed"} = 1; |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
return \$self; |
70
|
|
|
|
|
|
|
}|; |
71
|
|
|
|
|
|
|
|
72
|
0
|
|
|
|
|
|
eval $accs; |
73
|
|
|
|
|
|
|
|
74
|
0
|
0
|
|
|
|
|
croak $@ if $@; |
75
|
0
|
|
|
|
|
|
return !$@; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub _define_accessor |
79
|
|
|
|
|
|
|
{ |
80
|
0
|
0
|
|
0
|
|
|
if( @_ != 2 ) |
81
|
|
|
|
|
|
|
{ |
82
|
0
|
|
|
|
|
|
confess "_define_accessor is only called with two arguments!"; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
0
|
|
|
|
|
|
my ($pkg,$attrib) = @_; |
86
|
|
|
|
|
|
|
|
87
|
0
|
|
|
|
|
|
my $accs = qq| |
88
|
|
|
|
|
|
|
package $pkg; |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub $attrib |
91
|
|
|
|
|
|
|
{ |
92
|
|
|
|
|
|
|
my (\$class) = (shift); |
93
|
|
|
|
|
|
|
ref \$class or confess("Can only set attribute $attrib on an instance of class $pkg!"); |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
if( \@_ == 0 ) |
96
|
|
|
|
|
|
|
{ |
97
|
|
|
|
|
|
|
return \$class->{"$attrib"}; |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
elsif( \@_ == 1 ) |
100
|
|
|
|
|
|
|
{ |
101
|
|
|
|
|
|
|
\$class->{"$attrib"} = \$_[0]; |
102
|
|
|
|
|
|
|
\$class->{"_changed"} = 1; |
103
|
|
|
|
|
|
|
return \$class->{"$attrib"}; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
else |
106
|
|
|
|
|
|
|
{ |
107
|
|
|
|
|
|
|
confess("Can only retrieve or set class-data!"); |
108
|
|
|
|
|
|
|
return undef; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
}|; |
111
|
|
|
|
|
|
|
|
112
|
0
|
|
|
|
|
|
eval $accs; |
113
|
|
|
|
|
|
|
|
114
|
0
|
0
|
|
|
|
|
croak $@ if $@; |
115
|
0
|
|
|
|
|
|
return !$@; |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
sub _next_id |
119
|
|
|
|
|
|
|
{ |
120
|
0
|
0
|
|
0
|
|
|
if( @_ != 1 ) |
121
|
|
|
|
|
|
|
{ |
122
|
0
|
|
|
|
|
|
confess "_next_id only called with one argument!"; |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
0
|
|
|
|
|
|
my ($class) = @_; |
126
|
0
|
0
|
|
|
|
|
ref $class or confess "Can only get the next id on an instance of class $class!"; |
127
|
0
|
|
|
|
|
|
my $pkg = ref $class; |
128
|
|
|
|
|
|
|
|
129
|
0
|
|
|
|
|
|
return ${"${pkg}::_max_id"}++; |
|
0
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
sub set_attributes |
133
|
|
|
|
|
|
|
{ |
134
|
0
|
0
|
|
0
|
0
|
|
if( ( @_ - 1 ) % 2 != 0 ) |
135
|
|
|
|
|
|
|
{ |
136
|
0
|
|
|
|
|
|
confess "set_attributes can only be called with an even number of arguments!"; |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
0
|
|
|
|
|
|
my ($class,%attribs) = @_; |
140
|
0
|
0
|
|
|
|
|
ref $class or confess "set_attributes can only be called on an instance of class $class!"; |
141
|
0
|
|
|
|
|
|
my $pkg = ref $class; |
142
|
0
|
|
|
|
|
|
my $attrib; |
143
|
|
|
|
|
|
|
|
144
|
0
|
|
|
|
|
|
foreach $attrib (@${"${pkg}::_ATTRIBUTES"},"_id") |
|
0
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
{ |
146
|
0
|
0
|
|
|
|
|
if( exists $attribs{$attrib} ) |
|
|
0
|
|
|
|
|
|
147
|
|
|
|
|
|
|
{ |
148
|
0
|
|
|
|
|
|
$class->$attrib($attribs{$attrib}); |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
elsif( !$class->{"_allset"} ) |
151
|
|
|
|
|
|
|
{ |
152
|
0
|
|
|
|
|
|
$class->$attrib(undef); |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
|
156
|
0
|
|
|
|
|
|
$class->{"_allset"} = 1; |
157
|
0
|
|
|
|
|
|
$class->{"_changed"} = 1; |
158
|
|
|
|
|
|
|
|
159
|
0
|
|
|
|
|
|
return $class; |
160
|
|
|
|
|
|
|
} |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
sub get_attributes |
163
|
|
|
|
|
|
|
{ |
164
|
0
|
0
|
|
0
|
0
|
|
if( @_ != 1 ) |
165
|
|
|
|
|
|
|
{ |
166
|
0
|
|
|
|
|
|
confess "get_attributes is never called with any arguments!"; |
167
|
|
|
|
|
|
|
} |
168
|
|
|
|
|
|
|
|
169
|
0
|
|
|
|
|
|
my ($class) = @_; |
170
|
0
|
0
|
|
|
|
|
ref $class or confess "get_attributes can only be called on an instance of class $class!"; |
171
|
0
|
|
|
|
|
|
my $pkg = ref $class; |
172
|
0
|
|
|
|
|
|
my %ret_val = (); |
173
|
0
|
|
|
|
|
|
my $attrib; |
174
|
|
|
|
|
|
|
|
175
|
0
|
|
|
|
|
|
foreach $attrib (@${"${pkg}::_ATTRIBUTES"},"_id") |
|
0
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
{ |
177
|
0
|
|
|
|
|
|
$ret_val{$attrib} = $class->$attrib(); |
178
|
|
|
|
|
|
|
} |
179
|
|
|
|
|
|
|
|
180
|
0
|
|
|
|
|
|
return %ret_val; |
181
|
|
|
|
|
|
|
} |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
1; |