-
Notifications
You must be signed in to change notification settings - Fork 23
/
value_ptr.h
239 lines (203 loc) · 6.74 KB
/
value_ptr.h
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
// Source: https://github.com/LoopPerfect/valuable
// SPDX-License-Identifier: MIT
//
// This file has been modified by Adobe.
//
// All modifications are Copyright 2022 Adobe.
//
// MIT License
//
// Copyright (c) 2017 LoopPerfect
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
///
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#pragma once
#include <memory>
/// @addtogroup group-utils-misc
/// @{
#ifndef LA_DECLSPEC_EMPTY_BASES
#ifdef _MSC_VER
#define LA_DECLSPEC_EMPTY_BASES __declspec(empty_bases)
#else
#define LA_DECLSPEC_EMPTY_BASES
#endif
#endif
namespace lagrange {
/// @cond LA_INTERNAL_DOCS
namespace detail {
struct spacer
{
};
// For details of class_tag use here, see
// https://mortoray.com/2013/06/03/overriding-the-broken-universal-reference-t/
template <typename T>
struct class_tag
{
};
template <class T, class Deleter, class T2>
struct LA_DECLSPEC_EMPTY_BASES compressed_ptr : std::unique_ptr<T, Deleter>, T2
{
using T1 = std::unique_ptr<T, Deleter>;
compressed_ptr() = default;
compressed_ptr(compressed_ptr&&) = default;
compressed_ptr(const compressed_ptr&) = default;
compressed_ptr(T2&& a2)
: T2(std::move(a2))
{}
compressed_ptr(const T2& a2)
: T2(a2)
{}
template <typename A1>
compressed_ptr(A1&& a1)
: compressed_ptr(
std::forward<A1>(a1),
class_tag<typename std::decay<A1>::type>(),
spacer(),
spacer())
{}
template <typename A1, typename A2>
compressed_ptr(A1&& a1, A2&& a2)
: compressed_ptr(
std::forward<A1>(a1),
std::forward<A2>(a2),
class_tag<typename std::decay<A2>::type>(),
spacer())
{}
template <typename A1, typename A2, typename A3>
compressed_ptr(A1&& a1, A2&& a2, A3&& a3)
: T1(std::forward<A1>(a1), std::forward<A2>(a2))
, T2(std::forward<A3>(a3))
{}
template <typename A1>
compressed_ptr(A1&& a1, class_tag<typename std::decay<A1>::type>, spacer, spacer)
: T1(std::forward<A1>(a1))
{}
template <typename A1, typename A2>
compressed_ptr(A1&& a1, A2&& a2, class_tag<Deleter>, spacer)
: T1(std::forward<A1>(a1), std::forward<A2>(a2))
{}
template <typename A1, typename A2>
compressed_ptr(A1&& a1, A2&& a2, class_tag<T2>, spacer)
: T1(std::forward<A1>(a1))
, T2(std::forward<A2>(a2))
{}
};
} // namespace detail
template <typename T>
struct default_clone
{
default_clone() = default;
T* operator()(T const& x) const { return new T(x); }
T* operator()(T&& x) const { return new T(std::move(x)); }
};
/// @endcond
///
/// Smart pointer with value semantics. Copy/moving the pointer will copy/move the underlying
/// object. This is useful to implement PIMPL idioms.
///
/// @tparam T Object type of the pointee.
/// @tparam Cloner Optional cloner functor.
/// @tparam Deleter Optional deleter functor.
///
template <class T, class Cloner = default_clone<T>, class Deleter = std::default_delete<T>>
class value_ptr
{
::lagrange::detail::compressed_ptr<T, Deleter, Cloner> ptr_;
std::unique_ptr<T, Deleter>& ptr() { return ptr_; }
std::unique_ptr<T, Deleter> const& ptr() const { return ptr_; }
T* clone(T const& x) const { return get_cloner()(x); }
public:
using pointer = T*;
using element_type = T;
using cloner_type = Cloner;
using deleter_type = Deleter;
value_ptr() = default;
value_ptr(const T& value)
: ptr_(cloner_type()(value))
{}
value_ptr(T&& value)
: ptr_(cloner_type()(std::move(value)))
{}
value_ptr(const Cloner& value)
: ptr_(value)
{}
value_ptr(Cloner&& value)
: ptr_(value)
{}
template <typename V, typename ClonerOrDeleter>
value_ptr(V&& value, ClonerOrDeleter&& a2)
: ptr_(std::forward<V>(value), std::forward<ClonerOrDeleter>(a2))
{}
template <typename V, typename C, typename D>
value_ptr(V&& value, C&& cloner, D&& deleter)
: ptr_(std::forward<V>(value), std::forward<D>(deleter), std::forward<C>(cloner))
{}
value_ptr(value_ptr const& v)
: ptr_{nullptr, v.get_cloner()}
{
if (v) {
ptr().reset(clone(*v));
}
}
value_ptr(value_ptr&& v) = default;
explicit value_ptr(pointer value)
: ptr_(value)
{}
pointer release() { return ptr().release(); }
T* get() noexcept { return ptr().get(); }
T const* get() const noexcept { return ptr().get(); }
Cloner& get_cloner() noexcept { return ptr_; }
Cloner const& get_cloner() const noexcept { return ptr_; }
Deleter& get_deleter() noexcept { return ptr_; }
Deleter const& get_deleter() const noexcept { return ptr_; }
T& operator*() { return *get(); }
T const& operator*() const { return *get(); }
T const* operator->() const noexcept { return get(); }
T* operator->() noexcept { return get(); }
value_ptr<T>& operator=(value_ptr&& v) noexcept
{
ptr() = std::move(v.ptr());
get_cloner() = std::move(v.get_cloner());
return *this;
}
value_ptr<T>& operator=(value_ptr const& v)
{
ptr().reset(v.get_cloner()(*v));
get_cloner() = v.get_cloner();
return *this;
}
operator bool() const noexcept { return !!ptr(); }
~value_ptr() = default;
};
///
/// Helper function to create a value_ptr for a given type.
///
/// @param args Arguments to forward to the object's constructor.
///
/// @tparam T Type of the object to create.
/// @tparam Args Argument types for the object's constructor.
///
/// @return A value_ptr of the desired type.
///
template <class T, class... Args>
value_ptr<T> make_value_ptr(Args&&... args)
{
return value_ptr<T>(new T(std::forward<Args>(args)...));
}
/// @}
} // namespace lagrange