summaryrefslogtreecommitdiff
path: root/include/unique-ptr.h
blob: a81ddebfffc1435f825aca9754153a4986f45b9c (plain)
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/* gnu::unique_ptr, a simple std::unique_ptr replacement for C++03.

   Copyright (C) 2007-2020 Free Software Foundation, Inc.

   This file is part of GCC.

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

   This program 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 General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

/* gnu::unique_ptr defines a C++ owning smart pointer that exposes a
   subset of the std::unique_ptr API.

   In fact, when compiled with a C++11 compiler, gnu::unique_ptr
   actually _is_ std::unique_ptr.  When compiled with a C++03 compiler
   OTOH, it's an hand coded std::unique_ptr emulation that assumes
   code is correct and doesn't try to be too smart.

   This supports custom deleters, but not _stateful_ deleters, so you
   can't use those in C++11 mode either.  Only the managed pointer is
   stored in the smart pointer.  That could be changed; it simply
   wasn't found necessary.

   At the end of the file you'll find a gnu::unique_ptr partial
   specialization that uses a custom (stateless) deleter:
   gnu::unique_xmalloc_ptr.  That is used to manage pointers to
   objects allocated with xmalloc.

   The C++03 version was originally based on GCC 7.0's std::auto_ptr
   and then heavily customized to behave more like C++11's
   std::unique_ptr, but at this point, it no longer shares much at all
   with the original file.  But, that's the history and the reason for
   the copyright's starting year.

   The C++03 version lets you shoot yourself in the foot, since
   similarly to std::auto_ptr, the copy constructor and assignment
   operators actually move.  Also, in the name of simplicity, no
   effort is spent on using SFINAE to prevent invalid conversions,
   etc.  This is not really a problem, because the goal here is to
   allow code that would be correct using std::unique_ptr to be
   equally correct in C++03 mode, and, just as efficient.  If client
   code compiles correctly with a C++11 (or newer) compiler, we know
   we're not doing anything invalid by mistake.

   Usage notes:

   - Putting gnu::unique_ptr in standard containers is not supported,
     since C++03 containers are not move-aware (and our emulation
     relies on copy actually moving).

   - Since there's no nullptr in C++03, gnu::unique_ptr allows
     implicit initialization and assignment from NULL instead.

   - To check whether there's an associated managed object, all these
     work as expected:

      if (ptr)
      if (!ptr)
      if (ptr != NULL)
      if (ptr == NULL)
      if (NULL != ptr)
      if (NULL == ptr)
*/

#ifndef GNU_UNIQUE_PTR_H
#define GNU_UNIQUE_PTR_H 1

#if __cplusplus >= 201103
# include <memory>
#endif

namespace gnu
{

#if __cplusplus >= 201103

/* In C++11 mode, all we need is import the standard
   std::unique_ptr.  */
template<typename T> using unique_ptr = std::unique_ptr<T>;

/* Pull in move as well.  */
using std::move;

#else /* C++11 */

/* Default destruction policy used by gnu::unique_ptr when no deleter
   is specified.  Uses delete.  */

template<typename T>
struct default_delete
{
  void operator () (T *ptr) const { delete ptr; }
};

/* Specialization for arrays.  Uses delete[].  */

template<typename T>
struct default_delete<T[]>
{
  void operator () (T *ptr) const { delete [] ptr; }
};

namespace detail
{
/* Type used to support implicit construction from NULL:

     gnu::unique_ptr<foo> func (....)
     {
     return NULL;
     }

   and assignment from NULL:

     gnu::unique_ptr<foo> ptr (....);
     ...
     ptr = NULL;

  It is intentionally not defined anywhere.  */
struct nullptr_t;

/* Base class of our unique_ptr emulation.  Contains code common to
   both unique_ptr<T, D> and unique_ptr<T[], D>.  */

template<typename T, typename D>
class unique_ptr_base
{
public:
  typedef T *pointer;
  typedef T element_type;
  typedef D deleter_type;

  /* Takes ownership of a pointer.  P is a pointer to an object of
     element_type type.  Defaults to NULL.  */
  explicit unique_ptr_base (element_type *p = NULL) throw () : m_ptr (p) {}

