libzypp 17.31.23
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 <iosfwd>
17#include <string>
18
19#include <boost/scoped_ptr.hpp>
20#include <boost/shared_ptr.hpp>
21#include <boost/weak_ptr.hpp>
22#include <boost/intrusive_ptr.hpp>
23
25namespace zypp
26{
27
28 namespace str
29 {
30 // printing void* (prevents us from including <ostream>)
31 std::string form( const char * format, ... ) __attribute__ ((format (printf, 1, 2)));
32 }
33
50
82 struct NullDeleter
83 {
84 void operator()( const void *const ) const
85 {}
86 };
87
89 using boost::scoped_ptr;
90
92 using boost::shared_ptr;
93
95 using boost::weak_ptr;
96
98 using boost::intrusive_ptr;
99
101 using boost::static_pointer_cast;
103 using boost::const_pointer_cast;
105 using boost::dynamic_pointer_cast;
106
108} // namespace zypp
111namespace std
112{
113
114 // namespace sub {
115 // class Foo;
116 // typedef zypp::intrusive_ptr<Foo> Foo_Ptr; // see DEFINE_PTR_TYPE(NAME) macro below
117 // }
118
119 // Defined in namespace std g++ finds the output operator (König-Lookup),
120 // even if we typedef the pointer in a different namespace than ::zypp.
121 // Otherwise we had to define an output operator always in the same namespace
122 // as the typedef (else g++ will just print the pointer value).
123
125 template<class D>
126 inline std::ostream & operator<<( std::ostream & str, const zypp::shared_ptr<D> & obj )
127 {
128 if ( obj )
129 return str << *obj;
130 return str << std::string("NULL");
131 }
133 template<>
134 inline std::ostream & operator<<( std::ostream & str, const zypp::shared_ptr<void> & obj )
135 {
136 if ( obj )
137 return str << zypp::str::form( "%p", (void*)obj.get() );
138 return str << std::string("NULL");
139 }
140
142 template<class D>
143 inline std::ostream & dumpOn( std::ostream & str, const zypp::shared_ptr<D> & obj )
144 {
145 if ( obj )
146 return dumpOn( str, *obj );
147 return str << std::string("NULL");
148 }
150 template<>
151 inline std::ostream & dumpOn( std::ostream & str, const zypp::shared_ptr<void> & obj )
152 { return str << obj; }
153
155 template<class D>
156 inline std::ostream & operator<<( std::ostream & str, const zypp::intrusive_ptr<D> & obj )
157 {
158 if ( obj )
159 return str << *obj;
160 return str << std::string("NULL");
161 }
163 template<class D>
164 inline std::ostream & dumpOn( std::ostream & str, const zypp::intrusive_ptr<D> & obj )
165 {
166 if ( obj )
167 return dumpOn( str, *obj );
168 return str << std::string("NULL");
169 }
171} // namespace std
174namespace zypp
175{
176
178 //
179 // RW_pointer traits
180 //
182
187 namespace rw_pointer {
188
189 template<class D>
190 struct Shared
191 {
192 typedef shared_ptr<D> PtrType;
195 bool unique( const constPtrType & ptr_r )
196 { return !ptr_r || ptr_r.unique(); }
197 bool unique( const PtrType & ptr_r )
198 { return !ptr_r || ptr_r.unique(); }
200 long use_count( const constPtrType & ptr_r ) const
201 { return ptr_r.use_count(); }
202 long use_count( const PtrType & ptr_r ) const
203 { return ptr_r.use_count(); }
204 };
205
206 template<class D>
207 struct Intrusive
208 {
212 bool unique( const constPtrType & ptr_r )
213 { return !ptr_r || (ptr_r->refCount() <= 1); }
214 bool unique( const PtrType & ptr_r )
215 { return !ptr_r || (ptr_r->refCount() <= 1); }
217 long use_count( const constPtrType & ptr_r ) const
218 { return ptr_r ? ptr_r->refCount() : 0; }
219 long use_count( const PtrType & ptr_r ) const
220 { return ptr_r ? ptr_r->refCount() : 0; }
221 };
222
223 template<class D>
224 struct Scoped
225 {
226 typedef scoped_ptr<D> PtrType;
229 bool unique( const constPtrType & ptr_r )
230 { return true; }
231 bool unique( const PtrType & ptr_r )
232 { return true; }
234 long use_count( const constPtrType & ptr_r ) const
235 { return ptr_r ? 1 : 0; }
236 long use_count( const PtrType & ptr_r ) const
237 { return ptr_r ? 1 : 0; }
238 };
239
240 }
242
244 //
245 // CLASS NAME : RW_pointer
246 //
284 template<class D, class DTraits = rw_pointer::Shared<D> >
285 struct RW_pointer
286 {
287 typedef typename DTraits::PtrType PtrType;
288 typedef typename DTraits::constPtrType constPtrType;
289
290 RW_pointer()
291 {}
292
293 RW_pointer( std::nullptr_t )
294 {}
295
296 explicit
297 RW_pointer( typename PtrType::element_type * dptr )
298 : _dptr( dptr )
299 {}
300
301 explicit
302 RW_pointer( PtrType dptr )
303 : _dptr( dptr )
304 {}
305
306 RW_pointer & operator=( std::nullptr_t )
307 { reset(); return *this; }
308
309 void reset()
310 { PtrType().swap( _dptr ); }
311
312 void reset( typename PtrType::element_type * dptr )
313 { PtrType( dptr ).swap( _dptr ); }
314
315 void swap( RW_pointer & rhs )
316 { _dptr.swap( rhs._dptr ); }
317
318 void swap( PtrType & rhs )
319 { _dptr.swap( rhs ); }
320
321 explicit operator bool() const
322 { return _dptr.get() != nullptr; }
323
324 const D & operator*() const
325 { return *_dptr; };
326
327 const D * operator->() const
328 { return _dptr.operator->(); }
329
330 const D * get() const
331 { return _dptr.get(); }
332
333 D & operator*()
334 { return *_dptr; }
335
336 D * operator->()
337 { return _dptr.operator->(); }
338
339 D * get()
340 { return _dptr.get(); }
341
342 public:
343 bool unique() const
344 { return DTraits().unique( _dptr ); }
345
346 long use_count() const
347 { return DTraits().use_count( _dptr ); }
348
349 constPtrType getPtr() const
350 { return _dptr; }
351
353 { return _dptr; }
354
356 { return _dptr; }
357
358 private:
360 };
362
368 template<class D, class DPtr>
369 inline std::ostream & operator<<( std::ostream & str, const RW_pointer<D, DPtr> & obj )
370 {
371 if ( obj.get() )
372 return str << *obj.get();
373 return str << std::string("NULL");
374 }
375
377 template<class D, class DPtr>
378 inline bool operator==( const RW_pointer<D, DPtr> & lhs, const RW_pointer<D, DPtr> & rhs )
379 { return( lhs.get() == rhs.get() ); }
381 template<class D, class DPtr>
382 inline bool operator==( const RW_pointer<D, DPtr> & lhs, const typename DPtr::PtrType & rhs )
383 { return( lhs.get() == rhs.get() ); }
385 template<class D, class DPtr>
386 inline bool operator==( const typename DPtr::PtrType & lhs, const RW_pointer<D, DPtr> & rhs )
387 { return( lhs.get() == rhs.get() ); }
389 template<class D, class DPtr>
390 inline bool operator==( const RW_pointer<D, DPtr> & lhs, const typename DPtr::constPtrType & rhs )
391 { return( lhs.get() == rhs.get() ); }
393 template<class D, class DPtr>
394 inline bool operator==( const typename DPtr::constPtrType & lhs, const RW_pointer<D, DPtr> & rhs )
395 { return( lhs.get() == rhs.get() ); }
397 template<class D, class DPtr>
398 inline bool operator==( const RW_pointer<D, DPtr> & lhs, std::nullptr_t )
399 { return( lhs.get() == nullptr ); }
401 template<class D, class DPtr>
402 inline bool operator==( std::nullptr_t, const RW_pointer<D, DPtr> & rhs )
403 { return( nullptr == rhs.get() ); }
404
405
407 template<class D, class DPtr>
408 inline bool operator!=( const RW_pointer<D, DPtr> & lhs, const RW_pointer<D, DPtr> & rhs )
409 { return ! ( lhs == rhs ); }
411 template<class D, class DPtr>
412 inline bool operator!=( const RW_pointer<D, DPtr> & lhs, const typename DPtr::PtrType & rhs )
413 { return ! ( lhs == rhs ); }
415 template<class D, class DPtr>
416 inline bool operator!=( const typename DPtr::PtrType & lhs, const RW_pointer<D, DPtr> & rhs )
417 { return ! ( lhs == rhs ); }
419 template<class D, class DPtr>
420 inline bool operator!=( const RW_pointer<D, DPtr> & lhs, const typename DPtr::constPtrType & rhs )
421 { return ! ( lhs == rhs ); }
423 template<class D, class DPtr>
424 inline bool operator!=( const typename DPtr::constPtrType & lhs, const RW_pointer<D, DPtr> & rhs )
425 { return ! ( lhs == rhs ); }
427 template<class D, class DPtr>
428 inline bool operator!=( const RW_pointer<D, DPtr> & lhs, std::nullptr_t )
429 { return( lhs.get() != nullptr ); }
431 template<class D, class DPtr>
432 inline bool operator!=( std::nullptr_t, const RW_pointer<D, DPtr> & rhs )
433 { return( nullptr != rhs.get() ); }
434
436
442 template<class D>
443 inline D * rwcowClone( const D * rhs )
444 { return rhs->clone(); }
445
447 //
448 // CLASS NAME : RWCOW_pointer
449 //
457 template<class D, class DTraits = rw_pointer::Shared<D> >
458 struct RWCOW_pointer
459 {
460 typedef typename DTraits::PtrType PtrType;
461 typedef typename DTraits::constPtrType constPtrType;
462
464 {}
465
466 RWCOW_pointer( std::nullptr_t )
467 {}
468
469 explicit
470 RWCOW_pointer( typename PtrType::element_type * dptr )
471 : _dptr( dptr )
472 {}
473
474 explicit
475 RWCOW_pointer( PtrType dptr )
476 : _dptr( dptr )
477 {}
478
479 RWCOW_pointer & operator=( std::nullptr_t )
480 { reset(); return *this; }
481
482 void reset()
483 { PtrType().swap( _dptr ); }
484
485 void reset( typename PtrType::element_type * dptr )
486 { PtrType( dptr ).swap( _dptr ); }
487
488 void swap( RWCOW_pointer & rhs )
489 { _dptr.swap( rhs._dptr ); }
490
491 void swap( PtrType & rhs )
492 { _dptr.swap( rhs ); }
493
494 explicit operator bool() const
495 { return _dptr.get() != nullptr; }
496
497 const D & operator*() const
498 { return *_dptr; };
499
500 const D * operator->() const
501 { return _dptr.operator->(); }
502
503 const D * get() const
504 { return _dptr.get(); }
505
506 D & operator*()
507 { assertUnshared(); return *_dptr; }
508
509 D * operator->()
510 { assertUnshared(); return _dptr.operator->(); }
511
512 D * get()
513 { assertUnshared(); return _dptr.get(); }
514
515 public:
516 bool unique() const
517 { return DTraits().unique( _dptr ); }
518
519 long use_count() const
520 { return DTraits().use_count( _dptr ); }
521
522 constPtrType getPtr() const
523 { return _dptr; }
524
526 { assertUnshared(); return _dptr; }
527
529 { return _dptr; }
530
531 private:
532
533 void assertUnshared()
534 {
535 if ( !unique() )
536 PtrType( rwcowClone( _dptr.get() ) ).swap( _dptr );
537 }
538
539 private:
541 };
543
549 template<class D, class DPtr>
550 inline std::ostream & operator<<( std::ostream & str, const RWCOW_pointer<D, DPtr> & obj )
551 {
552 if ( obj.get() )
553 return str << *obj.get();
554 return str << std::string("NULL");
555 }
556
558 template<class D, class DPtr>
559 inline bool operator==( const RWCOW_pointer<D, DPtr> & lhs, const RWCOW_pointer<D, DPtr> & rhs )
560 { return( lhs.get() == rhs.get() ); }
562 template<class D, class DPtr>
563 inline bool operator==( const RWCOW_pointer<D, DPtr> & lhs, const typename DPtr::PtrType & rhs )
564 { return( lhs.get() == rhs.get() ); }
566 template<class D, class DPtr>
567 inline bool operator==( const typename DPtr::PtrType & lhs, const RWCOW_pointer<D, DPtr> & rhs )
568 { return( lhs.get() == rhs.get() ); }
570 template<class D, class DPtr>
571 inline bool operator==( const RWCOW_pointer<D, DPtr> & lhs, const typename DPtr::constPtrType & rhs )
572 { return( lhs.get() == rhs.get() ); }
574 template<class D, class DPtr>
575 inline bool operator==( const typename DPtr::constPtrType & lhs, const RWCOW_pointer<D, DPtr> & rhs )
576 { return( lhs.get() == rhs.get() ); }
578 template<class D, class DPtr>
579 inline bool operator==( const RWCOW_pointer<D, DPtr> & lhs, std::nullptr_t )
580 { return( lhs.get() == nullptr ); }
582 template<class D, class DPtr>
583 inline bool operator==( std::nullptr_t, const RWCOW_pointer<D, DPtr> & rhs )
584 { return( nullptr == rhs.get() ); }
585
587 template<class D, class DPtr>
588 inline bool operator!=( const RWCOW_pointer<D, DPtr> & lhs, const RWCOW_pointer<D, DPtr> & rhs )
589 { return ! ( lhs == rhs ); }
591 template<class D, class DPtr>
592 inline bool operator!=( const RWCOW_pointer<D, DPtr> & lhs, const typename DPtr::PtrType & rhs )
593 { return ! ( lhs == rhs ); }
595 template<class D, class DPtr>
596 inline bool operator!=( const typename DPtr::PtrType & lhs, const RWCOW_pointer<D, DPtr> & rhs )
597 { return ! ( lhs == rhs ); }
599 template<class D, class DPtr>
600 inline bool operator!=( const RWCOW_pointer<D, DPtr> & lhs, const typename DPtr::constPtrType & rhs )
601 { return ! ( lhs == rhs ); }
603 template<class D, class DPtr>
604 inline bool operator!=( const typename DPtr::constPtrType & lhs, const RWCOW_pointer<D, DPtr> & rhs )
605 { return ! ( lhs == rhs ); }
607 template<class D, class DPtr>
608 inline bool operator!=( const RWCOW_pointer<D, DPtr> & lhs, std::nullptr_t )
609 { return( lhs.get() != nullptr ); }
611 template<class D, class DPtr>
612 inline bool operator!=( std::nullptr_t, const RWCOW_pointer<D, DPtr> & rhs )
613 { return( nullptr != rhs.get() ); }
614
616
619} // namespace zypp
621
623#define DEFINE_PTR_TYPE(NAME) \
624class NAME; \
625extern void intrusive_ptr_add_ref( const NAME * ); \
626extern void intrusive_ptr_release( const NAME * ); \
627typedef zypp::intrusive_ptr<NAME> NAME##_Ptr; \
628typedef zypp::intrusive_ptr<const NAME> NAME##_constPtr;
629
631#endif // ZYPP_BASE_PTRTYPES_H
std::ostream & operator<<(std::ostream &str, const zypp::sat::detail::CDataiterator *obj)
Definition: LookupAttr.cc:807
Definition: Arch.h:361
unordered_set< D > * rwcowClone(const std::unordered_set< D > *rhs)
clone function for RW_pointer
Definition: Hash.h:41
String related utilities and Regular expression matching.
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
Definition: String.cc:36
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:2
bool operator==(const SetRelation::Enum &lhs, const SetCompare &rhs)
This is an overloaded member function, provided for convenience. It differs from the above function o...
std::ostream & dumpOn(std::ostream &str, const Capability &obj)
Definition: Capability.cc:580
std::ostream & operator<<(std::ostream &str, const SerialNumber &obj)
Definition: SerialNumber.cc:52
bool operator!=(const SetRelation::Enum &lhs, const SetCompare &rhs)
This is an overloaded member function, provided for convenience. It differs from the above function o...
RWCOW_pointer & operator=(std::nullptr_t)
Definition: PtrTypes.h:479
const D & operator*() const
Definition: PtrTypes.h:497
constPtrType cgetPtr()
Definition: PtrTypes.h:528
const D * operator->() const
Definition: PtrTypes.h:500
const D * get() const
Definition: PtrTypes.h:503
constPtrType getPtr() const
Definition: PtrTypes.h:522
long use_count() const
Definition: PtrTypes.h:519
bool unique() const
Definition: PtrTypes.h:516
D * rwcowClone(const D *rhs)
Clone the underlying object.
Definition: PtrTypes.h:443
DTraits::PtrType PtrType
Definition: PtrTypes.h:460
void swap(RWCOW_pointer &rhs)
Definition: PtrTypes.h:488
DTraits::constPtrType constPtrType
Definition: PtrTypes.h:461
void assertUnshared()
Definition: PtrTypes.h:533
DTraits::constPtrType constPtrType
Definition: PtrTypes.h:288
DTraits::PtrType PtrType
Definition: PtrTypes.h:287
PtrType _dptr
Definition: PtrTypes.h:359
constPtrType cgetPtr()
Definition: PtrTypes.h:355
long use_count() const
Definition: PtrTypes.h:346
const D * operator->() const
Definition: PtrTypes.h:327
void swap(RW_pointer &rhs)
Definition: PtrTypes.h:315
constPtrType getPtr() const
Definition: PtrTypes.h:349
const D & operator*() const
Definition: PtrTypes.h:324
const D * get() const
Definition: PtrTypes.h:330
bool unique() const
Definition: PtrTypes.h:343
RW_pointer & operator=(std::nullptr_t)
Definition: PtrTypes.h:306
intrusive_ptr< D > PtrType
Definition: PtrTypes.h:209
long use_count(const constPtrType &ptr_r) const
Return number of references.
Definition: PtrTypes.h:217
bool unique(const constPtrType &ptr_r)
Check whether pointer is not shared.
Definition: PtrTypes.h:212
intrusive_ptr< const D > constPtrType
Definition: PtrTypes.h:210
scoped_ptr< const D > constPtrType
Definition: PtrTypes.h:227
scoped_ptr< D > PtrType
Definition: PtrTypes.h:226
bool unique(const constPtrType &ptr_r)
Check whether pointer is not shared.
Definition: PtrTypes.h:229
long use_count(const constPtrType &ptr_r) const
Return number of references.
Definition: PtrTypes.h:234
shared_ptr< D > PtrType
Definition: PtrTypes.h:192
shared_ptr< const D > constPtrType
Definition: PtrTypes.h:193
long use_count(const constPtrType &ptr_r) const
Return number of references.
Definition: PtrTypes.h:200
bool unique(const constPtrType &ptr_r)
Check whether pointer is not shared.
Definition: PtrTypes.h:195