forked from pcb2gcode/pcb2gcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
value_ptr.hpp
280 lines (220 loc) · 11.1 KB
/
value_ptr.hpp
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
// Copyright 2017-2018 by Tom Clunie
// https://github.com/clunietp/value_ptr
// Distributed under the Boost Software License, Version 1.0.
// (See http://www.boost.org/LICENSE_1_0.txt)
#ifndef SMART_PTR_VALUE_PTR
#define SMART_PTR_VALUE_PTR
#include <memory> // std::unique_ptr
#include <functional> // std::less
#include <cassert> // assert
#if defined( _MSC_VER)
#if (_MSC_VER >= 1915) // constexpr tested/working _MSC_VER 1915 (vs17 15.8)
#define VALUE_PTR_CONSTEXPR constexpr
#else // msvc 15 bug prevents constexpr in some cases
#define VALUE_PTR_CONSTEXPR
#endif
// https://blogs.msdn.microsoft.com/vcblog/2016/03/30/optimizing-the-layout-of-empty-base-classes-in-vs2015-update-2-3/
#define VALUE_PTR_USE_EMPTY_BASE_OPTIMIZATION __declspec(empty_bases) // requires vs2015 update 2 or later. still needed vs2017
#else
#define VALUE_PTR_CONSTEXPR constexpr
#define VALUE_PTR_USE_EMPTY_BASE_OPTIMIZATION
#endif
namespace smart_ptr {
namespace detail {
// has clone() method detection
template<class T, class = void> struct has_clone : std::false_type {};
template<class T> struct has_clone<T, decltype(void(std::declval<T>().clone()))> : std::true_type {};
// Returns flag if test passes (false==slicing is probable)
// T==base pointer, U==derived/supplied pointer
template <typename T, typename U, bool IsDefaultCopier>
struct slice_test : std::integral_constant<bool,
std::is_same<T, U>::value // if U==T, no need to check for slicing
|| std::is_same<std::nullptr_t, U>::value // nullptr is fine
|| !IsDefaultCopier // user provided cloner, assume they're handling it
|| has_clone<typename std::remove_pointer<U>::type>::value // using default cloner, clone method must exist in U
>::type
{};
// default copier/cloner, analogous to std::default_delete
template <typename T>
struct default_copy {
private:
struct _clone_tag {};
struct _copy_tag {};
T* operator()(const T* what, _clone_tag) const { return what->clone(); }
T* operator()(const T* what, _copy_tag) const { return new T(*what); }
public:
T* operator()(const T* what) const { // copy operator
if (!what)
return nullptr;
return this->operator()(what, typename std::conditional<detail::has_clone<T>::value, _clone_tag, _copy_tag>::type()); // tag dispatch on has_clone
} //
}; // default_copy
// ptr_data: holds pointer, deleter, copier
// pointer and deleter held in unique_ptr member, this struct is derived from copier to minimize overall footprint
// uses EBCO to solve sizeof(value_ptr<T>) == sizeof(T*) problem
template <typename T, typename Deleter, typename Copier>
struct
VALUE_PTR_USE_EMPTY_BASE_OPTIMIZATION
ptr_data
: public Copier
{
using unique_ptr_type = std::unique_ptr<T, Deleter>;
using pointer = typename unique_ptr_type::pointer;
using deleter_type = typename unique_ptr_type::deleter_type;
using copier_type = Copier;
unique_ptr_type uptr;
ptr_data() = default;
template <typename Dx, typename Cx>
constexpr ptr_data( T* px, Dx&& dx, Cx&& cx )
: copier_type( std::forward<Cx>(cx) )
, uptr(px, std::forward<Dx>(dx))
{}
ptr_data( ptr_data&& ) = default;
ptr_data& operator=( ptr_data&& ) = default;
constexpr ptr_data( const ptr_data& that )
: ptr_data( that.clone() )
{}
ptr_data& operator=( const ptr_data& that ) {
if ( this != &that )
*this = that.clone();
return *this;
}
// get_copier, analogous to std::unique_ptr<T>::get_deleter()
copier_type& get_copier() noexcept { return *this; }
// get_copier, analogous to std::unique_ptr<T>::get_deleter()
const copier_type& get_copier() const noexcept { return *this; }
ptr_data clone() const {
// get a copier, use it to clone ptr, construct/return a ptr_data
return{
this->get_copier()(this->uptr.get())
, this->uptr.get_deleter()
, this->get_copier()
};
}
}; // ptr_data
} // detail
template <typename T
, typename Deleter = std::default_delete<T>
, typename Copier = detail::default_copy<T>
>
struct value_ptr {
using deleter_type = Deleter;
using copier_type = Copier;
using _data_type = detail::ptr_data<T, deleter_type, copier_type>;
using unique_ptr_type = typename _data_type::unique_ptr_type;
using element_type = T;
using pointer = typename _data_type::pointer;
using reference = typename std::add_lvalue_reference<element_type>::type;
_data_type _data;
// construct with pointer
template <typename Px, typename Dx = Deleter, typename Cx = Copier, typename = typename std::enable_if<std::is_convertible<Px, pointer>::value>::type>
VALUE_PTR_CONSTEXPR
value_ptr(Px px, Dx&& dx = {}, Cx&& cx = {} )
: _data(
std::forward<Px>(px)
, std::forward<Dx>(dx)
, std::forward<Cx>(cx)
)
{
static_assert(
detail::slice_test<pointer, Px, std::is_convertible<detail::default_copy<T>, Copier>::value>::value
, "value_ptr; clone() method not detected and not using custom copier; slicing may occur"
);
}
// construct from unique_ptr, copier
VALUE_PTR_CONSTEXPR
value_ptr( std::unique_ptr<T, Deleter> uptr, Copier copier = {})
: value_ptr(uptr.release(), std::move( uptr.get_deleter() ), std::move(copier))
{}
// std::nullptr_t, default ctor
explicit
VALUE_PTR_CONSTEXPR
value_ptr(std::nullptr_t = nullptr)
: value_ptr(nullptr, Deleter(), Copier())
{}
// return unique_ptr, ref qualified
const unique_ptr_type& uptr() const & noexcept {
return this->_data.uptr;
}
// return unique_ptr, ref qualified
unique_ptr_type& uptr() & noexcept {
return this->_data.uptr;
}
// conversion to unique_ptr, ref qualified
operator unique_ptr_type const&() const & noexcept {
return this->uptr();
}
// conversion to unique_ptr, ref qualified
operator unique_ptr_type& () & noexcept {
return this->uptr();
}
deleter_type& get_deleter() noexcept { return this->uptr().get_deleter(); }
const deleter_type& get_deleter() const noexcept { return this->uptr().get_deleter(); }
copier_type& get_copier() noexcept { return this->_data.get_copier(); }
const copier_type& get_copier() const noexcept { return this->_data.get_copier(); }
// get pointer
pointer get() const noexcept { return this->uptr().get(); }
// reset pointer to compatible type
template <typename Px, typename = typename std::enable_if<std::is_convertible<Px, pointer>::value>::type>
void reset(Px px) {
static_assert(
detail::slice_test<pointer, Px, std::is_same<detail::default_copy<T>, Copier>::value>::value
, "value_ptr; clone() method not detected and not using custom copier; slicing may occur"
);
*this = value_ptr(std::forward<Px>(px), std::move( this->get_deleter() ), std::move( this->get_copier() ) );
}
// reset pointer
void reset() { this->reset(nullptr); }
// release pointer
pointer release() noexcept {
return this->uptr().release();
} // release
// return flag if has pointer
explicit operator bool() const noexcept {
return this->get() != nullptr;
}
// return reference to T, UB if null
reference operator*() const noexcept { return *this->get(); }
// return pointer to T
pointer operator-> () const noexcept { return this->get(); }
// swap with other value_ptr
void swap(value_ptr& that) { std::swap(this->_data, that._data); }
}; // value_ptr
// non-member swap
template <class T1, class D1, class C1, class T2, class D2, class C2> void swap( value_ptr<T1, D1, C1>& x, value_ptr<T2, D2, C2>& y ) { x.swap( y ); }
// non-member operators, based on https://en.cppreference.com/w/cpp/memory/unique_ptr/operator_cmp
template <class T1, class D1, class C1, class T2, class D2, class C2> bool operator == ( const value_ptr<T1, D1, C1>& x, const value_ptr<T2, D2, C2>& y ) { return x.get() == y.get(); }
template<class T1, class D1, class C1, class T2, class D2, class C2> bool operator != ( const value_ptr<T1, D1, C1>& x, const value_ptr<T2, D2, C2>& y ) { return x.get() != y.get(); }
template<class T1, class D1, class C1, class T2, class D2, class C2> bool operator < ( const value_ptr<T1, D1, C1>& x, const value_ptr<T2, D2, C2>& y ) {
using common_type = typename std::common_type<typename value_ptr<T1, D1, C1>::pointer, typename value_ptr<T2, D2, C2>::pointer>::type;
return std::less<common_type>()( x.get(), y.get() );
}
template<class T1, class D1, class C1, class T2, class D2, class C2> bool operator <= ( const value_ptr<T1, D1, C1>& x, const value_ptr<T2, D2, C2>& y ) { return !( y < x ); }
template<class T1, class D1, class C1, class T2, class D2, class C2> bool operator > ( const value_ptr<T1, D1, C1>& x, const value_ptr<T2, D2, C2>& y ) { return y < x; }
template<class T1, class D1, class C1, class T2, class D2, class C2> bool operator >= ( const value_ptr<T1, D1, C1>& x, const value_ptr<T2, D2, C2>& y ) { return !( x < y ); }
template <class T, class D, class C> bool operator == ( const value_ptr<T, D, C>& x, std::nullptr_t ) noexcept { return !x; }
template <class T, class D, class C> bool operator == (std::nullptr_t, const value_ptr<T, D, C>& y) noexcept { return !y; }
template <class T, class D, class C> bool operator != (const value_ptr<T, D, C>& x, std::nullptr_t) noexcept { return (bool)x; }
template <class T, class D, class C> bool operator != (std::nullptr_t, const value_ptr<T, D, C>& y) noexcept { return (bool)y; }
template <class T, class D, class C> bool operator < (const value_ptr<T, D, C>& x, std::nullptr_t) { return std::less<typename value_ptr<T, D, C>::pointer>()(x.get(), nullptr); }
template <class T, class D, class C> bool operator < (std::nullptr_t, const value_ptr<T, D, C>& y) { return std::less<typename value_ptr<T, D, C>::pointer>()(nullptr, y.get()); }
template <class T, class D, class C> bool operator <= (const value_ptr<T, D, C>& x, std::nullptr_t) { return !(nullptr < x); }
template <class T, class D, class C> bool operator <= (std::nullptr_t, const value_ptr<T, D, C>& y) { return !(y < nullptr); }
template <class T, class D, class C> bool operator > (const value_ptr<T, D, C>& x, std::nullptr_t) { return nullptr < x; }
template <class T, class D, class C> bool operator > (std::nullptr_t, const value_ptr<T, D, C>& y) { return y < nullptr; }
template <class T, class D, class C> bool operator >= (const value_ptr<T, D, C>& x, std::nullptr_t) { return !(x < nullptr); }
template <class T, class D, class C> bool operator >= (std::nullptr_t, const value_ptr<T, D, C>& y) { return !(nullptr < y); }
// make value_ptr with default deleter and copier, analogous to std::make_unique
template<typename T, typename... Args>
value_ptr<T> make_value(Args&&... args) {
return value_ptr<T>(new T(std::forward<Args>(args)...));
}
// make a value_ptr from pointer with custom deleter and copier
template <typename T, typename Deleter = std::default_delete<T>, typename Copier = detail::default_copy<T>>
static inline auto make_value_ptr(T* ptr, Deleter&& dx = {}, Copier&& cx = {}) -> value_ptr<T, Deleter, Copier> {
return value_ptr<T, Deleter, Copier>( ptr, std::forward<Deleter>( dx ), std::forward<Copier>(cx) );
} // make_value_ptr
} // smart_ptr ns
#undef VALUE_PTR_CONSTEXPR
#undef VALUE_PTR_USE_EMPTY_BASE_OPTIMIZATION
#endif // !SMART_PTR_VALUE_PTR