  /* The "move" constructor.  Really a copy constructor that actually
     moves.  Even though std::unique_ptr is not copyable, our little
     simpler emulation allows it, because:

       - There are no rvalue references in C++03.  Our move emulation
       instead relies on copy/assignment moving, like std::auto_ptr.
       - RVO/NRVO requires an accessible copy constructor
  */
  unique_ptr_base (const unique_ptr_base &other) throw ()
    : m_ptr (const_cast<unique_ptr_base &> (other).release ()) {}

  /* Converting "move" constructor.  Really an lvalue ref converting
     constructor that actually moves.  This allows constructs such as:

      unique_ptr<Derived> func_returning_unique_ptr (.....);
      ...
      unique_ptr<Base> ptr = func_returning_unique_ptr (.....);
  */
  template<typename T1, typename D1>
  unique_ptr_base (const unique_ptr_base<T1, D1> &other) throw ()
    : m_ptr (const_cast<unique_ptr_base<T1, D1> &> (other).release ()) {}

  /* The "move" assignment operator.  Really an lvalue ref copy
     assignment operator that actually moves.  See comments above.  */
  unique_ptr_base &operator= (const unique_ptr_base &other) throw ()
  {
    reset (const_cast<unique_ptr_base &> (other).release ());
    return *this;
  }

  /* Converting "move" assignment.  Really an lvalue ref converting
     copy assignment operator that moves.  See comments above.  */
  template<typename T1, typename D1>
  unique_ptr_base &operator= (const unique_ptr_base<T1, D1> &other) throw ()
  {
    reset (const_cast<unique_ptr_base<T1, D1> &> (other).release ());
    return *this;
  }

  /* std::unique_ptr does not allow assignment, except from nullptr.
     nullptr doesn't exist in C++03, so we allow assignment from NULL
     instead [ptr = NULL;].
  */
  unique_ptr_base &operator= (detail::nullptr_t *) throw ()
  {
    reset ();
    return *this;
  }

  ~unique_ptr_base () { call_deleter (); }

  /* "explicit operator bool ()" emulation using the safe bool
     idiom.  */
private:
  typedef void (unique_ptr_base::*explicit_operator_bool) () const;
  void this_type_does_not_support_comparisons () const {}

public:
  operator explicit_operator_bool () const
  {
    return (m_ptr != NULL
	    ? &unique_ptr_base::this_type_does_not_support_comparisons
	    : 0);
  }

  element_type *get () const throw () { return m_ptr; }

  element_type *release () throw ()
  {
    pointer tmp = m_ptr;
    m_ptr = NULL;
    return tmp;
  }

  void reset (element_type *p = NULL) throw ()
  {
    if (p != m_ptr)
      {
	call_deleter ();
	m_ptr = p;
      }
  }

private:

  /* Call the deleter.  Note we assume the deleter is "stateless".  */
  void call_deleter ()
  {
    D d;

    d (m_ptr);
  }

  element_type *m_ptr;
};

} /* namespace detail */

/* Macro used to create a unique_ptr_base "partial specialization" --
   a subclass that uses a specific deleter.  Basically this re-defines
   the necessary constructors.  This is necessary because C++03
   doesn't support inheriting constructors with "using".  While at it,
   we inherit the assignment operator.  TYPE is the name of the type
   being defined.  Assumes that 'base_type' is a typedef of the
   baseclass TYPE is inheriting from.  */
#define DEFINE_GNU_UNIQUE_PTR(TYPE)						\
public:									\
  explicit TYPE (T *p = NULL) throw ()					\
    : base_type (p) {}							\
									\
  TYPE (const TYPE &other) throw () : base_type (other) {}		\
									\
  TYPE (detail::nullptr_t *) throw () : base_type (NULL) {}		\
									\
  template<typename T1, typename D1>					\
  TYPE (const detail::unique_ptr_base<T1, D1> &other) throw ()		\
    : base_type (other) {}						\
									\
  using base_type::operator=;

/* Define single-object gnu::unique_ptr.  */

template <typename T, typename D = default_delete<T> >
class unique_ptr : public detail::unique_ptr_base<T, D>
{
  typedef detail::unique_ptr_base<T, D> base_type;

