| 1 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||
| 2 | # GX Framework (c) 2009-2011 Jörg A. Uzarek <uzarek@runlevelnull.de> | ||||||||||||||||||||
| 3 | # File: GX/Class/Singleton.pm | ||||||||||||||||||||
| 4 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | package GX::Class::Singleton; | ||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 76 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 76 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 76 | ▶ | use base 'GX::Class::Object'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 76 | ▶ | use GX::Exception (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||
| 17 | # Export mechanism | ||||||||||||||||||||
| 18 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 45 | ▶ | sub import { | ||||||||||||||||||
|
Subroutine
import: called CC=2 SCAR=7.0
| |||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 45 | my $package = shift; | |||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | 45 | ▶ | return unless $package eq __PACKAGE__; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | 45 | unshift( @_, 'GX::Class::Object', superclass => $package ); | |||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 45 | goto &GX::Class::Object::import; | |||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | } | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||
| 34 | # Object registry | ||||||||||||||||||||
| 35 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | 76 | my %OBJECTS; | |||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||
| 41 | # Constructor | ||||||||||||||||||||
| 42 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | 91 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | 91 | ▶ | return $_[0] if ref $_[0]; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | 90 | my $class = shift; | |||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | 90 | ▶ | if ( $OBJECTS{$class} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | 4 | ▶ | if ( @_ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 53 | 1 | GX::Exception->complain( "$class has already been instantiated" ); | |||||||||||||||||||
| 54 | } | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | 3 | return $OBJECTS{$class}; | |||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | } | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | 86 | ▶ | return $OBJECTS{$class} = eval { $class->SUPER::new( @_ ) } || GX::Exception->complain( $@ ); | ||||||||||||||||||
|
Condition66%2/3
eval {
do {
$class->SUPER::new(@_)
}
} || 'GX::Exception'->complain($@)
MC/DC0%0/2
eval {
do {
$class->SUPER::new(@_)
}
} || 'GX::Exception'->complain($@)
eval {
do {
$class->SUPER::new(@_)
}
}
'GX::Exception'->complain($@)
Truth table33%1/3
A || B
A= eval {
do {
$class->SUPER::new(@_)
}
}B= 'GX::Exception'->complain($@)
| |||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | } | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||
| 66 | # Public methods | ||||||||||||||||||||
| 67 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 0 | ▶ | sub clear { | ||||||||||||||||||
|
Subroutine
clear: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | 0 | return shift->instance->clear; | |||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | } | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 2 | ▶ | sub destroy { | ||||||||||||||||||
|
Subroutine
destroy: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | 2 | ▶ | return delete $OBJECTS{ ref $_[0] || $_[0] }; | ||||||||||||||||||
|
Condition66%2/3
ref $_[0] || $_[0]
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0]
$_[0]
Truth table66%2/3
A || B
A= ref $_[0]B= $_[0]
| |||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | } | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | 0 | ▶ | sub dump { | ||||||||||||||||||
|
Subroutine
dump: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | 0 | return shift->instance->dump; | |||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | } | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | 463 | ▶ | sub instance { | ||||||||||||||||||
|
Subroutine
instance: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | 463 | ▶ | return $_[0] if ref $_[0]; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | 110 | ▶ | return $OBJECTS{$_[0]} if defined $OBJECTS{$_[0]}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 71 | ▶ | return eval { $_[0]->new } || GX::Exception->complain( $@ ); | ||||||||||||||||||
|
Condition66%2/3
eval {
do {
$_[0]->new
}
} || 'GX::Exception'->complain($@)
MC/DC0%0/2
eval {
do {
$_[0]->new
}
} || 'GX::Exception'->complain($@)
eval {
do {
$_[0]->new
}
}
'GX::Exception'->complain($@)
Truth table33%1/3
A || B
A= eval {
do {
$_[0]->new
}
}B= 'GX::Exception'->complain($@)
| |||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | } | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | 76 | 1; | |||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | __END__ | ||||||||||||||||||||