All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SharedPtr.h
Go to the documentation of this file.
1 //=================================================================================================
20 //=================================================================================================
21 
22 #ifndef _BLAZE_UTIL_SMARTPTR_SHAREDPTR_H_
23 #define _BLAZE_UTIL_SMARTPTR_SHAREDPTR_H_
24 
25 
26 //*************************************************************************************************
27 // Includes
28 //*************************************************************************************************
29 
30 #include <algorithm>
31 #include <blaze/util/Assert.h>
33 #include <blaze/util/NonCopyable.h>
34 #include <blaze/util/Null.h>
36 #include <blaze/util/Types.h>
38 
39 
40 namespace blaze {
41 
42 //=================================================================================================
43 //
44 // CLASS SHAREDCOUNTBASE
45 //
46 //=================================================================================================
47 
48 //*************************************************************************************************
57 {
58  public:
59  //**Constructors********************************************************************************
62  explicit inline SharedCountBase();
64  //**********************************************************************************************
65 
66  //**Destructor**********************************************************************************
69  virtual inline ~SharedCountBase();
71  //**********************************************************************************************
72 
73  //**Counting functions**************************************************************************
76  inline void addReference();
77  inline void release();
79  //**********************************************************************************************
80 
81  protected:
82  //**Utility functions***************************************************************************
85  virtual void destroy() = 0;
87  //**********************************************************************************************
88 
89  private:
90  //**Member variables****************************************************************************
93  std::size_t counter_;
94 
95  //**********************************************************************************************
96 };
97 //*************************************************************************************************
98 
99 
100 //*************************************************************************************************
106  : counter_( 1 ) // Reference counter
107 {}
108 //*************************************************************************************************
109 
110 
111 //*************************************************************************************************
115 {}
116 //*************************************************************************************************
117 
118 
119 //*************************************************************************************************
125 {
126  ++counter_;
127 }
128 //*************************************************************************************************
129 
130 
131 //*************************************************************************************************
140 {
141  if( --counter_ == 0 ) {
142  destroy();
143  delete this;
144  }
145 }
146 //*************************************************************************************************
147 
148 
149 
150 
151 //=================================================================================================
152 //
153 // CLASS SHAREDCOUNTIMPL
154 //
155 //=================================================================================================
156 
157 //*************************************************************************************************
165 template< typename Type >
167 {
168  public:
169  //**Constructors********************************************************************************
172  explicit inline SharedCountImpl( Type* ptr );
174  //**********************************************************************************************
175 
176  //**Destructor**********************************************************************************
177  // No explicitly declared destructor.
178  //**********************************************************************************************
179 
180  private:
181  //**Utility functions***************************************************************************
184  virtual void destroy();
186  //**********************************************************************************************
187 
188  //**Member variables****************************************************************************
191  Type* ptr_;
192 
193  //**********************************************************************************************
194 };
195 //*************************************************************************************************
196 
197 
198 //*************************************************************************************************
203 template< typename Type >
205  : SharedCountBase() // Initializing the base object
206  , ptr_( ptr ) // The bound memory resource
207 {}
208 //*************************************************************************************************
209 
210 
211 //*************************************************************************************************
218 template< typename Type >
220 {
221  delete ptr_;
222 }
223 //*************************************************************************************************
224 
225 
226 
227 
228 //=================================================================================================
229 //
230 // CLASS REFCOUNT
231 //
232 //=================================================================================================
233 
234 //*************************************************************************************************
240 class RefCount
241 {
242  public:
243  //**Constructors********************************************************************************
244  explicit inline RefCount();
245 
246  template< typename Type >
247  explicit inline RefCount( Type* ptr );
248 
249  inline RefCount( const RefCount& count );
250  //**********************************************************************************************
251 
252  //**Destructor**********************************************************************************
253  inline ~RefCount();
254  //**********************************************************************************************
255 
256  //**Copy assignment operator********************************************************************
257  inline RefCount& operator=( const RefCount& count );
258  //**********************************************************************************************
259 
260  //**Utility functions***************************************************************************
263  inline void swap( RefCount& count ) throw();
265  //**********************************************************************************************
266 
267  private:
268  //**Member variables****************************************************************************
272 
273  //**********************************************************************************************
274 };
275 //*************************************************************************************************
276 
277 
278 //*************************************************************************************************
282  : counter_( NULL ) // The shared reference counter
283 {}
284 //*************************************************************************************************
285 
286 
287 //*************************************************************************************************
290 template< typename Type >
291 inline RefCount::RefCount( Type* ptr )
292  : counter_( new SharedCountImpl<Type>( ptr ) ) // The shared reference counter
293 {}
294 //*************************************************************************************************
295 
296 
297 //*************************************************************************************************
302 inline RefCount::RefCount( const RefCount& count )
303  : counter_(count.counter_) // The shared reference counter
304 {
305  if( counter_ != NULL )
307 }
308 //*************************************************************************************************
309 
310 
311 //*************************************************************************************************
315 {
316  if( counter_ != NULL )
317  counter_->release();
318 }
319 //*************************************************************************************************
320 
321 
322 //*************************************************************************************************
328 inline RefCount& RefCount::operator=( const RefCount& count )
329 {
330  SharedCountBase* tmp( count.counter_ );
331 
332  if( tmp != counter_ )
333  {
334  if( tmp != NULL )
335  tmp->addReference();
336  if( counter_ != NULL )
337  counter_->release();
338  counter_ = count.counter_;
339  }
340  return *this;
341 }
342 //*************************************************************************************************
343 
344 
345 //*************************************************************************************************
352 inline void RefCount::swap( RefCount& count ) throw()
353 {
354  std::swap( counter_, count.counter_ );
355 }
356 //*************************************************************************************************
357 
358 
359 
360 
361 //=================================================================================================
362 //
363 // CLASS SHAREDPTR
364 //
365 //=================================================================================================
366 
367 //*************************************************************************************************
375 template< typename Type >
377 {
378  private:
379  //**Friend declarations*************************************************************************
381  template< typename Other > friend class SharedPtr;
383  //**********************************************************************************************
384 
385  public:
386  //**Type definitions****************************************************************************
387  typedef Type ElementType;
388  typedef Type* Pointer;
389  typedef Type& Reference;
390  //**********************************************************************************************
391 
392  //**Constructors********************************************************************************
393  inline SharedPtr();
394 
395  template< typename Other >
396  inline SharedPtr( Other* ptr );
397 
398  inline SharedPtr( const SharedPtr& ptr );
399 
400  template< typename Other >
401  inline SharedPtr( const SharedPtr<Other>& ptr );
402  //**********************************************************************************************
403 
404  //**Destructor**********************************************************************************
405  inline ~SharedPtr();
406  //**********************************************************************************************
407 
408  //**Assignment operators************************************************************************
409  inline SharedPtr& operator=( const SharedPtr& ptr );
410 
411  template< typename Other >
412  inline SharedPtr& operator=( const SharedPtr<Other>& ptr );
413  //**********************************************************************************************
414 
415  //**Access operators****************************************************************************
418  inline Reference operator* () const;
419  inline Pointer operator->() const;
421  //**********************************************************************************************
422 
423  //**Utility functions***************************************************************************
426  inline void swap( SharedPtr& ptr ) throw();
428  inline void reset();
429  inline Pointer get() const;
432  //**********************************************************************************************
433 
434  private:
435  //**Member variables****************************************************************************
440 
441  //**********************************************************************************************
442 };
443 
444 
445 
446 
447 //=================================================================================================
448 //
449 // CONSTRUCTORS
450 //
451 //=================================================================================================
452 
453 //*************************************************************************************************
458 template< typename Type >
460  : ptr_(NULL) // Pointer to the bound object
461  , counter_() // Reference counter
462 {}
463 //*************************************************************************************************
464 
465 
466 //*************************************************************************************************
475 template< typename Type >
476 template< typename Other >
477 inline SharedPtr<Type>::SharedPtr( Other* ptr )
478  : ptr_(ptr) // Pointer to the bound object
479  , counter_(ptr) // Reference counter
480 {}
481 //*************************************************************************************************
482 
483 
484 //*************************************************************************************************
492 template< typename Type >
494  : ptr_(ptr.ptr_) // Pointer to the bound object
495  , counter_(ptr.counter_) // Reference counter
496 {}
497 //*************************************************************************************************
498 
499 
500 //*************************************************************************************************
508 template< typename Type >
509 template< typename Other >
511  : ptr_(ptr.ptr_) // Pointer to the bound object
512  , counter_(ptr.counter_) // Reference counter
513 {}
514 //*************************************************************************************************
515 
516 
517 
518 
519 //=================================================================================================
520 //
521 // DESTRUCTOR
522 //
523 //=================================================================================================
524 
525 //*************************************************************************************************
528 template< typename Type >
530 {}
531 //*************************************************************************************************
532 
533 
534 
535 
536 //=================================================================================================
537 //
538 // ASSIGNMENT OPERATORS
539 //
540 //=================================================================================================
541 
542 //*************************************************************************************************
552 template< typename Type >
554 {
555  ptr_ = ptr.ptr_;
556  counter_ = ptr.counter_;
557  return *this;
558 }
559 //*************************************************************************************************
560 
561 
562 //*************************************************************************************************
572 template< typename Type >
573 template< typename Other >
575 {
576  ptr_ = ptr.ptr_;
577  counter_ = ptr.counter_;
578  return *this;
579 }
580 //*************************************************************************************************
581 
582 
583 
584 
585 //=================================================================================================
586 //
587 // ACCESS OPERATORS
588 //
589 //=================================================================================================
590 
591 //*************************************************************************************************
596 template< typename Type >
598 {
599  pe_USER_ASSERT( ptr_, "Uninitialized shared pointer" );
600  return *ptr_;
601 }
602 //*************************************************************************************************
603 
604 
605 //*************************************************************************************************
610 template< typename Type >
612 {
613  pe_USER_ASSERT( ptr_, "Uninitialized shared pointer" );
614  return ptr_;
615 }
616 //*************************************************************************************************
617 
618 
619 
620 
621 //=================================================================================================
622 //
623 // UTILITY FUNCTIONS
624 //
625 //=================================================================================================
626 
627 //*************************************************************************************************
634 template< typename Type >
635 inline void SharedPtr<Type>::swap( SharedPtr& ptr ) throw()
636 {
637  std::swap( ptr_, ptr.ptr_ );
638  counter_.swap( ptr.counter_ );
639 }
640 //*************************************************************************************************
641 
642 
643 //*************************************************************************************************
652 template< typename Type >
653 inline void SharedPtr<Type>::reset()
654 {
655  SharedPtr().swap( *this );
656 }
658 //*************************************************************************************************
659 
660 
661 //*************************************************************************************************
667 template< typename Type >
668 inline typename SharedPtr<Type>::Pointer SharedPtr<Type>::get() const
669 {
670  return ptr_;
671 }
673 //*************************************************************************************************
674 
675 
676 
677 
678 //=================================================================================================
679 //
680 // GLOBAL OPERATORS
681 //
682 //=================================================================================================
683 
684 //*************************************************************************************************
687 template< typename L, typename R >
688 inline bool operator==( const SharedPtr<L>& lhs, const SharedPtr<R>& rhs );
689 
690 template< typename Type >
691 inline bool operator==( const SharedPtr<Type>& ptr, const Null& null );
692 
693 template< typename Type >
694 inline bool operator==( const Null& null, const SharedPtr<Type>& ptr );
695 
696 template< typename L, typename R >
697 inline bool operator!=( const SharedPtr<L>& lhs, const SharedPtr<R>& rhs );
698 
699 template< typename Type >
700 inline bool operator!=( const SharedPtr<Type>& ptr, const Null& null );
701 
702 template< typename Type >
703 inline bool operator!=( const Null& null, const SharedPtr<Type>& ptr );
704 
705 template< typename L, typename R >
706 inline bool operator<( const SharedPtr<L>& lhs, const SharedPtr<R>& rhs );
707 
708 template< typename L, typename R >
709 inline bool operator>( const SharedPtr<L>& lhs, const SharedPtr<R>& rhs );
710 
711 template< typename Type >
712 inline void swap( SharedPtr<Type>& a, SharedPtr<Type>& b ) throw();
714 //*************************************************************************************************
715 
716 
717 //*************************************************************************************************
724 template< typename L, typename R >
725 inline bool operator==( const SharedPtr<L>& lhs, const SharedPtr<R>& rhs )
726 {
727  return lhs.get() == rhs.get();
728 }
729 //*************************************************************************************************
730 
731 
732 //*************************************************************************************************
739 template< typename Type >
740 inline bool operator==( const SharedPtr<Type>& ptr, const Null& null )
741 {
742  return ptr.get() == null;
743 }
744 //*************************************************************************************************
745 
746 
747 //*************************************************************************************************
754 template< typename Type >
755 inline bool operator==( const Null& null, const SharedPtr<Type>& ptr )
756 {
757  return null == ptr.get();
758 }
759 //*************************************************************************************************
760 
761 
762 //*************************************************************************************************
769 template< typename L, typename R >
770 inline bool operator!=( const SharedPtr<L>& lhs, const SharedPtr<R>& rhs )
771 {
772  return lhs.get() != rhs.get();
773 }
774 //*************************************************************************************************
775 
776 
777 //*************************************************************************************************
784 template< typename Type >
785 inline bool operator!=( const SharedPtr<Type>& ptr, const Null& null )
786 {
787  return ptr.get() != null;
788 }
789 //*************************************************************************************************
790 
791 
792 //*************************************************************************************************
799 template< typename Type >
800 inline bool operator!=( const Null& null, const SharedPtr<Type>& ptr )
801 {
802  return null != ptr.get();
803 }
804 //*************************************************************************************************
805 
806 
807 //*************************************************************************************************
814 template< typename L, typename R >
815 inline bool operator<( const SharedPtr<L>& lhs, const SharedPtr<R>& rhs )
816 {
817  return lhs.get() < rhs.get();
818 }
819 //*************************************************************************************************
820 
821 
822 //*************************************************************************************************
829 template< typename L, typename R >
830 inline bool operator>( const SharedPtr<L>& lhs, const SharedPtr<R>& rhs )
831 {
832  return lhs.get() > rhs.get();
833 }
834 //*************************************************************************************************
835 
836 
837 //*************************************************************************************************
845 template< typename Type >
846 inline void swap( SharedPtr<Type>& a, SharedPtr<Type>& b ) throw()
847 {
848  a.swap( b );
849 }
850 //*************************************************************************************************
851 
852 } // namespace blaze
853 
854 #endif