ddc
ptrholder.h
Go to the documentation of this file.
1 #ifndef ptrholder_h
2 #define ptrholder_h
3 
4 
6 template <class T>
7 class PtrHolder {
8 public:
9  inline PtrHolder(T* t = 0) throw ()
10  : T_(t)
11  {};
12 
13  inline PtrHolder(PtrHolder& that)
14  : T_(that.Release())
15  {};
16 
17  inline ~PtrHolder() throw ()
18  { DoDestroy(); };
19 
20  inline void Destroy() throw ()
21  { Reset(0); };
22 
23  inline T* operator-> () const throw ()
24  { return AsT(); };
25 
26  template <class C>
27  inline bool operator== (const C& p) const
28  { return (p == AsT()); };
29 
30  template <class C>
31  inline bool operator!= (const C& p) const
32  { return (p != AsT()); };
33 
34  inline operator bool () const
35  { return 0 != AsT(); };
36 
37  inline T* Release() {
38  T* ret = T_;
39  T_ = 0;
40  return ret;
41  };
42 
43  inline void Reset(T* t) {
44  if (T_ != t) {
45  DoDestroy();
46  T_ = t;
47  }
48  };
49 
50  inline T* Get() const
51  { return T_; };
52 
54  this->Reset(that.Release());
55  return *this;
56  };
57 
58  T& operator* () const {
59  assert(this->AsT());
60  return *(this->AsT());
61  };
62 
63 protected:
64  void DoDestroy()
65  { if (T_) delete T_; };
66 
67  T* AsT() const
68  { return (static_cast<PtrHolder<T>*>(this))->Get(); };
69 
70 protected:
71  T* T_;
72 };
73 
74 
76 template <class T>
78 public:
79  inline ArrayPtrHolder(T* t = 0) throw ()
80  : T_(t)
81  {};
82 
84  : T_(that.Release())
85  {};
86 
87  inline ~ArrayPtrHolder() throw ()
88  { DoDestroy(); };
89 
90  inline void Destroy() throw ()
91  { Reset(0); };
92 
93  inline T* operator-> () const throw ()
94  { return AsT(); };
95 
96  template <class C>
97  inline bool operator== (const C& p) const
98  { return (p == AsT()); };
99 
100  template <class C>
101  inline bool operator!= (const C& p) const
102  { return (p != AsT()); };
103 
104  inline operator bool () const
105  { return 0 != AsT(); };
106 
107  inline T* Release() {
108  T* ret = T_;
109  T_ = 0;
110  return ret;
111  };
112 
113  inline void Reset(T* t) {
114  if (T_ != t) {
115  DoDestroy();
116  T_ = t;
117  }
118  };
119 
120  inline T* Get() const
121  { return T_; };
122 
124  this->Reset(that.Release());
125  return *this;
126  };
127 
128  T& operator* () const {
129  assert(this->AsT());
130  return *(this->AsT());
131  };
132 
133 protected:
134  void DoDestroy()
135  { if (T_) delete[] T_; };
136 
137  T* AsT() const
138  { return (static_cast<ArrayPtrHolder<T>*>(this))->Get(); };
139 
140 protected:
141  T* T_;
142 };
143 
144 
145 
146 #endif
147 
148 /*--- emacs style variables ---
149  * Local Variables:
150  * mode: C++
151  * c-file-style: "ellemtel"
152  * c-basic-offset: 4
153  * tab-width: 8
154  * indent-tabs-mode: nil
155  * End:
156  */
T * Get() const
Definition: ptrholder.h:120
T * Release()
Definition: ptrholder.h:37
T * T_
Definition: ptrholder.h:138
void Reset(T *t)
Definition: ptrholder.h:43
bool operator==(const C &p) const
Definition: ptrholder.h:27
bool operator!=(const C &p) const
Definition: ptrholder.h:31
PtrHolder & operator=(PtrHolder &that)
Definition: ptrholder.h:53
Definition: ptrholder.h:77
void DoDestroy()
Definition: ptrholder.h:134
void Destroy()
Definition: ptrholder.h:20
T * operator->() const
Definition: ptrholder.h:23
T * Get() const
Definition: ptrholder.h:50
void Reset(T *t)
Definition: ptrholder.h:113
ArrayPtrHolder(ArrayPtrHolder &that)
Definition: ptrholder.h:83
ArrayPtrHolder(T *t=0)
Definition: ptrholder.h:79
~ArrayPtrHolder()
Definition: ptrholder.h:87
Definition: ptrholder.h:7
T * AsT() const
Definition: ptrholder.h:67
void Destroy()
Definition: ptrholder.h:90
PtrHolder(PtrHolder &that)
Definition: ptrholder.h:13
~PtrHolder()
Definition: ptrholder.h:17
PtrHolder(T *t=0)
Definition: ptrholder.h:9
T * T_
Definition: ptrholder.h:68
T & operator*() const
Definition: ptrholder.h:58
void DoDestroy()
Definition: ptrholder.h:64
T * AsT() const
Definition: ptrholder.h:137
T * Release()
Definition: ptrholder.h:107