  DEFINE_GNU_UNIQUE_PTR (unique_ptr)

public:
  /* Dereferencing.  */
  T &operator* () const throw () { return *this->get (); }
  T *operator-> () const throw () { return this->get (); }
};

/* Define gnu::unique_ptr specialization for T[].  */

template <typename T, typename D>
class unique_ptr<T[], D> : public detail::unique_ptr_base<T, D>
{
  typedef detail::unique_ptr_base<T, D> base_type;

  DEFINE_GNU_UNIQUE_PTR (unique_ptr)

public:
  /* Indexing operator.  */
  T &operator[] (size_t i) const { return this->get ()[i]; }
};

/* Comparison operators.  */

template <typename T, typename D,
	  typename U, typename E>
inline bool
operator== (const detail::unique_ptr_base<T, D> &x,
	    const detail::unique_ptr_base<U, E> &y)
{ return x.get() == y.get(); }

template <typename T, typename D,
	  typename U, typename E>
inline bool
operator!= (const detail::unique_ptr_base<T, D> &x,
	    const detail::unique_ptr_base<U, E> &y)
{ return x.get() != y.get(); }

template<typename T, typename D,
	 typename U, typename E>
inline bool
operator< (const detail::unique_ptr_base<T, D> &x,
	   const detail::unique_ptr_base<U, E> &y)
{ return x.get() < y.get (); }

template<typename T, typename D,
	 typename U, typename E>
inline bool
operator<= (const detail::unique_ptr_base<T, D> &x,
	    const detail::unique_ptr_base<U, E> &y)
{ return !(y < x); }

template<typename T, typename D,
	 typename U, typename E>
inline bool
operator> (const detail::unique_ptr_base<T, D> &x,
	   const detail::unique_ptr_base<U, E> &y)
{ return y < x; }

template<typename T, typename D,
	 typename U, typename E>
inline bool
operator>= (const detail::unique_ptr_base<T, D> &x,
	    const detail::unique_ptr_base<U, E> &y)
{ return !(x < y); }

/* std::move "emulation".  This is as simple as it can be -- no
   attempt is made to emulate rvalue references.  This relies on T
   having move semantics like std::auto_ptr.
   I.e., copy/assignment actually moves.  */

template<typename T>
const T&
move (T& v)
{
  return v;
}

#endif /* C++11 */

/* Define gnu::unique_xmalloc_ptr, a gnu::unique_ptr that manages
   xmalloc'ed memory.  */

/* The deleter for gnu::unique_xmalloc_ptr.  Uses free.  */
template <typename T>
struct xmalloc_deleter
{
  void operator() (T *ptr) const { free (ptr); }
};

/* Same, for arrays.  */
template <typename T>
struct xmalloc_deleter<T[]>
{
  void operator() (T *ptr) const { free (ptr); }
};

#if __cplusplus >= 201103

/* In C++11, we just import the standard unique_ptr to our namespace
   with a custom deleter.  */

template<typename T> using unique_xmalloc_ptr
  = std::unique_ptr<T, xmalloc_deleter<T>>;

#else /* C++11 */

/* In C++03, we don't have template aliases, so we need to define a
   subclass instead, and re-define the constructors, because C++03
   doesn't support inheriting constructors either.  */

template <typename T>
class unique_xmalloc_ptr : public unique_ptr<T, xmalloc_deleter<T> >
{
  typedef unique_ptr<T, xmalloc_deleter<T> > base_type;

  DEFINE_GNU_UNIQUE_PTR (unique_xmalloc_ptr)
};

/* Define gnu::unique_xmalloc_ptr specialization for T[].  */

template <typename T>
class unique_xmalloc_ptr<T[]> : public unique_ptr<T[], xmalloc_deleter<T[]> >
{
  typedef unique_ptr<T[], xmalloc_deleter<T[]> > base_type;

  DEFINE_GNU_UNIQUE_PTR (unique_xmalloc_ptr)
};

#endif /* C++11 */

} /* namespace gnu */

#endif /* GNU_UNIQUE_PTR_H */