DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
CowPtr.h
1 
17 #pragma once
18 
33 #if defined(CowPtr_RECURSES)
34 #error Recursive header files inclusion detected in CowPtr.h
35 #else // defined(CowPtr_RECURSES)
36 
37 #define CowPtr_RECURSES
38 
39 #if !defined CowPtr_h
40 
41 #define CowPtr_h
42 
44 // Inclusions
45 #include <iostream>
46 #include "DGtal/base/Common.h"
47 #include "DGtal/base/CountedPtr.h"
49 
50 namespace DGtal
51 {
52 
54  // template class CowPtr
65  template <typename T>
66  class CowPtr
67  {
68  // ----------------------- Standard services ------------------------------
69  public:
70 public:
71  typedef T element_type;
72 
73  explicit CowPtr(T* p = 0) throw() : myPtr(p) {}
74  // no need for ~CowPtr - the CountedPtr takes care of everything.
75  CowPtr(const CowPtr& r) throw() : myPtr(r.myPtr) {}
79  CowPtr(const CountedPtr<T> & r) : myPtr( r ) {}
80  CowPtr& operator=(const CowPtr& r)
81  {
82  if (this != &r)
83  myPtr = r.myPtr;
84  return *this;
85  }
86 
87 
88  const T& operator*() const throw() {return *myPtr;}
89  const T* operator->() const throw() {return myPtr.get();}
90  const T* get() const throw() {return myPtr.get();}
91 
92  T& operator*() {copy(); return *myPtr;}
93  T* operator->() {copy(); return myPtr.get();}
94  T* get() {copy(); return myPtr.get();}
95 
99  unsigned int count() const {return myPtr.count();}
100 
101  private:
102 
103  // ------------------------- Private Datas --------------------------------
104  private:
105 
107 
108  // ------------------------- Internals ------------------------------------
109  private:
110  void copy() // create a new copy of myPtr
111  {
112  if (!myPtr.unique()) {
113  T* old_p = myPtr.get();
114  myPtr = CountedPtr<T>(new T(*old_p));
115  }
116  }
117 
118 
119  // ----------------------- Interface --------------------------------------
120  public:
121 
126  void selfDisplay ( std::ostream & out ) const;
127 
132  bool isValid() const;
133 
134  // ------------------------- Protected Datas ------------------------------
135  private:
136 
137  // ------------------------- Hidden services ------------------------------
138  protected:
139 
140 
141 
142  }; // end of class CowPtr
143 
144 
151  template <typename T>
152  std::ostream&
153  operator<< ( std::ostream & out, const CowPtr<T> & object );
154 
155 } // namespace DGtal
156 
157 
159 // Includes inline functions.
160 #include "DGtal/base/CowPtr.ih"
161 
162 // //
164 
165 #endif // !defined CowPtr_h
166 
167 #undef CowPtr_RECURSES
168 #endif // else defined(CowPtr_RECURSES)