File: refcounted.h

package info (click to toggle)
mysql%2B%2B 3.2.1%2Bpristine-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 13,624 kB
  • ctags: 11,252
  • sloc: cpp: 35,659; sh: 3,034; makefile: 951; perl: 786
file content (268 lines) | stat: -rw-r--r-- 8,427 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/// \file refcounted.h
/// \brief Declares the RefCountedPointer template

/***********************************************************************
 Copyright (c) 2007-2011 by Educational Technology Resources, Inc. and
 (c) 2007 by Jonathan Wakely.  Others may also hold copyrights on
 code in this file.  See the CREDITS.txt file in the top directory
 of the distribution for details.

 This file is part of MySQL++.

 MySQL++ is free software; you can redistribute it and/or modify it
 under the terms of the GNU Lesser General Public License as published
 by the Free Software Foundation; either version 2.1 of the License, or
 (at your option) any later version.

 MySQL++ is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 License for more details.

 You should have received a copy of the GNU Lesser General Public
 License along with MySQL++; if not, write to the Free Software
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
 USA
***********************************************************************/

#if !defined(MYSQLPP_REFCOUNTED_H)
#define MYSQLPP_REFCOUNTED_H

#include <memory>
#include <cstddef>

#include <stddef.h>

namespace mysqlpp {

/// \brief Functor to call delete on the pointer you pass to it
///
/// The default "destroyer" for RefCountedPointer.  You won't use this
/// directly, you'll pass a functor of your own devising for the second
/// parameter to the RefCountedPointer template to override this.  Or
/// simpler, just specialize this template for your type if possible:
/// see ResUse::result_.
template <class T>
struct RefCountedPointerDestroyer
{
	/// \brief Functor implementation
	void operator()(T* doomed) const { delete doomed; }
};


/// \brief Creates an object that acts as a reference-counted pointer
/// to another object.
///
/// Resulting type acts like a pointer in all respects, except that it
/// manages the memory it points to by observing how many users there
/// are for the object.
///
/// This attempts to be as automatic as reference counting in a
/// programming language with memory management.  Like all automatic
/// memory management schemes, it has penalties: it turns the single
/// indirection of an unmanaged pointer into a double indirection, and
/// has additional management overhead in the assignment operators due
/// to the reference counter.  This is an acceptable tradeoff when
/// wrapping objects that are expensive to copy, and which need to
/// be "owned" by disparate parties: you can allocate the object just
/// once, then pass around the reference counted pointer, knowing that
/// the last user will "turn out the lights".
///
/// \b Implementation \b detail: You may notice that this class manages
/// two pointers, one to the data we're managing, and one to the
/// reference count.  You might wonder why we don't wrap these up into a
/// structure and keep just a pointer to an instance of it to simplify
/// the memory management.  It would indeed do that, but then every
/// access to the data we manage would be a triple indirection instead
/// of just double.  It's a tradeoff, and we've chosen to take a minor
/// complexity hit to avoid the performance hit.

template <class T, class Destroyer = RefCountedPointerDestroyer<T> >
class RefCountedPointer
{
public:
	typedef RefCountedPointer<T> ThisType;	///< alias for this object's type

	/// \brief Default constructor
	///
	/// An object constructed this way is useless until you vivify it
	/// with operator =() or assign().
	RefCountedPointer() :
	counted_(0),
	refs_(0)
	{
	}
	
	/// \brief Standard constructor
	///
	/// \param c A pointer to the object to be managed.  If you pass 0,
	/// it's like calling the default ctor instead, only more work: the
	/// object's useless until you vivify it with operator =() or assign().
	explicit RefCountedPointer(T* c) :
	counted_(c),
	refs_(0)
	{
		std::auto_ptr<T> exception_guard(counted_);
		if (counted_) {
			refs_ = new size_t(1);
		}
		exception_guard.release();	// previous new didn't throw
	}

	/// \brief Copy constructor
	RefCountedPointer(const ThisType& other) :
	counted_(other.counted_),
	refs_(other.counted_ ? other.refs_ : 0)
	{
		if (counted_) {
			++(*refs_);
		}
	}

