Thread/Shared/Array.pm untested

filter: Statementstmt 0.0% 0 / 72 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 16 totaltotal 0.0% 0 / 126 CC 8 SCAR 15.5
CC
8
Cov
0%
STORESIZE
12.0
TIEARRAY
6.0
FETCH
6.0
CRAP 4.7
1  
2 package Thread::Shared::Array;
3  
4 use threads::shared '1.02', qw/ share is_shared /;
5  
6 use Thread::Shared;
7 use strict;
8  
9 sub TIEARRAY
10 {
11 my $class = shift;
12 my $value = shift;
13  
14 if ( not defined $value or not is_shared $value )
15 {
16 $value = &share([]);
17 }
18  
19 my $self = {
20 array => $value
21 };
22  
23 bless $self, $class;
24 return $self;
25 }
26  
27 sub FETCH
28 {
29 my ($self, $index) = @_;
30 my $value = $self->{array}->[$index];
31 if ( ref($value) )
32 {
33 $value = Thread::Shared::wrap($value);
34 }
35 return $value;
36 }
37  
38 sub STORE
39 {
40 my ($self, $index, $value) = @_;
41 $self->{array}->[$index] = Thread::Shared::make_shared($value);
42 }
43  
44 sub FETCHSIZE
45 {
46 my $self = shift;
47 return scalar @{$self->{array}};
48 }
49  
50 sub STORESIZE
51 {
52 my ($self, $count) = shift;
53  
54 if ( $count > $self->FETCHSIZE() )
55 {
56 foreach ( $count - $self->FETCHSIZE() .. $count )
57 {
58 $self->STORE($_, undef);
59 }
60 }
61 elsif ( $count < $self->FETCHSIZE() )
62 {
63 foreach ( 0 .. $self->FETCHSIZE() - $count - 2 )
64 {
65 $self->POP();
66 }
67 }
68 }
69  
70 sub EXTEND
71 {
72 my ($self, $count) = shift;
73 $self->STORESIZE($count);
74 }
75  
76 sub EXISTS
77 {
78 my ($self, $index) = shift;
79 return exists $self->{array}->[$index];
80 }
81  
82 sub DELETE
83 {
84 my ($self, $index) = shift;
85 delete $self->{array}->[$index];
86 }
87  
88 sub CLEAR
89 {
90 my $self = shift;
91 $self->{array} = &share([]);
92 }
93  
94 sub PUSH
95 {
96 my $self = shift;
97 foreach my $item ( @_ )
98 {
99 push @{$self->{array}}, Thread::Shared::make_sharable($item);
100 }
101 return $self->FETCHSIZE();
102 }
103  
104 sub POP
105 {
106 my $self = shift;
107 my $value = pop @{$self->{array}};
108 if ( ref($value) )
109 {
110 $value = Thread::Shared::wrap($value);
111 }
112 return $value;
113 }
114  
115 sub SHIFT
116 {
117 my $self = shift;
118 my $value = shift @{$self->{array}};
119 if ( ref($value) )
120 {
121 $value = Thread::Shared::wrap($value);
122 }
123 return $value;
124 }
125  
126 sub UNSHIFT
127 {
128 my $self = shift;
129 my $value = unshift @{$self->{array}};
130 if ( ref($value) )
131 {
132 $value = Thread::Shared::wrap($value);
133 }
134 return $value;
135 }
136  
137 sub SPLICE
138 {
139 my ($self, $offset, $length) = (shift, shift, shift);
140 my @value = splice @{$self->{array}}, $offset, $length, @_;
141 my $ref = Thread::Shared::wrap(\@value);
142 return @$ref;
143 }
144  
145 sub UNTIE
146 {
147 my $self = shift;
148 # TODO: me!
149 }
150  
151 sub DESTROY
152 {
153 my $self = shift;
154 # TODO: me!
155 }
156  
157 1;
158