00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 import sys
00019 import string
00020 import threading
00021
00022 import OpenRTM
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 class ScopedLock:
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053 def __init__(self, mutex):
00054 self.mutex = mutex
00055 self.mutex.acquire()
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069 def __del__(self):
00070 self.mutex.release()
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086 class ObjectManager:
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103 def __init__(self, predicate):
00104 self._objects = self.Objects()
00105 self._predicate = predicate
00106
00107
00108
00109
00110
00111
00112
00113
00114 class Objects:
00115 def __init__(self):
00116 self._mutex = threading.RLock()
00117 self._obj = []
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136 def registerObject(self, obj):
00137 guard = ScopedLock(self._objects._mutex)
00138 predi = self._predicate(factory=obj)
00139
00140 for _obj in self._objects._obj:
00141 if predi(_obj):
00142 return False
00143
00144 self._objects._obj.append(obj)
00145 return True
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164 def unregisterObject(self, id):
00165 guard = ScopedLock(self._objects._mutex)
00166 predi = self._predicate(name=id)
00167 i = 0
00168 for _obj in self._objects._obj:
00169 if predi(_obj):
00170 ret = _obj
00171 del self._objects._obj[i]
00172 return ret
00173 i+=1
00174
00175 return None
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195 def find(self, id):
00196 guard = ScopedLock(self._objects._mutex)
00197 predi = self._predicate(name=id)
00198 for _obj in self._objects._obj:
00199 if predi(_obj):
00200 return _obj
00201
00202 return None
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219 def getObjects(self):
00220 guard = ScopedLock(self._objects._mutex)
00221 return self._objects._obj
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236 def for_each(self,p):
00237 guard = ScopedLock(self._objects._mutex)
00238 predi = p()
00239
00240 for _obj in self._objects._obj:
00241 predi(_obj)
00242
00243 return predi
00244