	/// \brief Destructor
	///
	/// This only destroys the managed memory if the reference count
	/// drops to 0.
	~RefCountedPointer()
	{
		if (refs_ && (--(*refs_) == 0)) {
			Destroyer()(counted_);
			delete refs_;
		}
	}

	/// \brief Sets (or resets) the pointer to the counted object.
	///
	/// If we are managing a pointer, this decrements the refcount for
	/// it and destroys the managed object if the refcount falls to 0.
	///
	/// This is a no-op if you pass the same pointer we're already
	/// managing.
	ThisType& assign(T* c)
	{
		// The create-temporary-and-swap idiom lets us keep memory
		// allocation in the ctor and deallocation in the dtor so
		// we don't leak in the face of an exception.
		ThisType(c).swap(*this);
		return *this;
	}

	/// \brief Copy an existing refcounted pointer
	///
	/// If we are managing a pointer, this decrements the refcount for
	/// it and destroys the managed object if the refcount falls to 0.
	/// Then we increment the other object's reference count and copy
	/// that refcount and the managed pointer into this object.
	///
	/// This is a no-op if you pass a reference to this same object.
	ThisType& assign(const ThisType& other)
	{
		// The create-temporary-and-swap idiom lets us keep memory
		// allocation in the ctor and deallocation in the dtor so
		// we don't leak in the face of an exception.
		ThisType(other).swap(*this);
		return *this;
	}

	/// \brief Set (or reset) the pointer to the counted object
	///
	/// This is essentially the same thing as assign(T*).  The choice
	/// between the two is just a matter of syntactic preference.
	ThisType& operator =(T* c)
	{
		return assign(c);
	}

	/// \brief Copy an existing refcounted pointer
	///
	/// This is essentially the same thing as assign(const ThisType&).
	/// The choice between the two is just a matter of syntactic
	/// preference.
	ThisType& operator =(const ThisType& rhs)
	{
		return assign(rhs);
	}

	/// \brief Access the object through the smart pointer
	T* operator ->() const
	{
		return counted_;
	}	

	/// \brief Dereference the smart pointer
	T& operator *() const
	{
		return *counted_;
	}	

	/// \brief Returns the internal raw pointer converted to void*
	///
	/// This isn't intended to be used directly; if you need the
	/// pointer, call raw() instead.  It's used internally by the
	/// compiler to implement operators bool, ==, and !=
	///
	/// \b WARNING: This makes it possible to say
	/// \code
	/// RefCountedPointer<Foo> bar(new Foo);
	/// delete bar;
	/// \endcode
	///
	/// This will almost kinda sorta do the right thing: the Foo
	/// object held by the refcounted pointer will be destroyed as
	/// you wanted, but then when the refcounted pointer goes out of
	/// scope, the memory is deleted a second time, which will probably
	/// crash your program.  This is easy to accidentally do when
	/// converting a good ol' unmanaged pointer to a refcounted pointer
	/// and forgetting to remove the delete calls needed previously.
	operator void*()
	{
		return counted_;
	}

	/// \brief Returns the internal raw pointer converted to const void*
	///
	/// \see comments for operator void*()
	operator const void*() const
	{
		return counted_;
	}

	/// \brief Return the raw pointer in T* context
	T* raw()
	{
		return counted_;
	}

	/// \brief Return the raw pointer when used in const T* context
	const T* raw() const
	{
		return counted_;
	}

	/// \brief Exchange our managed memory with another pointer.
	///
	/// \internal This exists primarily to implement assign() in an
	/// exception-safe manner.
	void swap(ThisType& other)
	{
		std::swap(counted_, other.counted_);
		std::swap(refs_, other.refs_);
	}	

private:
	/// \brief Pointer to the reference-counted object
	T* counted_;

	/// \brief Pointer to the reference count.
	///
	/// We can't keep this as a plain integer because this object
	/// allows itself to be copied.  All copies need to share this
	/// reference count, not just the pointer to the counted object.
	size_t* refs_;
};


} // end namespace mysqlpp

#endif // !defined(MYSQLPP_REFCOUNTED_H)