File Coverage

blib/lib/KiokuDB/LiveObjects/Scope.pm
Criterion Covered Total %
statement 15 15 100.0
branch 3 4 75.0
condition n/a
subroutine 5 5 100.0
pod 2 3 66.6
total 25 27 92.5


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2              
3             package KiokuDB::LiveObjects::Scope;
4 22     22   95 use Moose;
  22         27  
  22         166  
5              
6 22     22   109035 use namespace::clean -except => 'meta';
  22         41  
  22         179  
7              
8             has objects => (
9             traits => [qw(Array)],
10             isa => "ArrayRef",
11             default => sub { [] },
12             clearer => "_clear_objects",
13             handles => {
14             push => "push",
15             objects => "elements",
16             clear => "clear",
17             },
18             );
19              
20             has parent => (
21             isa => __PACKAGE__,
22             is => "ro",
23             );
24              
25             has live_objects => (
26             isa => "KiokuDB::LiveObjects",
27             is => "ro",
28             clearer => "_clear_live_objects",
29             );
30              
31             sub DEMOLISH {
32 3482     3482 0 5701 my $self = shift;
33              
34             # consider possibilities of optimizing live object set removal at this
35             # point
36              
37             # problems can arise from an object outliving the scope it was loaded in:
38             # { my $outer = lookup(...); { my $inner = lookup(...); $outer->foo($inner) } }
39              
40 3482         12090 $self->remove;
41             }
42              
43             sub detach {
44 1     1 1 3 my $self = shift;
45              
46 1 50       55 if ( my $l = $self->live_objects ) {
47 1         6 $l->detach_scope($self);
48             }
49             }
50              
51             sub remove {
52 3483     3483 1 4413 my $self = shift;
53              
54 3483 100       100321 if ( my $l = $self->live_objects ) { # can be false under global destruction
55 3482         14040 $l->remove_scope($self);
56 3482         119681 $self->_clear_live_objects;
57             }
58             }
59              
60             __PACKAGE__->meta->make_immutable;
61              
62             __PACKAGE__
63              
64             __END__
65              
66             =pod
67              
68             =head1 NAME
69              
70             KiokuDB::LiveObjects::Scope - Scope helper object
71              
72             =head1 SYNOPSIS
73              
74             {
75             my $scope = $dir->new_scope;
76              
77             ... do work on $dir ...
78             }
79              
80             =head1 DESCRIPTION
81              
82             Live object scopes exist in order to ensure objects don't die too soon if the
83             only other references to them are weak.
84              
85             When scopes are destroyed the refcounts of the objects they refer to go down,
86             and the parent scope is replaced in the live object set.
87              
88             =head1 METHODS
89              
90             =over 4
91              
92             =item push
93              
94             Adds objects or entries, increasing their reference count.
95              
96             =item clear
97              
98             Clears the objects from the scope object.
99              
100             =item detach
101              
102             Marks this scope as no longer the "current" live object scope, if it is the current one.
103              
104             This allows keeping branching of scopes, which can be useful under long running
105             applications.
106              
107             =item remove
108              
109             Effectively kills the scope by clearing it and removing it from the live object set.
110              
111             =back
112              
113             =cut
114              
115