cprover
Toggle main menu visibility
Loading...
Searching...
No Matches
small_shared_ptr.h
Go to the documentation of this file.
1
/*******************************************************************\
2
3
Module: Small intrusive shared pointers
4
5
Author: Reuben Thomas, reuben.thomas@diffblue.com
6
7
\*******************************************************************/
8
9
#ifndef CPROVER_UTIL_SMALL_SHARED_PTR_H
10
#define CPROVER_UTIL_SMALL_SHARED_PTR_H
11
12
#include <iosfwd>
// ostream
13
#include <utility>
// swap
14
15
// TODO We should liberally scatter `constexpr`s and `noexcept`s on platforms
16
// that support them.
17
23
template
<
typename
T>
24
class
small_shared_ptrt
final
25
{
26
public
:
27
small_shared_ptrt
() =
default
;
28
29
explicit
small_shared_ptrt
(T *t) :
t_
(t)
30
{
31
if
(
t_
)
32
{
33
pointee_increment_use_count
(*
t_
);
34
}
35
}
36
37
small_shared_ptrt
(
const
small_shared_ptrt
&rhs) :
t_
(rhs.
t_
)
38
{
39
if
(
t_
)
40
{
41
pointee_increment_use_count
(*
t_
);
42
}
43
}
44
45
small_shared_ptrt
&
operator=
(
const
small_shared_ptrt
&rhs)
46
{
47
auto
copy(rhs);
48
swap
(copy);
49
return
*
this
;
50
}
51
52
small_shared_ptrt
(
small_shared_ptrt
&&rhs)
53
{
54
swap
(rhs);
55
}
56
57
small_shared_ptrt
&
operator=
(
small_shared_ptrt
&&rhs)
58
{
59
swap
(rhs);
60
return
*
this
;
61
}
62
63
~small_shared_ptrt
()
64
{
65
if
(!
t_
)
66
{
67
return
;
68
}
69
if
(
pointee_use_count
(*
t_
) == 1)
70
{
71
delete
t_
;
72
return
;
73
}
74
pointee_decrement_use_count
(*
t_
);
75
}
76
77
void
swap
(
small_shared_ptrt
&rhs)
78
{
79
std::swap(
t_
, rhs.
t_
);
80
}
81
82
T *
get
()
const
83
{
84
return
t_
;
85
}
86
87
T &
operator*
()
const
88
{
89
return
*
t_
;
90
}
91
92
T *
operator->
()
const
93
{
94
return
t_
;
95
}
96
97
auto
use_count
() const -> decltype(
pointee_use_count
(
std
::declval<T>()))
98
{
99
return
t_
?
pointee_use_count
(*
t_
) : 0;
100
}
101
102
explicit
operator
bool()
const
103
{
104
return
t_
!=
nullptr
;
105
}
106
107
private
:
108
T *
t_
=
nullptr
;
109
};
110
111
template
<
typename
T>
112
std::ostream &
operator<<
(std::ostream &os,
const
small_shared_ptrt<T>
&ptr)
113
{
114
return
os << ptr.
get
();
115
}
116
122
template
<
typename
T,
typename
... Ts>
123
small_shared_ptrt<T>
make_small_shared_ptr
(Ts &&... ts)
124
{
125
return
small_shared_ptrt<T>
(
new
T(std::forward<Ts>(ts)...));
126
}
127
128
template
<
typename
T,
typename
U>
129
bool
operator==
(
130
const
small_shared_ptrt<T>
&lhs,
131
const
small_shared_ptrt<U>
&rhs)
132
{
133
return
lhs.
get
() == rhs.
get
();
134
}
135
136
template
<
typename
T,
typename
U>
137
bool
operator!=
(
138
const
small_shared_ptrt<T>
&lhs,
139
const
small_shared_ptrt<U>
&rhs)
140
{
141
return
lhs.
get
() != rhs.
get
();
142
}
143
144
template
<
typename
T,
typename
U>
145
bool
operator<
(
const
small_shared_ptrt<T>
&lhs,
const
small_shared_ptrt<U>
&rhs)
146
{
147
return
lhs.
get
() < rhs.
get
();
148
}
149
150
template
<
typename
T,
typename
U>
151
bool
operator>
(
const
small_shared_ptrt<T>
&lhs,
const
small_shared_ptrt<U>
&rhs)
152
{
153
return
lhs.
get
() > rhs.
get
();
154
}
155
156
template
<
typename
T,
typename
U>
157
bool
operator<=
(
158
const
small_shared_ptrt<T>
&lhs,
159
const
small_shared_ptrt<U>
&rhs)
160
{
161
return
lhs.
get
() <= rhs.
get
();
162
}
163
164
template
<
typename
T,
typename
U>
165
bool
operator>=
(
166
const
small_shared_ptrt<T>
&lhs,
167
const
small_shared_ptrt<U>
&rhs)
168
{
169
return
lhs.
get
() >= rhs.
get
();
170
}
171
173
181
template
<
typename
Num>
182
class
small_shared_pointeet
183
{
184
public
:
185
small_shared_pointeet
() =
default
;
186
187
// These can't be `= default` because we need the use_count_ to be unaffected
188
small_shared_pointeet
(
const
small_shared_pointeet
&)
189
{
190
}
191
small_shared_pointeet
&
operator=
(
const
small_shared_pointeet
&)
192
{
193
return
*
this
;
194
}
195
small_shared_pointeet
(
small_shared_pointeet
&&)
196
{
197
}
198
small_shared_pointeet
&
operator=
(
small_shared_pointeet
&&)
199
{
200
return
*
this
;
201
}
202
203
void
increment_use_count
()
204
{
205
++
use_count_
;
206
}
207
void
decrement_use_count
()
208
{
209
--
use_count_
;
210
}
211
Num
use_count
()
const
212
{
213
return
use_count_
;
214
}
215
216
protected
:
217
// Enables public inheritance but disables polymorphic usage
218
~small_shared_pointeet
() =
default
;
219
220
private
:
221
Num
use_count_
= 0;
222
};
223
231
232
template
<
typename
Num>
233
inline
void
pointee_increment_use_count
(
small_shared_pointeet<Num>
&p)
234
{
235
p.
increment_use_count
();
236
}
237
238
template
<
typename
Num>
239
inline
void
pointee_decrement_use_count
(
small_shared_pointeet<Num>
&p)
240
{
241
p.
decrement_use_count
();
242
}
243
244
template
<
typename
Num>
245
inline
Num
pointee_use_count
(
const
small_shared_pointeet<Num>
&p)
246
{
247
return
p.
use_count
();
248
}
249
250
#endif
small_shared_pointeet
A helper class to store use-counts of objects held by small shared pointers.
Definition
small_shared_ptr.h:183
small_shared_pointeet::~small_shared_pointeet
~small_shared_pointeet()=default
small_shared_pointeet::operator=
small_shared_pointeet & operator=(small_shared_pointeet &&)
Definition
small_shared_ptr.h:198
small_shared_pointeet::small_shared_pointeet
small_shared_pointeet()=default
small_shared_pointeet::use_count
Num use_count() const
Definition
small_shared_ptr.h:211
small_shared_pointeet::operator=
small_shared_pointeet & operator=(const small_shared_pointeet &)
Definition
small_shared_ptr.h:191
small_shared_pointeet::decrement_use_count
void decrement_use_count()
Definition
small_shared_ptr.h:207
small_shared_pointeet::use_count_
Num use_count_
Definition
small_shared_ptr.h:221
small_shared_pointeet::small_shared_pointeet
small_shared_pointeet(const small_shared_pointeet &)
Definition
small_shared_ptr.h:188
small_shared_pointeet::small_shared_pointeet
small_shared_pointeet(small_shared_pointeet &&)
Definition
small_shared_ptr.h:195
small_shared_pointeet::increment_use_count
void increment_use_count()
Definition
small_shared_ptr.h:203
small_shared_ptrt
This class is really similar to boost's intrusive_pointer, but can be a bit simpler seeing as we're o...
Definition
small_shared_ptr.h:25
small_shared_ptrt::small_shared_ptrt
small_shared_ptrt()=default
small_shared_ptrt::operator->
T * operator->() const
Definition
small_shared_ptr.h:92
small_shared_ptrt::~small_shared_ptrt
~small_shared_ptrt()
Definition
small_shared_ptr.h:63
small_shared_ptrt::use_count
auto use_count() const -> decltype(pointee_use_count(std::declval< T >()))
Definition
small_shared_ptr.h:97
small_shared_ptrt::small_shared_ptrt
small_shared_ptrt(T *t)
Definition
small_shared_ptr.h:29
small_shared_ptrt::small_shared_ptrt
small_shared_ptrt(const small_shared_ptrt &rhs)
Definition
small_shared_ptr.h:37
small_shared_ptrt::t_
T * t_
Definition
small_shared_ptr.h:108
small_shared_ptrt::operator=
small_shared_ptrt & operator=(const small_shared_ptrt &rhs)
Definition
small_shared_ptr.h:45
small_shared_ptrt::get
T * get() const
Definition
small_shared_ptr.h:82
small_shared_ptrt::operator*
T & operator*() const
Definition
small_shared_ptr.h:87
small_shared_ptrt::small_shared_ptrt
small_shared_ptrt(small_shared_ptrt &&rhs)
Definition
small_shared_ptr.h:52
small_shared_ptrt::operator=
small_shared_ptrt & operator=(small_shared_ptrt &&rhs)
Definition
small_shared_ptr.h:57
small_shared_ptrt::swap
void swap(small_shared_ptrt &rhs)
Definition
small_shared_ptr.h:77
std
STL namespace.
pointee_decrement_use_count
void pointee_decrement_use_count(small_shared_pointeet< Num > &p)
Definition
small_shared_ptr.h:239
operator<
bool operator<(const small_shared_ptrt< T > &lhs, const small_shared_ptrt< U > &rhs)
Definition
small_shared_ptr.h:145
make_small_shared_ptr
small_shared_ptrt< T > make_small_shared_ptr(Ts &&... ts)
This function is similar to std::make_unique and std::make_shared, and should be the preferred way of...
Definition
small_shared_ptr.h:123
operator>=
bool operator>=(const small_shared_ptrt< T > &lhs, const small_shared_ptrt< U > &rhs)
Definition
small_shared_ptr.h:165
pointee_increment_use_count
void pointee_increment_use_count(small_shared_pointeet< Num > &p)
The following functions are required by small_shared_ptrt, and by default pass through to the member ...
Definition
small_shared_ptr.h:233
operator<<
std::ostream & operator<<(std::ostream &os, const small_shared_ptrt< T > &ptr)
Definition
small_shared_ptr.h:112
operator>
bool operator>(const small_shared_ptrt< T > &lhs, const small_shared_ptrt< U > &rhs)
Definition
small_shared_ptr.h:151
operator<=
bool operator<=(const small_shared_ptrt< T > &lhs, const small_shared_ptrt< U > &rhs)
Definition
small_shared_ptr.h:157
operator==
bool operator==(const small_shared_ptrt< T > &lhs, const small_shared_ptrt< U > &rhs)
Definition
small_shared_ptr.h:129
pointee_use_count
Num pointee_use_count(const small_shared_pointeet< Num > &p)
Definition
small_shared_ptr.h:245
operator!=
bool operator!=(const small_shared_ptrt< T > &lhs, const small_shared_ptrt< U > &rhs)
Definition
small_shared_ptr.h:137
util
small_shared_ptr.h
Generated by
1.17.0