monotone

monotone Mtn Source Tree

Root/cryptopp/smartptr.h

1#ifndef CRYPTOPP_SMARTPTR_H
2#define CRYPTOPP_SMARTPTR_H
3
4#include "config.h"
5#include <algorithm>
6
7NAMESPACE_BEGIN(CryptoPP)
8
9template<class T> class member_ptr
10{
11public:
12explicit member_ptr(T *p = NULL) : m_p(p) {}
13
14~member_ptr();
15
16const T& operator*() const { return *m_p; }
17T& operator*() { return *m_p; }
18
19const T* operator->() const { return m_p; }
20T* operator->() { return m_p; }
21
22const T* get() const { return m_p; }
23T* get() { return m_p; }
24
25T* release()
26{
27T *old_p = m_p;
28m_p = 0;
29return old_p;
30}
31
32void reset(T *p = 0);
33
34protected:
35member_ptr(const member_ptr<T>& rhs);// copy not allowed
36void operator=(const member_ptr<T>& rhs);// assignment not allowed
37
38T *m_p;
39};
40
41template <class T> member_ptr<T>::~member_ptr() {delete m_p;}
42template <class T> void member_ptr<T>::reset(T *p) {delete m_p; m_p = p;}
43
44// ********************************************************
45
46template<class T> class value_ptr : public member_ptr<T>
47{
48public:
49value_ptr(const T &obj) : member_ptr<T>(new T(obj)) {}
50value_ptr(T *p = NULL) : member_ptr<T>(p) {}
51value_ptr(const value_ptr<T>& rhs)
52: member_ptr<T>(rhs.m_p ? new T(*rhs.m_p) : NULL) {}
53
54value_ptr<T>& operator=(const value_ptr<T>& rhs);
55bool operator==(const value_ptr<T>& rhs)
56{
57return (!m_p && !rhs.m_p) || (m_p && rhs.m_p && *m_p == *rhs.m_p);
58}
59};
60
61template <class T> value_ptr<T>& value_ptr<T>::operator=(const value_ptr<T>& rhs)
62{
63T *old_p = m_p;
64m_p = rhs.m_p ? new T(*rhs.m_p) : NULL;
65delete old_p;
66return *this;
67}
68
69// ********************************************************
70
71template<class T> class clonable_ptr : public member_ptr<T>
72{
73public:
74clonable_ptr(const T &obj) : member_ptr<T>(obj.Clone()) {}
75clonable_ptr(T *p = NULL) : member_ptr<T>(p) {}
76clonable_ptr(const clonable_ptr<T>& rhs)
77: member_ptr<T>(rhs.m_p ? rhs.m_p->Clone() : NULL) {}
78
79clonable_ptr<T>& operator=(const clonable_ptr<T>& rhs);
80};
81
82template <class T> clonable_ptr<T>& clonable_ptr<T>::operator=(const clonable_ptr<T>& rhs)
83{
84T *old_p = m_p;
85m_p = rhs.m_p ? rhs.m_p->Clone() : NULL;
86delete old_p;
87return *this;
88}
89
90// ********************************************************
91
92template<class T> class counted_ptr
93{
94public:
95explicit counted_ptr(T *p = 0);
96counted_ptr(const T &r) : m_p(0) {attach(r);}
97counted_ptr(const counted_ptr<T>& rhs);
98
99~counted_ptr();
100
101const T& operator*() const { return *m_p; }
102T& operator*() { return *m_p; }
103
104const T* operator->() const { return m_p; }
105T* operator->() { return get(); }
106
107const T* get() const { return m_p; }
108T* get();
109
110void attach(const T &p);
111
112counted_ptr<T> & operator=(const counted_ptr<T>& rhs);
113
114private:
115T *m_p;
116};
117
118template <class T> counted_ptr<T>::counted_ptr(T *p)
119: m_p(p)
120{
121if (m_p)
122m_p->m_referenceCount = 1;
123}
124
125template <class T> counted_ptr<T>::counted_ptr(const counted_ptr<T>& rhs)
126: m_p(rhs.m_p)
127{
128if (m_p)
129m_p->m_referenceCount++;
130}
131
132template <class T> counted_ptr<T>::~counted_ptr()
133{
134if (m_p && --m_p->m_referenceCount == 0)
135delete m_p;
136}
137
138template <class T> void counted_ptr<T>::attach(const T &r)
139{
140if (m_p && --m_p->m_referenceCount == 0)
141delete m_p;
142if (r.m_referenceCount == 0)
143{
144m_p = r.clone();
145m_p->m_referenceCount = 1;
146}
147else
148{
149m_p = const_cast<T *>(&r);
150m_p->m_referenceCount++;
151}
152}
153
154template <class T> T* counted_ptr<T>::get()
155{
156if (m_p && m_p->m_referenceCount > 1)
157{
158T *temp = m_p->clone();
159m_p->m_referenceCount--;
160m_p = temp;
161m_p->m_referenceCount = 1;
162}
163return m_p;
164}
165
166template <class T> counted_ptr<T> & counted_ptr<T>::operator=(const counted_ptr<T>& rhs)
167{
168if (m_p != rhs.m_p)
169{
170if (m_p && --m_p->m_referenceCount == 0)
171delete m_p;
172m_p = rhs.m_p;
173if (m_p)
174m_p->m_referenceCount++;
175}
176return *this;
177}
178
179// ********************************************************
180
181template <class T> class vector_member_ptrs
182{
183public:
184vector_member_ptrs(unsigned int size=0)
185: _size(size) {ptr = new member_ptr<T>[_size];}
186~vector_member_ptrs()
187{delete [] ptr;}
188
189member_ptr<T>& operator[](unsigned int index)
190{assert(index<_size); return ptr[index];}
191const member_ptr<T>& operator[](unsigned int index) const
192{assert(index<_size); return ptr[index];}
193
194unsigned int size() const {return _size;}
195void resize(unsigned int newSize)
196{
197member_ptr<T> *newPtr = new member_ptr<T>[newSize];
198for (unsigned int i=0; i<STDMIN(_size, newSize); i++)
199newPtr[i].reset(ptr[i].release());
200delete [] ptr;
201_size = newSize;
202ptr = newPtr;
203}
204
205private:
206vector_member_ptrs(const vector_member_ptrs<T> &c);// copy not allowed
207void operator=(const vector_member_ptrs<T> &x);// assignment not allowed
208
209unsigned int _size;
210member_ptr<T> *ptr;
211};
212
213NAMESPACE_END
214
215#endif

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status