NEST  2.6.0,not_revisioned_source_dir@0
lockptr.h
Go to the documentation of this file.
1 /*
2  * lockptr.h
3  *
4  * This file is part of NEST.
5  *
6  * Copyright (C) 2004 The NEST Initiative
7  *
8  * NEST is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * NEST is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with NEST. If not, see <http://www.gnu.org/licenses/>.
20  *
21  */
22 
23 #ifndef LOCK_PTR_H
24 #define LOCK_PTR_H
25 
26 #ifdef NDEBUG
27 #define LOCK_PTR_NDEBUG
28 #endif
29 
30 #include <cassert>
31 #include <cstddef>
32 
83 template<class D>
84 class lockPTR
85 {
87  {
88 
89  private:
90 
91  D *pointee; // pointer to handled Datum object
93  bool deletable;
94  bool locked;
95 
96 // forbid this constructor!
98 
99  public:
100 
101  PointerObject(D* p = NULL)
102  :pointee(p),
104  deletable(true), locked(false){}
105 
107  :pointee(&p_o),
109  deletable(false), locked(false){}
110 
112  {
113  assert(number_of_references == 0); // This will invalidate the still
114  // existing pointer!
115  assert(!locked);
116  if((pointee != NULL) && deletable && (!locked))
117  delete pointee;
118  }
119 
120  D * get(void) const
121  {
122  return pointee;
123  }
124 
125  void addReference(void)
126  {
128  }
129 
130  void removeReference(void)
131  {
132 // assert(number_of_references > 0);
133 
135  if(number_of_references == 0)
136  {
137  delete this;
138  }
139  }
140 
141  size_t references(void) const
142  {
143  return number_of_references;
144  }
145 
146  bool islocked(void) const
147  {
148  return locked;
149  }
150 
151  bool isdeletable(void) const
152  {
153  return deletable;
154  }
155 
156  void lock(void)
157  {
158  assert(locked == false);
159  locked = true;
160  }
161 
162  void unlock(void)
163  {
164  assert(locked == true);
165  locked = false;
166  }
167 
168  };
169 
170  PointerObject *obj;
171 
172  public:
173 
174  // lockPTR() ; // generated automatically.
175  // The default, argumentless constructor is used in
176  // class declarations and generates an empty lockPTR
177  // object which must then be initialised, for example
178  // by assignement.
179 
180  explicit lockPTR(D* p=NULL)
181  {
182  obj = new PointerObject(p);
183  assert(obj != NULL);
184  }
185 
186  explicit lockPTR(D& p_o)
187  {
188  obj = new PointerObject(p_o);
189  assert(obj != NULL);
190  }
191 
192  lockPTR(const lockPTR<D>& spd)
193  : obj(spd.obj)
194  {
195  assert(obj != NULL);
196  obj->addReference();
197  }
198 
199  virtual ~lockPTR()
200  {
201  assert(obj != NULL);
202  obj->removeReference();
203  }
204 
206  {
207  // assert(obj != NULL);
208  // assert(spd.obj != NULL);
209 
210 // The following order of the expressions protects
211 // against a=a;
212 
213  spd.obj->addReference();
214  obj->removeReference();
215 
216  obj = spd.obj;
217 
218  return *this;
219  }
220 
222  {
223  *this = lockPTR<D>(s);
224  assert(!(obj->isdeletable()));
225  return *this;
226  }
227 
228  lockPTR<D> operator=(D const &s)
229  {
230  *this = lockPTR<D>(s);
231  assert(!(obj->isdeletable()));
232  return *this;
233  }
234 
235  D* get(void)
236  {
237  assert(!obj->islocked());
238  obj->lock(); // Try to lock Object
239  return obj->get();
240  }
241 
242  D* get(void) const
243  {
244  assert(!obj->islocked());
245 
246  obj->lock(); // Try to lock Object
247  return obj->get();
248  }
249 
250  D* operator->() const
251  {
252  assert(obj->get() != NULL);
253 
254  return obj->get();
255  }
256 
258  {
259  assert(obj->get() != NULL);
260 
261  return obj->get();
262  }
263 
265  {
266  assert(obj->get() != NULL);
267 
268  return *(obj->get());
269  }
270 
271  const D& operator*() const
272  {
273  assert(obj->get() != NULL);
274  return *(obj->get());
275  }
276 
277 
278  bool operator!() const
279  {
280  // assert(obj != NULL);
281 
282  return (obj->get() == NULL);
283  }
284 
285 
286  /* operator==, !=
287  Identity operator. These are inherited by derived types, so they should only be called
288  by the equals method of the derived class which checks for type identity
289  or when both classes are known to be bare lockPTR<D>.
290 
291  These follow identity semantics rather than equality semantics.
292  The underlying object should only ever be owned by a single PointerObject
293  that are shared by lockPTR<D>s, so this is equivalent to comparing the address
294  of the D objects.
295  */
296  bool operator==(const lockPTR<D>& p) const
297  {
298  return (obj == p.obj);
299  }
300 
301  bool operator!=(const lockPTR<D>& p) const
302  {
303  return (obj != p.obj);
304  }
305 
306 
307  bool valid(void) const
308  {
309  assert(obj != NULL);
310  return (obj->get() != NULL);
311  }
312 
313  bool islocked(void) const
314  {
315  assert(obj != NULL);
316  return (obj->islocked());
317  }
318 
319  bool deletable(void) const
320  {
321  assert(obj != NULL);
322  return (obj->isdeletable());
323  }
324 
325  void lock(void) const
326  {
327  assert(obj!=NULL);
328  obj->lock();
329  }
330 
331  void unlock(void) const
332  {
333  assert(obj!=NULL);
334  obj->unlock();
335  }
336 
337  void unlock(void)
338  {
339  assert(obj!=NULL);
340  obj->unlock();
341  }
342 
343  size_t references(void) const
344  {
345  return (obj==NULL)? 0: obj->references();
346  }
347 };
348 
349 #ifndef LOCK_PTR_NDEBUG
350 #undef NDEBUG
351 #endif
352 
353 #endif
PointerObject(PointerObject const &)
bool operator!=(const lockPTR< D > &p) const
Definition: lockptr.h:301
void lock(void) const
Definition: lockptr.h:325
lockPTR< D > operator=(D &s)
Definition: lockptr.h:221
void lock(void)
Definition: lockptr.h:156
bool islocked(void) const
Definition: lockptr.h:313
~PointerObject()
Definition: lockptr.h:111
bool locked
Definition: lockptr.h:94
void unlock(void) const
Definition: lockptr.h:331
virtual ~lockPTR()
Definition: lockptr.h:199
bool operator==(const lockPTR< D > &p) const
Definition: lockptr.h:296
void unlock(void)
Definition: lockptr.h:337
D & operator*()
Definition: lockptr.h:264
assert(pNet!=0)
size_t references(void) const
Definition: lockptr.h:141
bool valid(void) const
< returns true if and only if obj->pointee != NULL
Definition: lockptr.h:307
lockPTR(D &p_o)
Definition: lockptr.h:186
lockPTR< D > operator=(const lockPTR< D > &spd)
Definition: lockptr.h:205
size_t references(void) const
Definition: lockptr.h:343
bool deletable
Definition: lockptr.h:93
void unlock(void)
Definition: lockptr.h:162
lockPTR(D *p=NULL)
Definition: lockptr.h:180
bool operator!() const
< returns true if and only if obj->pointee == NULL
Definition: lockptr.h:278
const D & operator*() const
Definition: lockptr.h:271
D * operator->()
Definition: lockptr.h:257
lockPTR(const lockPTR< D > &spd)
Definition: lockptr.h:192
bool isdeletable(void) const
Definition: lockptr.h:151
PointerObject(D *p=NULL)
Definition: lockptr.h:101
size_t number_of_references
Definition: lockptr.h:92
D * get(void) const
Definition: lockptr.h:120
lockPTR< D > operator=(D const &s)
Definition: lockptr.h:228
PointerObject(D &p_o)
Definition: lockptr.h:106
void removeReference(void)
Definition: lockptr.h:130
This template is the standard safe-pointer implementation of SYNOD.
Definition: lockptr.h:84
D * pointee
Definition: lockptr.h:91
D * operator->() const
Definition: lockptr.h:250
bool deletable(void) const
Definition: lockptr.h:319
const Name p("p")
current release probability (Tsodyks2_connection)
Definition: nest_names.h:218
PointerObject * obj
Definition: lockptr.h:170
Definition: lockptr.h:86
void addReference(void)
Definition: lockptr.h:125
bool islocked(void) const
Definition: lockptr.h:146