libcamgm
PtrTypes.hpp
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
11 #ifndef CA_MGM_BASE_PTRTYPES_H
12 #define CA_MGM_BASE_PTRTYPES_H
13 
14 #include <string>
15 
16 #include <boost/scoped_ptr.hpp>
17 #include <boost/shared_ptr.hpp>
18 #include <boost/weak_ptr.hpp>
19 #include <boost/intrusive_ptr.hpp>
20 
22 namespace ca_mgm
23 {
24 
41 
73  struct NullDeleter
74  {
75  void operator()( const void *const ) const
76  {}
77  };
78 
80  using boost::scoped_ptr;
81 
83  using boost::shared_ptr;
84 
86  using boost::weak_ptr;
87 
89  using boost::intrusive_ptr;
90 
92  using boost::static_pointer_cast;
94  using boost::const_pointer_cast;
96  using boost::dynamic_pointer_cast;
97 
99 } // namespace ca_mgm
102 namespace std
103 {
104 
105  // namespace sub {
106  // class Foo;
107  // typedef ca_mgm::intrusive_ptr<Foo> Foo_Ptr; // see DEFINE_PTR_TYPE(NAME) macro below
108  // }
109 
110  // Defined in namespace std g++ finds the output operator (König-Lookup),
111  // even if we typedef the pointer in a different namespace than ::ca_mgm.
112  // Otherwise we had to define an output operator always in the same namespace
113  // as the typedef (else g++ will just print the pointer value).
114 
116  template<class _D>
117  inline std::ostream & operator<<( std::ostream & str, const ca_mgm::shared_ptr<_D> & obj )
118  {
119  if ( obj )
120  return str << *obj;
121  return str << std::string("NULL");
122  }
124  template<class _D>
125  inline std::ostream & dumpOn( std::ostream & str, const ca_mgm::shared_ptr<_D> & obj )
126  {
127  if ( obj )
128  return dumpOn( str, *obj );
129  return str << std::string("NULL");
130  }
131 
133  template<class _D>
134  inline std::ostream & operator<<( std::ostream & str, const ca_mgm::intrusive_ptr<_D> & obj )
135  {
136  if ( obj )
137  return str << *obj;
138  return str << std::string("NULL");
139  }
141  template<class _D>
142  inline std::ostream & dumpOn( std::ostream & str, const ca_mgm::intrusive_ptr<_D> & obj )
143  {
144  if ( obj )
145  return dumpOn( str, *obj );
146  return str << std::string("NULL");
147  }
149 } // namespace std
152 namespace ca_mgm
153 {
154 
156  //
157  // RW_pointer traits
158  //
160 
165  namespace rw_pointer {
166 
167  template<class _D>
168  struct Shared
169  {
170  typedef shared_ptr<_D> _Ptr;
171  typedef shared_ptr<const _D> _constPtr;
173  bool unique( const _constPtr & ptr_r )
174  { return !ptr_r || ptr_r.unique(); }
175  bool unique( const _Ptr & ptr_r )
176  { return !ptr_r || ptr_r.unique(); }
178  long use_count( const _constPtr & ptr_r ) const
179  { return ptr_r.use_count(); }
180  long use_count( const _Ptr & ptr_r ) const
181  { return ptr_r.use_count(); }
182  };
183 
184  template<class _D>
185  struct Intrusive
186  {
187  typedef intrusive_ptr<_D> _Ptr;
188  typedef intrusive_ptr<const _D> _constPtr;
190  bool unique( const _constPtr & ptr_r )
191  { return !ptr_r || (ptr_r->refCount() <= 1); }
192  bool unique( const _Ptr & ptr_r )
193  { return !ptr_r || (ptr_r->refCount() <= 1); }
195  long use_count( const _constPtr & ptr_r ) const
196  { return ptr_r ? ptr_r->refCount() : 0; }
197  long use_count( const _Ptr & ptr_r ) const
198  { return ptr_r ? ptr_r->refCount() : 0; }
199  };
200 
201  template<class _D>
202  struct Scoped
203  {
204  typedef scoped_ptr<_D> _Ptr;
205  typedef scoped_ptr<const _D> _constPtr;
207  bool unique( const _constPtr & ptr_r )
208  { return true; }
209  bool unique( const _Ptr & ptr_r )
210  { return true; }
212  long use_count( const _constPtr & ptr_r ) const
213  { return ptr_r ? 1 : 0; }
214  long use_count( const _Ptr & ptr_r ) const
215  { return ptr_r ? 1 : 0; }
216  };
217 
218  }
220 
226  template<class _D>
227  inline _D * rwcowClone( const _D * rhs )
228  { return rhs->clone(); }
229 
231  //
232  // CLASS NAME : RW_pointer
233  //
271  template<class _D, class _Traits = rw_pointer::Shared<_D> >
272  struct RW_pointer
273  {
274  typedef typename _Traits::_Ptr _Ptr;
275  typedef typename _Traits::_constPtr _constPtr;
276 #if defined( BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS ) || defined( BOOST_NO_CXX11_NULLPTR )
277  typedef typename _Ptr::unspecified_bool_type unspecified_bool_type;
278 #endif
279 
280  explicit
281  RW_pointer( typename _Ptr::element_type * dptr = 0 )
282  : _dptr( dptr )
283  {}
284 
285  explicit
286  RW_pointer( _Ptr dptr )
287  : _dptr( dptr )
288  {}
289 
290  void reset()
291  { _Ptr().swap( _dptr ); }
292 
293  void reset( typename _Ptr::element_type * dptr )
294  { _Ptr( dptr ).swap( _dptr ); }
295 
296  void swap( RW_pointer & rhs )
297  { _dptr.swap( rhs._dptr ); }
298 
299  void swap( _Ptr & rhs )
300  { _dptr.swap( rhs ); }
301 
302 #if defined( BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS ) || defined( BOOST_NO_CXX11_NULLPTR )
303  operator unspecified_bool_type() const
304  { return _dptr; }
305 #else
306  explicit operator bool () const { return _dptr.get() != 0; }
307 #endif
308 
309  const _D & operator*() const
310  { return *_dptr; };
311 
312  const _D * operator->() const
313  { return _dptr.get(); }
314 
315  const _D * get() const
316  { return _dptr.get(); }
317 
318  _D & operator*()
319  { return *_dptr; }
320 
321  _D * operator->()
322  { return _dptr.get(); }
323 
324  _D * get()
325  { return _dptr.get(); }
326 
327  public:
328  bool unique() const
329  { return _Traits().unique( _dptr ); }
330 
331  long use_count() const
332  { return _Traits().use_count( _dptr ); }
333 
335  { return _dptr; }
336 
338  { return _dptr; }
339 
341  { return _dptr; }
342 
343  private:
345  };
347 
353  template<class _D, class _Ptr>
354  inline std::ostream & operator<<( std::ostream & str, const RW_pointer<_D, _Ptr> & obj )
355  {
356  if ( obj.get() )
357  return str << *obj.get();
358  return str << std::string("NULL");
359  }
360 
362  template<class _D, class _Ptr>
363  inline bool operator==( const RW_pointer<_D, _Ptr> & lhs, const RW_pointer<_D, _Ptr> & rhs )
364  { return( lhs.get() == rhs.get() ); }
366  template<class _D, class _Ptr>
367  inline bool operator==( const RW_pointer<_D, _Ptr> & lhs, const typename _Ptr::_Ptr & rhs )
368  { return( lhs.get() == rhs.get() ); }
370  template<class _D, class _Ptr>
371  inline bool operator==( const typename _Ptr::_Ptr & lhs, const RW_pointer<_D, _Ptr> & rhs )
372  { return( lhs.get() == rhs.get() ); }
374  template<class _D, class _Ptr>
375  inline bool operator==( const RW_pointer<_D, _Ptr> & lhs, const typename _Ptr::_constPtr & rhs )
376  { return( lhs.get() == rhs.get() ); }
378  template<class _D, class _Ptr>
379  inline bool operator==( const typename _Ptr::_constPtr & lhs, const RW_pointer<_D, _Ptr> & rhs )
380  { return( lhs.get() == rhs.get() ); }
381 
382 
384  template<class _D, class _Ptr>
385  inline bool operator!=( const RW_pointer<_D, _Ptr> & lhs, const RW_pointer<_D, _Ptr> & rhs )
386  { return ! ( lhs == rhs ); }
388  template<class _D, class _Ptr>
389  inline bool operator!=( const RW_pointer<_D, _Ptr> & lhs, const typename _Ptr::_Ptr & rhs )
390  { return ! ( lhs == rhs ); }
392  template<class _D, class _Ptr>
393  inline bool operator!=( const typename _Ptr::_Ptr & lhs, const RW_pointer<_D, _Ptr> & rhs )
394  { return ! ( lhs == rhs ); }
396  template<class _D, class _Ptr>
397  inline bool operator!=( const RW_pointer<_D, _Ptr> & lhs, const typename _Ptr::_constPtr & rhs )
398  { return ! ( lhs == rhs ); }
400  template<class _D, class _Ptr>
401  inline bool operator!=( const typename _Ptr::_constPtr & lhs, const RW_pointer<_D, _Ptr> & rhs )
402  { return ! ( lhs == rhs ); }
403 
404 
406  //
407  // CLASS NAME : RWCOW_pointer
408  //
416  template<class _D, class _Traits = rw_pointer::Shared<_D> >
418  {
419  typedef typename _Traits::_Ptr _Ptr;
420  typedef typename _Traits::_constPtr _constPtr;
421 #if defined( BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS ) || defined( BOOST_NO_CXX11_NULLPTR )
422  typedef typename _Ptr::unspecified_bool_type unspecified_bool_type;
423 #endif
424  explicit
425  RWCOW_pointer( typename _Ptr::element_type * dptr = 0 )
426  : _dptr( dptr )
427  {}
428 
429  explicit
431  : _dptr( dptr )
432  {}
433 
434  void reset()
435  { _Ptr().swap( _dptr ); }
436 
437  void reset( typename _Ptr::element_type * dptr )
438  { _Ptr( dptr ).swap( _dptr ); }
439 
440  void swap( RWCOW_pointer & rhs )
441  { _dptr.swap( rhs._dptr ); }
442 
443  void swap( _Ptr & rhs )
444  { _dptr.swap( rhs ); }
445 
446 #if defined( BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS ) || defined( BOOST_NO_CXX11_NULLPTR )
447  operator unspecified_bool_type() const
448  { return _dptr; }
449 #else
450  explicit operator bool () const { return _dptr.get() != 0; }
451 #endif
452 
453  const _D & operator*() const
454  { return *_dptr; };
455 
456  const _D * operator->() const
457  { return _dptr.get(); }
458 
459  const _D * get() const
460  { return _dptr.get(); }
461 
462  _D & operator*()
463  { assertUnshared(); return *_dptr; }
464 
465  _D * operator->()
466  { assertUnshared(); return _dptr.get(); }
467 
468  _D * get()
469  { assertUnshared(); return _dptr.get(); }
470 
471  public:
472  bool unique() const
473  { return _Traits().unique( _dptr ); }
474 
475  long use_count() const
476  { return _Traits().use_count( _dptr ); }
477 
479  { return _dptr; }
480 
482  { assertUnshared(); return _dptr; }
483 
485  { return _dptr; }
486 
487  private:
488 
490  {
491  if ( !unique() )
492  _Ptr( rwcowClone( _dptr.get() ) ).swap( _dptr );
493  }
494 
495  private:
497  };
499 
505  template<class _D, class _Ptr>
506  inline std::ostream & operator<<( std::ostream & str, const RWCOW_pointer<_D, _Ptr> & obj )
507  {
508  if ( obj.get() )
509  return str << *obj.get();
510  return str << std::string("NULL");
511  }
512 
514  template<class _D, class _Ptr>
515  inline bool operator==( const RWCOW_pointer<_D, _Ptr> & lhs, const RWCOW_pointer<_D, _Ptr> & rhs )
516  { return( lhs.get() == rhs.get() ); }
518  template<class _D, class _Ptr>
519  inline bool operator==( const RWCOW_pointer<_D, _Ptr> & lhs, const typename _Ptr::_Ptr & rhs )
520  { return( lhs.get() == rhs.get() ); }
522  template<class _D, class _Ptr>
523  inline bool operator==( const typename _Ptr::_Ptr & lhs, const RWCOW_pointer<_D, _Ptr> & rhs )
524  { return( lhs.get() == rhs.get() ); }
526  template<class _D, class _Ptr>
527  inline bool operator==( const RWCOW_pointer<_D, _Ptr> & lhs, const typename _Ptr::_constPtr & rhs )
528  { return( lhs.get() == rhs.get() ); }
530  template<class _D, class _Ptr>
531  inline bool operator==( const typename _Ptr::_constPtr & lhs, const RWCOW_pointer<_D, _Ptr> & rhs )
532  { return( lhs.get() == rhs.get() ); }
533 
535  template<class _D, class _Ptr>
536  inline bool operator!=( const RWCOW_pointer<_D, _Ptr> & lhs, const RWCOW_pointer<_D, _Ptr> & rhs )
537  { return ! ( lhs == rhs ); }
539  template<class _D, class _Ptr>
540  inline bool operator!=( const RWCOW_pointer<_D, _Ptr> & lhs, const typename _Ptr::_Ptr & rhs )
541  { return ! ( lhs == rhs ); }
543  template<class _D, class _Ptr>
544  inline bool operator!=( const typename _Ptr::_Ptr & lhs, const RWCOW_pointer<_D, _Ptr> & rhs )
545  { return ! ( lhs == rhs ); }
547  template<class _D, class _Ptr>
548  inline bool operator!=( const RWCOW_pointer<_D, _Ptr> & lhs, const typename _Ptr::_constPtr & rhs )
549  { return ! ( lhs == rhs ); }
551  template<class _D, class _Ptr>
552  inline bool operator!=( const typename _Ptr::_constPtr & lhs, const RWCOW_pointer<_D, _Ptr> & rhs )
553  { return ! ( lhs == rhs ); }
554 
556 
558 } // namespace ca_mgm
561 
563 #define DEFINE_PTR_TYPE(NAME) \
564 class NAME; \
565 extern void intrusive_ptr_add_ref( const NAME * ); \
566 extern void intrusive_ptr_release( const NAME * ); \
567 typedef ca_mgm::intrusive_ptr<NAME> NAME##_Ptr; \
568 typedef ca_mgm::intrusive_ptr<const NAME> NAME##_constPtr;
569 
571 #endif // CA_MGM_BASE_PTRTYPES_H
bool operator!=(const RW_pointer< _D, _Ptr > &lhs, const RW_pointer< _D, _Ptr > &rhs)
Definition: PtrTypes.hpp:385
bool operator!=(const RWCOW_pointer< _D, _Ptr > &lhs, const typename _Ptr::_Ptr &rhs)
Definition: PtrTypes.hpp:540
void reset()
Definition: PtrTypes.hpp:434
bool unique(const _constPtr &ptr_r)
Definition: PtrTypes.hpp:190
long use_count() const
Definition: PtrTypes.hpp:331
shared_ptr< _D > _Ptr
Definition: PtrTypes.hpp:170
Definition: PtrTypes.hpp:202
RW_pointer(typename _Ptr::element_type *dptr=0)
Definition: PtrTypes.hpp:281
Definition: PtrTypes.hpp:185
bool operator!=(const RWCOW_pointer< _D, _Ptr > &lhs, const RWCOW_pointer< _D, _Ptr > &rhs)
Definition: PtrTypes.hpp:536
bool operator==(const RW_pointer< _D, _Ptr > &lhs, const RW_pointer< _D, _Ptr > &rhs)
Definition: PtrTypes.hpp:363
const _D * get() const
Definition: PtrTypes.hpp:315
_Traits::_Ptr _Ptr
Definition: PtrTypes.hpp:274
_Traits::_constPtr _constPtr
Definition: PtrTypes.hpp:275
bool operator==(const RW_pointer< _D, _Ptr > &lhs, const typename _Ptr::_constPtr &rhs)
Definition: PtrTypes.hpp:375
bool operator!=(const typename _Ptr::_constPtr &lhs, const RW_pointer< _D, _Ptr > &rhs)
Definition: PtrTypes.hpp:401
bool unique(const _Ptr &ptr_r)
Definition: PtrTypes.hpp:192
long use_count(const _constPtr &ptr_r) const
Definition: PtrTypes.hpp:195
const _D * operator->() const
Definition: PtrTypes.hpp:456
long use_count(const _Ptr &ptr_r) const
Definition: PtrTypes.hpp:197
bool operator==(const RWCOW_pointer< _D, _Ptr > &lhs, const RWCOW_pointer< _D, _Ptr > &rhs)
Definition: PtrTypes.hpp:515
bool operator==(const typename _Ptr::_constPtr &lhs, const RW_pointer< _D, _Ptr > &rhs)
Definition: PtrTypes.hpp:379
const _D & operator*() const
Definition: PtrTypes.hpp:309
bool operator!=(const typename _Ptr::_Ptr &lhs, const RW_pointer< _D, _Ptr > &rhs)
Definition: PtrTypes.hpp:393
RW_pointer(_Ptr dptr)
Definition: PtrTypes.hpp:286
_constPtr getPtr() const
Definition: PtrTypes.hpp:478
bool operator==(const typename _Ptr::_Ptr &lhs, const RW_pointer< _D, _Ptr > &rhs)
Definition: PtrTypes.hpp:371
bool unique() const
Definition: PtrTypes.hpp:328
intrusive_ptr< _D > _Ptr
Definition: PtrTypes.hpp:187
_constPtr getPtr() const
Definition: PtrTypes.hpp:334
Definition: PtrTypes.hpp:417
_constPtr cgetPtr()
Definition: PtrTypes.hpp:484
_D & operator*()
Definition: PtrTypes.hpp:462
_Traits::_constPtr _constPtr
Definition: PtrTypes.hpp:420
shared_ptr< const _D > _constPtr
Definition: PtrTypes.hpp:171
void assertUnshared()
Definition: PtrTypes.hpp:489
long use_count(const _Ptr &ptr_r) const
Definition: PtrTypes.hpp:180
void swap(_Ptr &rhs)
Definition: PtrTypes.hpp:443
const _D & operator*() const
Definition: PtrTypes.hpp:453
_Ptr getPtr()
Definition: PtrTypes.hpp:481
Definition: PtrTypes.hpp:272
long use_count(const _constPtr &ptr_r) const
Definition: PtrTypes.hpp:178
bool operator==(const RWCOW_pointer< _D, _Ptr > &lhs, const typename _Ptr::_constPtr &rhs)
Definition: PtrTypes.hpp:527
_Ptr _dptr
Definition: PtrTypes.hpp:496
void operator()(const void *const ) const
Definition: PtrTypes.hpp:75
void swap(RW_pointer &rhs)
Definition: PtrTypes.hpp:296
_Traits::_Ptr _Ptr
Definition: PtrTypes.hpp:419
RWCOW_pointer(typename _Ptr::element_type *dptr=0)
Definition: PtrTypes.hpp:425
_D * operator->()
Definition: PtrTypes.hpp:321
_constPtr cgetPtr()
Definition: PtrTypes.hpp:340
long use_count() const
Definition: PtrTypes.hpp:475
bool operator!=(const RWCOW_pointer< _D, _Ptr > &lhs, const typename _Ptr::_constPtr &rhs)
Definition: PtrTypes.hpp:548
Definition: PtrTypes.hpp:168
bool operator!=(const RW_pointer< _D, _Ptr > &lhs, const typename _Ptr::_constPtr &rhs)
Definition: PtrTypes.hpp:397
bool operator==(const typename _Ptr::_Ptr &lhs, const RWCOW_pointer< _D, _Ptr > &rhs)
Definition: PtrTypes.hpp:523
void reset(typename _Ptr::element_type *dptr)
Definition: PtrTypes.hpp:437
scoped_ptr< const _D > _constPtr
Definition: PtrTypes.hpp:205
void reset()
Definition: PtrTypes.hpp:290
void reset(typename _Ptr::element_type *dptr)
Definition: PtrTypes.hpp:293
Definition: PtrTypes.hpp:73
bool unique() const
Definition: PtrTypes.hpp:472
bool operator==(const typename _Ptr::_constPtr &lhs, const RWCOW_pointer< _D, _Ptr > &rhs)
Definition: PtrTypes.hpp:531
bool operator!=(const typename _Ptr::_constPtr &lhs, const RWCOW_pointer< _D, _Ptr > &rhs)
Definition: PtrTypes.hpp:552
bool unique(const _constPtr &ptr_r)
Definition: PtrTypes.hpp:207
RWCOW_pointer(_Ptr dptr)
Definition: PtrTypes.hpp:430
bool unique(const _Ptr &ptr_r)
Definition: PtrTypes.hpp:209
intrusive_ptr< const _D > _constPtr
Definition: PtrTypes.hpp:188
bool operator!=(const typename _Ptr::_Ptr &lhs, const RWCOW_pointer< _D, _Ptr > &rhs)
Definition: PtrTypes.hpp:544
void swap(RWCOW_pointer &rhs)
Definition: PtrTypes.hpp:440
void swap(_Ptr &rhs)
Definition: PtrTypes.hpp:299
_D * operator->()
Definition: PtrTypes.hpp:465
long use_count(const _Ptr &ptr_r) const
Definition: PtrTypes.hpp:214
bool operator!=(const RW_pointer< _D, _Ptr > &lhs, const typename _Ptr::_Ptr &rhs)
Definition: PtrTypes.hpp:389
bool operator==(const RW_pointer< _D, _Ptr > &lhs, const typename _Ptr::_Ptr &rhs)
Definition: PtrTypes.hpp:367
bool unique(const _constPtr &ptr_r)
Definition: PtrTypes.hpp:173
bool unique(const _Ptr &ptr_r)
Definition: PtrTypes.hpp:175
_D * rwcowClone(const _D *rhs)
Definition: PtrTypes.hpp:227
_Ptr getPtr()
Definition: PtrTypes.hpp:337
long use_count(const _constPtr &ptr_r) const
Definition: PtrTypes.hpp:212
const _D * get() const
Definition: PtrTypes.hpp:459
bool operator==(const RWCOW_pointer< _D, _Ptr > &lhs, const typename _Ptr::_Ptr &rhs)
Definition: PtrTypes.hpp:519
const _D * operator->() const
Definition: PtrTypes.hpp:312
scoped_ptr< _D > _Ptr
Definition: PtrTypes.hpp:204
_D & operator*()
Definition: PtrTypes.hpp:318
_Ptr _dptr
Definition: PtrTypes.hpp:344