libzypp  13.10.6
PtrTypes.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
13 #ifndef ZYPP_BASE_PTRTYPES_H
14 #define ZYPP_BASE_PTRTYPES_H
15 
16 #include <string>
17 
18 #include <boost/scoped_ptr.hpp>
19 #include <boost/shared_ptr.hpp>
20 #include <boost/weak_ptr.hpp>
21 #include <boost/intrusive_ptr.hpp>
22 
24 namespace zypp
25 {
26 
43 
75  struct NullDeleter
76  {
77  void operator()( const void *const ) const
78  {}
79  };
80 
82  using boost::scoped_ptr;
83 
85  using boost::shared_ptr;
86 
88  using boost::weak_ptr;
89 
91  using boost::intrusive_ptr;
92 
94  using boost::static_pointer_cast;
96  using boost::const_pointer_cast;
98  using boost::dynamic_pointer_cast;
99 
101 } // namespace zypp
104 namespace std
105 {
106 
107  // namespace sub {
108  // class Foo;
109  // typedef zypp::intrusive_ptr<Foo> Foo_Ptr; // see DEFINE_PTR_TYPE(NAME) macro below
110  // }
111 
112  // Defined in namespace std g++ finds the output operator (König-Lookup),
113  // even if we typedef the pointer in a different namespace than ::zypp.
114  // Otherwise we had to define an output operator always in the same namespace
115  // as the typedef (else g++ will just print the pointer value).
116 
118  template<class _D>
119  inline std::ostream & operator<<( std::ostream & str, const zypp::shared_ptr<_D> & obj )
120  {
121  if ( obj )
122  return str << *obj;
123  return str << std::string("NULL");
124  }
126  template<class _D>
127  inline std::ostream & dumpOn( std::ostream & str, const zypp::shared_ptr<_D> & obj )
128  {
129  if ( obj )
130  return dumpOn( str, *obj );
131  return str << std::string("NULL");
132  }
133 
135  template<class _D>
136  inline std::ostream & operator<<( std::ostream & str, const zypp::intrusive_ptr<_D> & obj )
137  {
138  if ( obj )
139  return str << *obj;
140  return str << std::string("NULL");
141  }
143  template<class _D>
144  inline std::ostream & dumpOn( std::ostream & str, const zypp::intrusive_ptr<_D> & obj )
145  {
146  if ( obj )
147  return dumpOn( str, *obj );
148  return str << std::string("NULL");
149  }
151 } // namespace std
154 namespace zypp
155 {
156 
158  //
159  // RW_pointer traits
160  //
162 
167  namespace rw_pointer {
168 
169  template<class _D>
170  struct Shared
171  {
172  typedef shared_ptr<_D> _Ptr;
173  typedef shared_ptr<const _D> _constPtr;
175  bool unique( const _constPtr & ptr_r )
176  { return !ptr_r || ptr_r.unique(); }
177  bool unique( const _Ptr & ptr_r )
178  { return !ptr_r || ptr_r.unique(); }
180  long use_count( const _constPtr & ptr_r ) const
181  { return ptr_r.use_count(); }
182  long use_count( const _Ptr & ptr_r ) const
183  { return ptr_r.use_count(); }
184  };
185 
186  template<class _D>
187  struct Intrusive
188  {
189  typedef intrusive_ptr<_D> _Ptr;
190  typedef intrusive_ptr<const _D> _constPtr;
192  bool unique( const _constPtr & ptr_r )
193  { return !ptr_r || (ptr_r->refCount() <= 1); }
194  bool unique( const _Ptr & ptr_r )
195  { return !ptr_r || (ptr_r->refCount() <= 1); }
197  long use_count( const _constPtr & ptr_r ) const
198  { return ptr_r ? ptr_r->refCount() : 0; }
199  long use_count( const _Ptr & ptr_r ) const
200  { return ptr_r ? ptr_r->refCount() : 0; }
201  };
202 
203  template<class _D>
204  struct Scoped
205  {
206  typedef scoped_ptr<_D> _Ptr;
207  typedef scoped_ptr<const _D> _constPtr;
209  bool unique( const _constPtr & ptr_r )
210  { return true; }
211  bool unique( const _Ptr & ptr_r )
212  { return true; }
214  long use_count( const _constPtr & ptr_r ) const
215  { return ptr_r ? 1 : 0; }
216  long use_count( const _Ptr & ptr_r ) const
217  { return ptr_r ? 1 : 0; }
218  };
219 
220  }
222 
224  //
225  // CLASS NAME : RW_pointer
226  //
264  template<class _D, class _Traits = rw_pointer::Shared<_D> >
265  struct RW_pointer
266  {
267  typedef typename _Traits::_Ptr _Ptr;
268  typedef typename _Traits::_constPtr _constPtr;
269 
271  {}
272 
273  RW_pointer( std::nullptr_t )
274  {}
275 
276  explicit
277  RW_pointer( typename _Ptr::element_type * dptr )
278  : _dptr( dptr )
279  {}
280 
281  explicit
282  RW_pointer( _Ptr dptr )
283  : _dptr( dptr )
284  {}
285 
286  RW_pointer & operator=( std::nullptr_t )
287  { reset(); return *this; }
288 
289  void reset()
290  { _Ptr().swap( _dptr ); }
291 
292  void reset( typename _Ptr::element_type * dptr )
293  { _Ptr( dptr ).swap( _dptr ); }
294 
295  void swap( RW_pointer & rhs )
296  { _dptr.swap( rhs._dptr ); }
297 
298  void swap( _Ptr & rhs )
299  { _dptr.swap( rhs ); }
300 
301  explicit operator bool() const
302  { return _dptr.get() != nullptr; }
303 
304  const _D & operator*() const
305  { return *_dptr; };
306 
307  const _D * operator->() const
308  { return _dptr.operator->(); }
309 
310  const _D * get() const
311  { return _dptr.get(); }
312 
313  _D & operator*()
314  { return *_dptr; }
315 
316  _D * operator->()
317  { return _dptr.operator->(); }
318 
319  _D * get()
320  { return _dptr.get(); }
321 
322  public:
323  bool unique() const
324  { return _Traits().unique( _dptr ); }
325 
326  long use_count() const
327  { return _Traits().use_count( _dptr ); }
328 
330  { return _dptr; }
331 
333  { return _dptr; }
334 
336  { return _dptr; }
337 
338  private:
340  };
342 
348  template<class _D, class _Ptr>
349  inline std::ostream & operator<<( std::ostream & str, const RW_pointer<_D, _Ptr> & obj )
350  {
351  if ( obj.get() )
352  return str << *obj.get();
353  return str << std::string("NULL");
354  }
355 
357  template<class _D, class _Ptr>
358  inline bool operator==( const RW_pointer<_D, _Ptr> & lhs, const RW_pointer<_D, _Ptr> & rhs )
359  { return( lhs.get() == rhs.get() ); }
361  template<class _D, class _Ptr>
362  inline bool operator==( const RW_pointer<_D, _Ptr> & lhs, const typename _Ptr::_Ptr & rhs )
363  { return( lhs.get() == rhs.get() ); }
365  template<class _D, class _Ptr>
366  inline bool operator==( const typename _Ptr::_Ptr & lhs, const RW_pointer<_D, _Ptr> & rhs )
367  { return( lhs.get() == rhs.get() ); }
369  template<class _D, class _Ptr>
370  inline bool operator==( const RW_pointer<_D, _Ptr> & lhs, const typename _Ptr::_constPtr & rhs )
371  { return( lhs.get() == rhs.get() ); }
373  template<class _D, class _Ptr>
374  inline bool operator==( const typename _Ptr::_constPtr & lhs, const RW_pointer<_D, _Ptr> & rhs )
375  { return( lhs.get() == rhs.get() ); }
377  template<class _D, class _Ptr>
378  inline bool operator==( const RW_pointer<_D, _Ptr> & lhs, std::nullptr_t )
379  { return( lhs.get() == nullptr ); }
381  template<class _D, class _Ptr>
382  inline bool operator==( std::nullptr_t, const RW_pointer<_D, _Ptr> & rhs )
383  { return( nullptr == rhs.get() ); }
384 
385 
387  template<class _D, class _Ptr>
388  inline bool operator!=( const RW_pointer<_D, _Ptr> & lhs, const RW_pointer<_D, _Ptr> & rhs )
389  { return ! ( lhs == rhs ); }
391  template<class _D, class _Ptr>
392  inline bool operator!=( const RW_pointer<_D, _Ptr> & lhs, const typename _Ptr::_Ptr & rhs )
393  { return ! ( lhs == rhs ); }
395  template<class _D, class _Ptr>
396  inline bool operator!=( const typename _Ptr::_Ptr & lhs, const RW_pointer<_D, _Ptr> & rhs )
397  { return ! ( lhs == rhs ); }
399  template<class _D, class _Ptr>
400  inline bool operator!=( const RW_pointer<_D, _Ptr> & lhs, const typename _Ptr::_constPtr & rhs )
401  { return ! ( lhs == rhs ); }
403  template<class _D, class _Ptr>
404  inline bool operator!=( const typename _Ptr::_constPtr & lhs, const RW_pointer<_D, _Ptr> & rhs )
405  { return ! ( lhs == rhs ); }
407  template<class _D, class _Ptr>
408  inline bool operator!=( const RW_pointer<_D, _Ptr> & lhs, std::nullptr_t )
409  { return( lhs.get() != nullptr ); }
411  template<class _D, class _Ptr>
412  inline bool operator!=( std::nullptr_t, const RW_pointer<_D, _Ptr> & rhs )
413  { return( nullptr != rhs.get() ); }
414 
416 
422  template<class _D>
423  inline _D * rwcowClone( const _D * rhs )
424  { return rhs->clone(); }
425 
427  //
428  // CLASS NAME : RWCOW_pointer
429  //
437  template<class _D, class _Traits = rw_pointer::Shared<_D> >
439  {
440  typedef typename _Traits::_Ptr _Ptr;
441  typedef typename _Traits::_constPtr _constPtr;
442 
444  {}
445 
446  RWCOW_pointer( std::nullptr_t )
447  {}
448 
449  explicit
450  RWCOW_pointer( typename _Ptr::element_type * dptr )
451  : _dptr( dptr )
452  {}
453 
454  explicit
456  : _dptr( dptr )
457  {}
458 
459  RWCOW_pointer & operator=( std::nullptr_t )
460  { reset(); return *this; }
461 
462  void reset()
463  { _Ptr().swap( _dptr ); }
464 
465  void reset( typename _Ptr::element_type * dptr )
466  { _Ptr( dptr ).swap( _dptr ); }
467 
468  void swap( RWCOW_pointer & rhs )
469  { _dptr.swap( rhs._dptr ); }
470 
471  void swap( _Ptr & rhs )
472  { _dptr.swap( rhs ); }
473 
474  explicit operator bool() const
475  { return _dptr.get() != nullptr; }
476 
477  const _D & operator*() const
478  { return *_dptr; };
479 
480  const _D * operator->() const
481  { return _dptr.operator->(); }
482 
483  const _D * get() const
484  { return _dptr.get(); }
485 
486  _D & operator*()
487  { assertUnshared(); return *_dptr; }
488 
489  _D * operator->()
490  { assertUnshared(); return _dptr.operator->(); }
491 
492  _D * get()
493  { assertUnshared(); return _dptr.get(); }
494 
495  public:
496  bool unique() const
497  { return _Traits().unique( _dptr ); }
498 
499  long use_count() const
500  { return _Traits().use_count( _dptr ); }
501 
503  { return _dptr; }
504 
506  { assertUnshared(); return _dptr; }
507 
509  { return _dptr; }
510 
511  private:
512 
514  {
515  if ( !unique() )
516  _Ptr( rwcowClone( _dptr.get() ) ).swap( _dptr );
517  }
518 
519  private:
521  };
523 
529  template<class _D, class _Ptr>
530  inline std::ostream & operator<<( std::ostream & str, const RWCOW_pointer<_D, _Ptr> & obj )
531  {
532  if ( obj.get() )
533  return str << *obj.get();
534  return str << std::string("NULL");
535  }
536 
538  template<class _D, class _Ptr>
539  inline bool operator==( const RWCOW_pointer<_D, _Ptr> & lhs, const RWCOW_pointer<_D, _Ptr> & rhs )
540  { return( lhs.get() == rhs.get() ); }
542  template<class _D, class _Ptr>
543  inline bool operator==( const RWCOW_pointer<_D, _Ptr> & lhs, const typename _Ptr::_Ptr & rhs )
544  { return( lhs.get() == rhs.get() ); }
546  template<class _D, class _Ptr>
547  inline bool operator==( const typename _Ptr::_Ptr & lhs, const RWCOW_pointer<_D, _Ptr> & rhs )
548  { return( lhs.get() == rhs.get() ); }
550  template<class _D, class _Ptr>
551  inline bool operator==( const RWCOW_pointer<_D, _Ptr> & lhs, const typename _Ptr::_constPtr & rhs )
552  { return( lhs.get() == rhs.get() ); }
554  template<class _D, class _Ptr>
555  inline bool operator==( const typename _Ptr::_constPtr & lhs, const RWCOW_pointer<_D, _Ptr> & rhs )
556  { return( lhs.get() == rhs.get() ); }
558  template<class _D, class _Ptr>
559  inline bool operator==( const RWCOW_pointer<_D, _Ptr> & lhs, std::nullptr_t )
560  { return( lhs.get() == nullptr ); }
562  template<class _D, class _Ptr>
563  inline bool operator==( std::nullptr_t, const RWCOW_pointer<_D, _Ptr> & rhs )
564  { return( nullptr == rhs.get() ); }
565 
567  template<class _D, class _Ptr>
568  inline bool operator!=( const RWCOW_pointer<_D, _Ptr> & lhs, const RWCOW_pointer<_D, _Ptr> & rhs )
569  { return ! ( lhs == rhs ); }
571  template<class _D, class _Ptr>
572  inline bool operator!=( const RWCOW_pointer<_D, _Ptr> & lhs, const typename _Ptr::_Ptr & rhs )
573  { return ! ( lhs == rhs ); }
575  template<class _D, class _Ptr>
576  inline bool operator!=( const typename _Ptr::_Ptr & lhs, const RWCOW_pointer<_D, _Ptr> & rhs )
577  { return ! ( lhs == rhs ); }
579  template<class _D, class _Ptr>
580  inline bool operator!=( const RWCOW_pointer<_D, _Ptr> & lhs, const typename _Ptr::_constPtr & rhs )
581  { return ! ( lhs == rhs ); }
583  template<class _D, class _Ptr>
584  inline bool operator!=( const typename _Ptr::_constPtr & lhs, const RWCOW_pointer<_D, _Ptr> & rhs )
585  { return ! ( lhs == rhs ); }
587  template<class _D, class _Ptr>
588  inline bool operator!=( const RWCOW_pointer<_D, _Ptr> & lhs, std::nullptr_t )
589  { return( lhs.get() != nullptr ); }
591  template<class _D, class _Ptr>
592  inline bool operator!=( std::nullptr_t, const RWCOW_pointer<_D, _Ptr> & rhs )
593  { return( nullptr != rhs.get() ); }
594 
596 
598 } // namespace zypp
601 
603 #define DEFINE_PTR_TYPE(NAME) \
604 class NAME; \
605 extern void intrusive_ptr_add_ref( const NAME * ); \
606 extern void intrusive_ptr_release( const NAME * ); \
607 typedef zypp::intrusive_ptr<NAME> NAME##_Ptr; \
608 typedef zypp::intrusive_ptr<const NAME> NAME##_constPtr;
609 
611 #endif // ZYPP_BASE_PTRTYPES_H
bool operator!=(const RW_pointer< _D, _Ptr > &lhs, const RW_pointer< _D, _Ptr > &rhs)
Definition: PtrTypes.h:388
bool operator!=(const RWCOW_pointer< _D, _Ptr > &lhs, const typename _Ptr::_Ptr &rhs)
Definition: PtrTypes.h:572
long use_count() const
Definition: PtrTypes.h:499
bool operator!=(const RWCOW_pointer< _D, _Ptr > &lhs, const RWCOW_pointer< _D, _Ptr > &rhs)
Definition: PtrTypes.h:568
bool operator==(const RW_pointer< _D, _Ptr > &lhs, const RW_pointer< _D, _Ptr > &rhs)
Definition: PtrTypes.h:358
_Traits::_Ptr _Ptr
Definition: PtrTypes.h:440
_constPtr getPtr() const
Definition: PtrTypes.h:502
bool unique(const _Ptr &ptr_r)
Definition: PtrTypes.h:211
RWCOW_pointer & operator=(std::nullptr_t)
Definition: PtrTypes.h:459
bool operator==(const RW_pointer< _D, _Ptr > &lhs, const typename _Ptr::_constPtr &rhs)
Definition: PtrTypes.h:370
bool operator!=(const typename _Ptr::_constPtr &lhs, const RW_pointer< _D, _Ptr > &rhs)
Definition: PtrTypes.h:404
bool operator==(const RWCOW_pointer< _D, _Ptr > &lhs, const RWCOW_pointer< _D, _Ptr > &rhs)
Definition: PtrTypes.h:539
bool operator==(const typename _Ptr::_constPtr &lhs, const RW_pointer< _D, _Ptr > &rhs)
Definition: PtrTypes.h:374
long use_count(const _constPtr &ptr_r) const
Return number of references.
Definition: PtrTypes.h:214
void swap(_Ptr &rhs)
Definition: PtrTypes.h:471
bool operator!=(const typename _Ptr::_Ptr &lhs, const RW_pointer< _D, _Ptr > &rhs)
Definition: PtrTypes.h:396
long use_count() const
Definition: PtrTypes.h:326
const _D & operator*() const
Definition: PtrTypes.h:304
RW_pointer(std::nullptr_t)
Definition: PtrTypes.h:273
bool unique(const _constPtr &ptr_r)
Check whether pointer is not shared.
Definition: PtrTypes.h:209
bool operator==(const typename _Ptr::_Ptr &lhs, const RW_pointer< _D, _Ptr > &rhs)
Definition: PtrTypes.h:366
bool unique(const _constPtr &ptr_r)
Check whether pointer is not shared.
Definition: PtrTypes.h:192
bool operator!=(const RW_pointer< _D, _Ptr > &lhs, std::nullptr_t)
Definition: PtrTypes.h:408
_constPtr getPtr() const
Definition: PtrTypes.h:329
RW_pointer(_Ptr dptr)
Definition: PtrTypes.h:282
void reset(typename _Ptr::element_type *dptr)
Definition: PtrTypes.h:465
void reset(typename _Ptr::element_type *dptr)
Definition: PtrTypes.h:292
bool operator==(const RWCOW_pointer< _D, _Ptr > &lhs, const typename _Ptr::_constPtr &rhs)
Definition: PtrTypes.h:551
void swap(_Ptr &rhs)
Definition: PtrTypes.h:298
intrusive_ptr< _D > _Ptr
Definition: PtrTypes.h:189
const _D * get() const
Definition: PtrTypes.h:483
_Traits::_constPtr _constPtr
Definition: PtrTypes.h:441
RW_pointer & operator=(std::nullptr_t)
Definition: PtrTypes.h:286
_Traits::_Ptr _Ptr
Definition: PtrTypes.h:267
RW_pointer(typename _Ptr::element_type *dptr)
Definition: PtrTypes.h:277
bool unique() const
Definition: PtrTypes.h:496
bool operator==(std::nullptr_t, const RWCOW_pointer< _D, _Ptr > &rhs)
Definition: PtrTypes.h:563
bool operator!=(const RWCOW_pointer< _D, _Ptr > &lhs, const typename _Ptr::_constPtr &rhs)
Definition: PtrTypes.h:580
bool unique(const _Ptr &ptr_r)
Definition: PtrTypes.h:177
bool unique(const _constPtr &ptr_r)
Check whether pointer is not shared.
Definition: PtrTypes.h:175
long use_count(const _Ptr &ptr_r) const
Definition: PtrTypes.h:199
#define nullptr
Definition: Easy.h:54
bool operator!=(const RW_pointer< _D, _Ptr > &lhs, const typename _Ptr::_constPtr &rhs)
Definition: PtrTypes.h:400
std::ostream & dumpOn(std::ostream &str, const Capability &obj)
Definition: Capability.cc:435
void assertUnshared()
Definition: PtrTypes.h:513
bool operator==(const typename _Ptr::_Ptr &lhs, const RWCOW_pointer< _D, _Ptr > &rhs)
Definition: PtrTypes.h:547
_constPtr cgetPtr()
Definition: PtrTypes.h:508
bool operator!=(std::nullptr_t, const RW_pointer< _D, _Ptr > &rhs)
Definition: PtrTypes.h:412
long use_count(const _constPtr &ptr_r) const
Return number of references.
Definition: PtrTypes.h:180
shared_ptr< const _D > _constPtr
Definition: PtrTypes.h:173
bool unique() const
Definition: PtrTypes.h:323
const _D & operator*() const
Definition: PtrTypes.h:477
RWCOW_pointer(std::nullptr_t)
Definition: PtrTypes.h:446
scoped_ptr< _D > _Ptr
Definition: PtrTypes.h:206
bool operator==(const typename _Ptr::_constPtr &lhs, const RWCOW_pointer< _D, _Ptr > &rhs)
Definition: PtrTypes.h:555
bool operator!=(const typename _Ptr::_constPtr &lhs, const RWCOW_pointer< _D, _Ptr > &rhs)
Definition: PtrTypes.h:584
shared_ptr custom deleter doing nothing.
Definition: PtrTypes.h:75
const _D * get() const
Definition: PtrTypes.h:310
Wrapper for const correct access via Smart pointer types.
Definition: PtrTypes.h:265
bool operator==(const RWCOW_pointer< _D, _Ptr > &lhs, std::nullptr_t)
Definition: PtrTypes.h:559
void swap(RW_pointer &rhs)
Definition: PtrTypes.h:295
bool operator!=(const RWCOW_pointer< _D, _Ptr > &lhs, std::nullptr_t)
Definition: PtrTypes.h:588
long use_count(const _Ptr &ptr_r) const
Definition: PtrTypes.h:216
void swap(RWCOW_pointer &rhs)
Definition: PtrTypes.h:468
scoped_ptr< const _D > _constPtr
Definition: PtrTypes.h:207
_Traits::_constPtr _constPtr
Definition: PtrTypes.h:268
const _D * operator->() const
Definition: PtrTypes.h:480
bool operator!=(const typename _Ptr::_Ptr &lhs, const RWCOW_pointer< _D, _Ptr > &rhs)
Definition: PtrTypes.h:576
long use_count(const _Ptr &ptr_r) const
Definition: PtrTypes.h:182
shared_ptr< _D > _Ptr
Definition: PtrTypes.h:172
const _D * operator->() const
Definition: PtrTypes.h:307
_D * operator->()
Definition: PtrTypes.h:316
intrusive_ptr< const _D > _constPtr
Definition: PtrTypes.h:190
bool operator!=(const RW_pointer< _D, _Ptr > &lhs, const typename _Ptr::_Ptr &rhs)
Definition: PtrTypes.h:392
bool operator==(const RW_pointer< _D, _Ptr > &lhs, const typename _Ptr::_Ptr &rhs)
Definition: PtrTypes.h:362
_constPtr cgetPtr()
Definition: PtrTypes.h:335
bool unique(const _Ptr &ptr_r)
Definition: PtrTypes.h:194
long use_count(const _constPtr &ptr_r) const
Return number of references.
Definition: PtrTypes.h:197
bool operator==(std::nullptr_t, const RW_pointer< _D, _Ptr > &rhs)
Definition: PtrTypes.h:382
bool operator!=(std::nullptr_t, const RWCOW_pointer< _D, _Ptr > &rhs)
Definition: PtrTypes.h:592
bool operator==(const RW_pointer< _D, _Ptr > &lhs, std::nullptr_t)
Definition: PtrTypes.h:378
_D * rwcowClone(const _D *rhs)
Definition: PtrTypes.h:423
RWCOW_pointer(_Ptr dptr)
Definition: PtrTypes.h:455
bool operator==(const RWCOW_pointer< _D, _Ptr > &lhs, const typename _Ptr::_Ptr &rhs)
Definition: PtrTypes.h:543
_D & operator*()
Definition: PtrTypes.h:313
RW_pointer supporting &#39;copy on write&#39; functionality.
Definition: PtrTypes.h:438
RWCOW_pointer(typename _Ptr::element_type *dptr)
Definition: PtrTypes.h:450
void operator()(const void *const ) const
Definition: PtrTypes.h:77