GLI  0.5.0
cow_ptr.hpp
Go to the documentation of this file.
1 
29 #ifndef GLI_COW_PTR_INCLUDED
30 #define GLI_COW_PTR_INCLUDED
31 
32 #include "shared_ptr.hpp"
33 
34 namespace gli
35 {
36  template <typename T>
37  class cow_ptr
38  {
39  public:
40  cow_ptr(T* Pointer) :
41  SharedPtr(Pointer)
42  {}
43 
44  cow_ptr(shared_ptr<T> const & SharedPtr) :
45  SharedPtr(SharedPtr)
46  {}
47 
48  cow_ptr(cow_ptr<T> const & CowPtr) :
49  SharedPtr(CowPtr.Pointer)
50  {}
51 
52  cow_ptr & operator=(cow_ptr<T> const & CowPtr)
53  {
54  // no need to check for self-assignment with shared_ptr
55 
56  this->SharedPtr = CowPtr.SharedPtr;
57  return *this;
58  }
59 
60  T const & operator*() const
61  {
62  return *this->SharedPtr;
63  }
64 
65  T & operator*()
66  {
67  this->detach();
68  return *this->SharedPtr;
69  }
70 
71  T const * operator->() const
72  {
73  return this->SharedPtr.operator->();
74  }
75 
76  T * operator->()
77  {
78  this->detach();
79  return this->SharedPtr.operator->();
80  }
81 
82  private:
83  void detach()
84  {
85  T* Tmp = this->SharedPtr.get();
86  if(!(Tmp == 0 || this->SharedPtr.unique()))
87  this->SharedPtr = cow_ptr<T>(new T(*Tmp));
88  }
89 
90  shared_ptr<T> SharedPtr;
91  };
92 }//namespace gli
93 
94 #endif//GLI_COW_PTR_INCLUDED
T * operator->()
Definition: cow_ptr.hpp:76
cow_ptr(T *Pointer)
Definition: cow_ptr.hpp:40
T const & operator*() const
Definition: cow_ptr.hpp:60
OpenGL Image (gli.g-truc.net)
T & operator*()
Definition: cow_ptr.hpp:65
cow_ptr(shared_ptr< T > const &SharedPtr)
Definition: cow_ptr.hpp:44
T const * operator->() const
Definition: cow_ptr.hpp:71
cow_ptr & operator=(cow_ptr< T > const &CowPtr)
Definition: cow_ptr.hpp:52
cow_ptr(cow_ptr< T > const &CowPtr)
Definition: cow_ptr.hpp:48