1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
# -*- coding: utf-8 -*-
#
# AWL simulator - object reference manager
#
# Copyright 2015 Michael Buesch <m@bues.ch>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
from __future__ import division, absolute_import, print_function, unicode_literals
from awlsim.common.compat import *
class ObjRef(object):
"""An object reference."""
@classmethod
def make(cls, name, managerOrRef, obj = None, inheritRef = False):
"""Make a new ObjRef instance.
name -> A name string (or callable returning a string).
managerOrRef -> An ObjRefManager instance or ObjRef instance.
obj -> The object that is associated with this ref.
inheritRef -> If False, a new ref is created.
If True and managerOrRef is an ObjRef, the ref is inherited.
"""
if managerOrRef is None:
return None
elif isinstance(managerOrRef, ObjRefManager):
manager = managerOrRef
return cls(name, manager, obj)
elif isinstance(managerOrRef, ObjRef):
oldRef = managerOrRef
newRef = cls(name, oldRef.__manager, obj)
if inheritRef and oldRef.alive:
oldRef.destroy()
return newRef
else:
assert(0)
def __init__(self, name, manager, obj = None):
"""Contruct object reference.
name: Informational name string or callable returing a string.
manager: An ObjRefManager instance.
obj: The object that is associated with this ref (optional).
"""
self.__name = name
self.__obj = obj
self.__manager = manager
self.__manager._addRef(self)
def destroy(self):
"""Destroy (unref) this reference.
This removes the reference from the manager.
"""
if self.alive:
self.__manager.refDestroyed(self)
self.__name = None
self.__obj = None
self.__manager = None
@property
def name(self):
"""The reference name string.
"""
if callable(self.__name):
return self.__name(self)
return self.__name
@property
def obj(self):
"""The object that is associated with this ref.
"""
return self.__obj
@property
def manager(self):
"""Get the manager that this ref belongs to.
"""
return self.__manager
@property
def alive(self):
"""True, if this reference is alive.
False, if this reference was destroyed.
"""
return self.__manager is not None
def __repr__(self):
return str(self.name)
class ObjRefManager(object):
"""Object reference manager.
The manager belongs to the object that actually is referenced.
"""
def __init__(self, name,
oneDestroyedCallback = None,
allDestroyedCallback = None):
"""Contruct reference manager.
name: Informational name string or callable returing a string.
oneDestroyedCallback: Optional callback. Called, if one ref was destroyed.
allDestroyedCallback: Optional callback. Called, if all refs were destroyed.
"""
self.__name = name
self.__oneDestroyedCallback = oneDestroyedCallback
self.__allDestroyedCallback = allDestroyedCallback
self.__refs = set()
@property
def name(self):
"""The manager name string.
"""
if callable(self.__name):
return self.__name(self)
return self.__name
@property
def hasReferences(self):
"""Returns true, if this manager holds references.
"""
return bool(self.__refs)
@property
def refs(self):
"""Get a set of all references to this object.
"""
return set(self.__refs)
def _addRef(self, objRef):
self.__refs.add(objRef)
def refDestroyed(self, objRef):
"""Callback: Called if one reference was destroyed.
Override this method or set oneDestroyedCallback,
if you want to be notified.
"""
self.__refs.remove(objRef)
if self.__oneDestroyedCallback:
self.__oneDestroyedCallback(objRef)
if not self.__refs:
self.allRefsDestroyed()
def allRefsDestroyed(self):
"""Callback: Called if all references were destroyed.
Override this method or set allDestroyedCallback,
if you want to be notified.
"""
if self.__allDestroyedCallback:
self.__allDestroyedCallback()
def __repr__(self):
return str(self.name)
|