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
// Copyright (C) 2007 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#undef DLIB_SCOPED_PTr_ABSTRACT_
#ifdef DLIB_SCOPED_PTr_ABSTRACT_
#include "../noncopyable.h"
namespace dlib
{
// ----------------------------------------------------------------------------------------
template <typename T>
struct default_deleter
{
void operator() (
T* item
) const;
/*!
ensures
- if (T is an array type (e.g. int[])) then
- performs "delete [] item;"
- else
- performs "delete item;"
!*/
};
// ----------------------------------------------------------------------------------------
template <
typename T,
typename deleter = default_deleter<T>
>
class scoped_ptr : noncopyable
{
/*!
REQUIREMENTS ON deleter
Must be a function object that performs deallocation of a pointer
of type T. For example, see the default_deleter type defined above.
INITIAL VALUE
defined by constructor
WHAT THIS OBJECT REPRESENTS
This is a smart pointer class inspired by the implementation of the scoped_ptr
class found in the Boost C++ library. So this is a simple smart pointer
class which guarantees that the pointer contained within it will always be
deleted.
The class does not permit copying and so does not do any kind of
reference counting. Thus it is very simply and quite fast.
Note that this class allows you to use pointers to arrays as well as
pointers to single items. To let it know that it is supposed to point
to an array you have to declare it using the bracket syntax. Consider
the following examples:
// This is how you make a scoped pointer to a single thing
scoped_ptr<int> single_item(new int);
// This is how you can use a scoped pointer to contain array pointers.
// Note the use of []. This ensures that the proper version of delete
// is called.
scoped_ptr<int[]> array_of_ints(new int[50]);
!*/
public:
typedef T element_type;
typedef deleter deleter_type;
explicit scoped_ptr (
T* p = 0
);
/*!
ensures
- #get() == p
!*/
~scoped_ptr(
);
/*!
ensures
- if (get() != 0) then
- calls deleter()(get())
(i.e. uses the deleter type to delete the pointer that is
contained in this scoped pointer)
!*/
void reset (
T* p = 0
);
/*!
ensures
- if (get() != 0) then
- calls deleter()(get())
(i.e. uses the deleter type to delete the pointer that is
contained in this scoped pointer)
- #get() == p
(i.e. makes this object contain a pointer to p instead of whatever it
used to contain)
!*/
T& operator*(
) const;
/*!
requires
- get() != 0
- T is NOT an array type (e.g. not int[])
ensures
- returns a reference to *get()
!*/
T* operator->(
) const;
/*!
requires
- get() != 0
- T is NOT an array type (e.g. not int[])
ensures
- returns the pointer contained in this object
!*/
T& operator[](
unsigned long idx
) const;
/*!
requires
- get() != 0
- T is an array type (e.g. int[])
ensures
- returns get()[idx]
!*/
T* get(
) const;
/*!
ensures
- returns the pointer contained in this object
!*/
operator bool(
) const;
/*!
ensures
- returns get() != 0
!*/
void swap(
scoped_ptr& b
);
/*!
ensures
- swaps *this and item
!*/
};
template <
typename T
>
void swap(
scoped_ptr<T>& a,
scoped_ptr<T>& b
) { a.swap(b); }
/*!
provides a global swap function
!*/
}
#endif // DLIB_SCOPED_PTr_ABSTRACT_