1927
|
1 |
"
|
|
2 |
COPYRIGHT (c) 2006 by eXept Software AG
|
|
3 |
All Rights Reserved
|
|
4 |
|
|
5 |
This software is furnished under a license and may be used
|
|
6 |
only in accordance with the terms of that license and with the
|
|
7 |
inclusion of the above copyright notice. This software may not
|
|
8 |
be provided or otherwise made available to, or used by, any
|
|
9 |
other person. No title to or ownership of the software is
|
|
10 |
hereby transferred.
|
|
11 |
"
|
1552
|
12 |
"{ Package: 'stx:libbasic2' }"
|
|
13 |
|
4504
|
14 |
"{ NameSpace: Smalltalk }"
|
|
15 |
|
1552
|
16 |
Object subclass:#Singleton
|
|
17 |
instanceVariableNames:''
|
|
18 |
classVariableNames:'Lock'
|
|
19 |
poolDictionaries:''
|
|
20 |
category:'System-Support'
|
|
21 |
!
|
|
22 |
|
|
23 |
Singleton class instanceVariableNames:'theOnlyInstance'
|
|
24 |
|
|
25 |
"
|
|
26 |
No other class instance variables are inherited by this class.
|
|
27 |
"
|
|
28 |
!
|
|
29 |
|
|
30 |
!Singleton class methodsFor:'documentation'!
|
|
31 |
|
1927
|
32 |
copyright
|
|
33 |
"
|
|
34 |
COPYRIGHT (c) 2006 by eXept Software AG
|
|
35 |
All Rights Reserved
|
|
36 |
|
|
37 |
This software is furnished under a license and may be used
|
|
38 |
only in accordance with the terms of that license and with the
|
|
39 |
inclusion of the above copyright notice. This software may not
|
|
40 |
be provided or otherwise made available to, or used by, any
|
|
41 |
other person. No title to or ownership of the software is
|
|
42 |
hereby transferred.
|
|
43 |
"
|
|
44 |
!
|
|
45 |
|
1552
|
46 |
documentation
|
|
47 |
"
|
|
48 |
Subclasses of Singleton have only a single instance.
|
|
49 |
The instance creation methods ensure that there is only one instance.
|
|
50 |
|
|
51 |
Singletons that would have inherited from Object could inherit from
|
|
52 |
Singleton. For Singletons that do not inherit from Object,
|
|
53 |
you have to copy the Singleton's code to your class.
|
|
54 |
|
|
55 |
[author:]
|
|
56 |
Stefan Vogel (stefan@zwerg)
|
|
57 |
|
|
58 |
[class instance variables:]
|
|
59 |
theOnlyInstance Object The only instance of this class
|
|
60 |
|
|
61 |
[class variables:]
|
|
62 |
Lock RecursionLock A global lock to protect agains races during instance creation
|
|
63 |
"
|
|
64 |
! !
|
|
65 |
|
|
66 |
!Singleton class methodsFor:'initialization'!
|
|
67 |
|
|
68 |
initialize
|
4509
|
69 |
Lock := RecursionLock new name:#Singleton
|
|
70 |
|
|
71 |
"Modified: / 30-08-2017 / 15:20:24 / cg"
|
1552
|
72 |
! !
|
|
73 |
|
|
74 |
!Singleton class methodsFor:'instance creation'!
|
|
75 |
|
|
76 |
basicNew
|
|
77 |
"allocate a singleton in a thread-safe way.
|
|
78 |
Do lazy locking here"
|
|
79 |
|
|
80 |
theOnlyInstance isNil ifTrue:[
|
|
81 |
Lock critical:[
|
|
82 |
theOnlyInstance isNil ifTrue:[
|
|
83 |
theOnlyInstance := super basicNew.
|
|
84 |
].
|
|
85 |
]
|
|
86 |
].
|
|
87 |
^ theOnlyInstance.
|
|
88 |
!
|
|
89 |
|
|
90 |
basicNew:anInteger
|
|
91 |
"allocate a singleton in a thread-safe way.
|
|
92 |
Do lazy locking here"
|
|
93 |
|
|
94 |
theOnlyInstance isNil ifTrue:[
|
|
95 |
Lock critical:[
|
|
96 |
theOnlyInstance isNil ifTrue:[
|
|
97 |
theOnlyInstance := super basicNew:anInteger.
|
|
98 |
].
|
|
99 |
]
|
|
100 |
].
|
|
101 |
^ theOnlyInstance.
|
4933
|
102 |
!
|
|
103 |
|
|
104 |
new
|
|
105 |
"allocate a singleton in a thread-safe way.
|
|
106 |
Do lazy locking here"
|
|
107 |
|
|
108 |
theOnlyInstance isNil ifTrue:[
|
|
109 |
Lock critical:[
|
|
110 |
theOnlyInstance isNil ifTrue:[
|
|
111 |
theOnlyInstance := super basicNew initialize.
|
|
112 |
].
|
|
113 |
]
|
|
114 |
].
|
|
115 |
^ theOnlyInstance.
|
|
116 |
|
|
117 |
"Created: / 01-04-2019 / 12:05:25 / Stefan Vogel"
|
|
118 |
!
|
|
119 |
|
|
120 |
new:count
|
|
121 |
"allocate a singleton in a thread-safe way.
|
|
122 |
Do lazy locking here"
|
|
123 |
|
|
124 |
theOnlyInstance isNil ifTrue:[
|
|
125 |
Lock critical:[
|
|
126 |
theOnlyInstance isNil ifTrue:[
|
|
127 |
theOnlyInstance := (super basicNew:count) initialize.
|
|
128 |
].
|
|
129 |
]
|
|
130 |
].
|
|
131 |
^ theOnlyInstance.
|
|
132 |
|
|
133 |
"Created: / 01-04-2019 / 12:05:42 / Stefan Vogel"
|
|
134 |
!
|
|
135 |
|
|
136 |
releaseInstance
|
|
137 |
"release the singleton's instance.
|
|
138 |
Use only for debugging!!"
|
|
139 |
|
|
140 |
Lock critical:[
|
|
141 |
theOnlyInstance := nil.
|
|
142 |
].
|
|
143 |
|
|
144 |
"Created: / 01-04-2019 / 12:02:58 / Stefan Vogel"
|
1552
|
145 |
! !
|
|
146 |
|
|
147 |
!Singleton class methodsFor:'accessing'!
|
|
148 |
|
1643
|
149 |
singletonLock
|
|
150 |
"can be used by other classes that are not subclasses of Singleton"
|
|
151 |
|
|
152 |
^ Lock
|
|
153 |
!
|
|
154 |
|
1552
|
155 |
theOnlyInstance
|
4628
|
156 |
theOnlyInstance isNil ifTrue:[
|
|
157 |
Lock critical:[
|
|
158 |
theOnlyInstance isNil ifTrue:[
|
|
159 |
theOnlyInstance := self new
|
|
160 |
].
|
|
161 |
]
|
|
162 |
].
|
|
163 |
^ theOnlyInstance.
|
|
164 |
|
|
165 |
"Modified: / 27-03-2018 / 12:44:24 / stefan"
|
1552
|
166 |
! !
|
|
167 |
|
|
168 |
!Singleton class methodsFor:'documentation'!
|
|
169 |
|
|
170 |
version
|
4504
|
171 |
^ '$Header$'
|
1552
|
172 |
! !
|
|
173 |
|
4504
|
174 |
|
1552
|
175 |
Singleton initialize!
|