commore  1.0.6-SNAPSHOT
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
AutoRef.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2006-2014 Raphael David / CANTOR
3 //
4 
5 #ifndef CMR_AUTO_REF_DEFINED
6 #define CMR_AUTO_REF_DEFINED
7 
8 #include "Commore.h"
9 #include "RefObject.h"
10 
11 namespace commore
12 {
13 
14  inline void AutoRefBase::attach()
15  {
16  if (p_)
17  {
18  p_->attach();
19  }
20  }
21  inline void AutoRefBase::release()
22  {
23  if (p_)
24  {
25  p_->release();
26  p_ = 0;
27  }
28  }
29 
30  inline void IAutoRefBase::attach()
31  {
32  if (p_)
33  {
34  p_->nested_attach();
35  }
36  }
37  inline void IAutoRefBase::release()
38  {
39  if (p_)
40  {
41  p_->nested_release();
42  p_ = 0;
43  }
44  }
45 
86  template <class E> class AutoRef : public AutoRefBase
87  {
88  public:
94  : AutoRefBase(dynamic_cast<E*>(E::auto_create().get_()))
95  {}
99  AutoRef(const AChar* s)
100  : AutoRefBase(dynamic_cast<E*>(RefFactory::lookup_create(E::factory_name(), s, dynamic_cast<E*>(E::auto_create(s).get_())).get_()))
101  {}
105  explicit AutoRef(bool)
106  : AutoRefBase(dynamic_cast<E*>(E::create().get_()))
107  {}
111  AutoRef(E* p)
112  : AutoRefBase(dynamic_cast<E*>(p))
113  {}
118  : AutoRefBase(dynamic_cast<E*>(p))
119  {}
123  AutoRef(const AutoRef& a)
124  : AutoRefBase(a.get_())
125  {}
130  : AutoRefBase(dynamic_cast<E*>(a.get_()))
131  {}
136  {
137  if ((void*)this != (void*)&a)
138  {
139  set_(a.get_());
140  }
141  return *this;
142  }
148  {
149  if ((void*)this != (void*)&a)
150  {
151  set_(dynamic_cast<E*>(a.get_()));
152  }
153  return *this;
154  }
159  {
160  set_(dynamic_cast<E*>(p));
161  return *this;
162  }
166  operator bool () const
167  {
168  return get_() != 0;
169  }
170 
171  public:
175  bool create(const AChar* s)
176  {
177  set_(dynamic_cast<E*>(RefFactory::lookup_create(E::factory_name(), s, dynamic_cast<E*>(E::create(s).get_())).get_()));
178  return get() != 0;
179  }
183  bool create()
184  {
185  set_(dynamic_cast<E*>(E::create().get_()));
186  return get() != 0;
187  }
192  bool bind(const AChar* s)
193  {
194  set_(dynamic_cast<E*>(E::bind(s).get_()));
195  return get() != 0;
196  }
201  bool bind()
202  {
203  set_(dynamic_cast<E*>(E::bind().get_()));
204  return get() != 0;
205  }
209  E* get() const
210  {
211  return (E*)get_();
212  }
217  E& operator*() const
218  {
219  return *get();
220  }
224  E* operator->() const
225  {
226  return get();
227  }
231  operator E* () const
232  {
233  return get();
234  }
238  bool operator == (const AutoRef& a) const
239  {
240  return get() == a.get();
241  }
242  bool operator != (const AutoRef& a) const
243  {
244  return get() != a.get();
245  }
246  bool operator < (const AutoRef& a) const
247  {
248  return get() < a.get();
249  }
253  bool is_bound() const
254  {
255  return get_() != 0;
256  }
260  void unbind()
261  {
262  set_(0);
263  }
264  };
265 
270  template <class E> class IAutoRef : public IAutoRefBase
271  {
272  public:
274  : IAutoRefBase(dynamic_cast<E*>(E::auto_create().get_())) {}
275  IAutoRef(const AChar* s)
276  : IAutoRefBase(dynamic_cast<E*>(RefFactory::lookup_create(E::factory_name(), s, dynamic_cast<E*>(E::auto_create(s).get_())).get_())) {}
277  IAutoRef(bool)
278  : IAutoRefBase(dynamic_cast<E*>(E::create().get_())) {}
279  IAutoRef(E* p)
280  : IAutoRefBase(dynamic_cast<E*>(p)) {}
282  : IAutoRefBase(dynamic_cast<E*>(p)) {}
283  IAutoRef(const IAutoRef& a)
284  : IAutoRefBase(a.get_()) {}
286  : IAutoRefBase(dynamic_cast<E*>(a.get_())) {}
288  {
289  if ((void*)this != (void*)&a)
290  {
291  set_(a.get_());
292  }
293  return *this;
294  }
296  {
297  if ((void*)this != (void*)&a)
298  {
299  set_(dynamic_cast<E*>(a.get_()));
300  }
301  return *this;
302  }
303  //IAutoRef& operator = (E* p) { set_(p); return *this; }
305  {
306  set_(dynamic_cast<E*>(p));
307  return *this;
308  }
309  operator bool () const
310  {
311  return get_() != 0;
312  }
313 
314  public:
315  bool create(const AChar* s)
316  {
317  set_(dynamic_cast<E*>(RefFactory::lookup_create(E::factory_name(), s, dynamic_cast<E*>(E::create(s).get_())).get_()));
318  return get() != 0;
319  }
320  bool create()
321  {
322  set_(dynamic_cast<E*>(E::create().get_()));
323  return get() != 0;
324  }
325  bool bind()
326  {
327  set_(dynamic_cast<E*>(E::bind().get_()));
328  return get() != 0;
329  }
330  bool bind(const AChar* s)
331  {
332  set_(dynamic_cast<E*>(E::bind(s).get_()));
333  return get() != 0;
334  }
335  E* get() const
336  {
337  return (E*)get_();
338  }
339  //E& operator*() const { return *get();}
340  E* operator->() const
341  {
342  return get();
343  }
344  operator AutoRef<E> () const
345  {
346  return get();
347  }
348  bool operator == (const IAutoRef& a) const
349  {
350  return get() == a.get();
351  }
352  bool operator != (const IAutoRef& a) const
353  {
354  return get() != a.get();
355  }
356  bool is_bound() const
357  {
358  return get_() != 0;
359  }
360  };
361 
362 };
363 
364 #ifdef CMR_BSTREAM_INCLUDED
365 template<class X>
366 commore::OBStream& operator << (commore::OBStream& o, const commore::AutoRef<X>& p)
367 {
368  o << (void*)p.get();
369  return o;
370 }
371 
372 template<class X>
373 commore::OBStream& operator << (commore::OBStream& o, const commore::IAutoRef<X>& p)
374 {
375  o << (void*)p.get();
376  return o;
377 }
378 #endif
379 
380 #endif
bool bind(const AChar *s)
Definition: AutoRef.h:330
Definition: RefObject.h:24
bool bind(const AChar *s)
Definition: AutoRef.h:192
AutoRef(bool)
Definition: AutoRef.h:105
IAutoRef(const IAutoRefBase &a)
Definition: AutoRef.h:285
Definition: AutoRefBase.h:102
void set_(RefObject *p)
Definition: AutoRefBase.h:134
AutoRef(const AutoRef &a)
Definition: AutoRef.h:123
AutoRef(const AutoRefBase &a)
Definition: AutoRef.h:129
E & operator*() const
Definition: AutoRef.h:217
Definition: AutoRefBase.h:18
char AChar
Definition: Type.h:65
static AutoRefBase lookup_create(const AChar *factory_name, const AChar *s, const AutoRefBase &def)
Definition: AutoRef.cpp:149
void unbind()
Definition: AutoRef.h:260
bool bind()
Definition: AutoRef.h:325
virtual void release()
Definition: AutoRef.cpp:53
bool operator==(const AutoRef &a) const
Definition: AutoRef.h:238
virtual void nested_release()
Definition: AutoRef.cpp:87
E * get() const
Definition: AutoRef.h:209
virtual void attach()
Definition: AutoRef.cpp:37
bool create()
Definition: AutoRef.h:320
bool create(const AChar *s)
Definition: AutoRef.h:315
IAutoRef & operator=(const IAutoRef &a)
Definition: AutoRef.h:287
IAutoRef(bool)
Definition: AutoRef.h:277
E * operator->() const
Definition: AutoRef.h:224
bool operator!=(const AutoRef &a) const
Definition: AutoRef.h:242
bool operator!=(const IAutoRef &a) const
Definition: AutoRef.h:352
AutoRef & operator=(const AutoRef &a)
Definition: AutoRef.h:135
bool create(const AChar *s)
Definition: AutoRef.h:175
Definition: RefObject.h:89
bool operator<(const AutoRef &a) const
Definition: AutoRef.h:246
bool operator==(const IAutoRef &a) const
Definition: AutoRef.h:348
IAutoRef()
Definition: AutoRef.h:273
AutoRef is a smart pointer on RefObject objects. AutoRef is templatized with the type of the pointed ...
Definition: AutoRef.h:86
IAutoRef(const IAutoRef &a)
Definition: AutoRef.h:283
bool is_bound() const
Definition: AutoRef.h:253
IAutoRef(RefObject *p)
Definition: AutoRef.h:281
bool is_bound() const
Definition: AutoRef.h:356
RefObject * get_() const
Definition: AutoRefBase.h:65
AutoRef(E *p)
Definition: AutoRef.h:111
void set_(RefObject *p)
Definition: AutoRefBase.h:72
Definition: IOBStream.h:166
AutoRef(const AChar *s)
Definition: AutoRef.h:99
AutoRef(RefObject *p)
Definition: AutoRef.h:117
AutoRef()
Definition: AutoRef.h:93
IAutoRef(E *p)
Definition: AutoRef.h:279
E * operator->() const
Definition: AutoRef.h:340
E * get() const
Definition: AutoRef.h:335
RefObject * get_() const
Definition: AutoRefBase.h:130
Definition: AutoRef.h:270
virtual void nested_attach()
Definition: AutoRef.cpp:77
bool create()
Definition: AutoRef.h:183
IAutoRef(const AChar *s)
Definition: AutoRef.h:275
bool bind()
Definition: AutoRef.h:201