Lagrange
value_ptr.h
1// Source: https://github.com/LoopPerfect/valuable
2// SPDX-License-Identifier: MIT
3//
4// This file has been modified by Adobe.
5//
6// All modifications are Copyright 2022 Adobe.
7//
8// MIT License
9//
10// Copyright (c) 2017 LoopPerfect
11//
12// Permission is hereby granted, free of charge, to any person obtaining a copy
13// of this software and associated documentation files (the "Software"), to deal
14// in the Software without restriction, including without limitation the rights
15// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16// copies of the Software, and to permit persons to whom the Software is
17// furnished to do so, subject to the following conditions:
18//
19// The above copyright notice and this permission notice shall be included in all
20// copies or substantial portions of the Software.
22// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28// SOFTWARE.
29
30#pragma once
31
32#include <memory>
33
36
37#ifndef LA_DECLSPEC_EMPTY_BASES
38#ifdef _MSC_VER
39#define LA_DECLSPEC_EMPTY_BASES __declspec(empty_bases)
40#else
41#define LA_DECLSPEC_EMPTY_BASES
42#endif
43#endif
44
45namespace lagrange {
46
48
49namespace detail {
50
51struct spacer
52{
53};
54
55// For details of class_tag use here, see
56// https://mortoray.com/2013/06/03/overriding-the-broken-universal-reference-t/
57template <typename T>
58struct class_tag
59{
60};
61
62template <class T, class Deleter, class T2>
63struct LA_DECLSPEC_EMPTY_BASES compressed_ptr : std::unique_ptr<T, Deleter>, T2
64{
65 using T1 = std::unique_ptr<T, Deleter>;
66 compressed_ptr() = default;
67 compressed_ptr(compressed_ptr&&) = default;
68 compressed_ptr(const compressed_ptr&) = default;
69 compressed_ptr(T2&& a2)
70 : T2(std::move(a2))
71 {}
72 compressed_ptr(const T2& a2)
73 : T2(a2)
74 {}
75 template <typename A1>
76 compressed_ptr(A1&& a1)
77 : compressed_ptr(
78 std::forward<A1>(a1),
79 class_tag<typename std::decay<A1>::type>(),
80 spacer(),
81 spacer())
82 {}
83 template <typename A1, typename A2>
84 compressed_ptr(A1&& a1, A2&& a2)
85 : compressed_ptr(
86 std::forward<A1>(a1),
87 std::forward<A2>(a2),
88 class_tag<typename std::decay<A2>::type>(),
89 spacer())
90 {}
91 template <typename A1, typename A2, typename A3>
92 compressed_ptr(A1&& a1, A2&& a2, A3&& a3)
93 : T1(std::forward<A1>(a1), std::forward<A2>(a2))
94 , T2(std::forward<A3>(a3))
95 {}
96
97 template <typename A1>
98 compressed_ptr(A1&& a1, class_tag<typename std::decay<A1>::type>, spacer, spacer)
99 : T1(std::forward<A1>(a1))
100 {}
101 template <typename A1, typename A2>
102 compressed_ptr(A1&& a1, A2&& a2, class_tag<Deleter>, spacer)
103 : T1(std::forward<A1>(a1), std::forward<A2>(a2))
104 {}
105 template <typename A1, typename A2>
106 compressed_ptr(A1&& a1, A2&& a2, class_tag<T2>, spacer)
107 : T1(std::forward<A1>(a1))
108 , T2(std::forward<A2>(a2))
109 {}
110};
111
112} // namespace detail
113
114template <typename T>
115struct default_clone
116{
117 default_clone() = default;
118 T* operator()(T const& x) const { return new T(x); }
119 T* operator()(T&& x) const { return new T(std::move(x)); }
120};
121
123
132template <class T, class Cloner = default_clone<T>, class Deleter = std::default_delete<T>>
134{
135 ::lagrange::detail::compressed_ptr<T, Deleter, Cloner> ptr_;
136
137 std::unique_ptr<T, Deleter>& ptr() { return ptr_; }
138 std::unique_ptr<T, Deleter> const& ptr() const { return ptr_; }
139
140 T* clone(T const& x) const { return get_cloner()(x); }
141
142public:
143 using pointer = T*;
144 using element_type = T;
145 using cloner_type = Cloner;
146 using deleter_type = Deleter;
147
148 value_ptr() = default;
149
150 value_ptr(const T& value)
151 : ptr_(cloner_type()(value))
152 {}
153 value_ptr(T&& value)
154 : ptr_(cloner_type()(std::move(value)))
155 {}
156
157 value_ptr(const Cloner& value)
158 : ptr_(value)
159 {}
160 value_ptr(Cloner&& value)
161 : ptr_(value)
162 {}
163
164 template <typename V, typename ClonerOrDeleter>
165 value_ptr(V&& value, ClonerOrDeleter&& a2)
166 : ptr_(std::forward<V>(value), std::forward<ClonerOrDeleter>(a2))
167 {}
168
169 template <typename V, typename C, typename D>
170 value_ptr(V&& value, C&& cloner, D&& deleter)
171 : ptr_(std::forward<V>(value), std::forward<D>(deleter), std::forward<C>(cloner))
172 {}
173
174 value_ptr(value_ptr const& v)
175 : ptr_{nullptr, v.get_cloner()}
176 {
177 if (v) {
178 ptr().reset(clone(*v));
179 }
180 }
181 value_ptr(value_ptr&& v) = default;
182
183 explicit value_ptr(pointer value)
184 : ptr_(value)
185 {}
186 pointer release() { return ptr().release(); }
187
188 T* get() noexcept { return ptr().get(); }
189 T const* get() const noexcept { return ptr().get(); }
190
191 Cloner& get_cloner() noexcept { return ptr_; }
192 Cloner const& get_cloner() const noexcept { return ptr_; }
193
194 Deleter& get_deleter() noexcept { return ptr_; }
195 Deleter const& get_deleter() const noexcept { return ptr_; }
196
197 T& operator*() { return *get(); }
198 T const& operator*() const { return *get(); }
199
200 T const* operator->() const noexcept { return get(); }
201 T* operator->() noexcept { return get(); }
202
203 value_ptr<T>& operator=(value_ptr&& v) noexcept
204 {
205 ptr() = std::move(v.ptr());
206 get_cloner() = std::move(v.get_cloner());
207 return *this;
208 }
209
210 value_ptr<T>& operator=(value_ptr const& v)
211 {
212 ptr().reset(v.get_cloner()(*v));
213 get_cloner() = v.get_cloner();
214 return *this;
215 }
216
217 operator bool() const noexcept { return !!ptr(); }
218 ~value_ptr() = default;
219};
220
231template <class T, class... Args>
233{
234 return value_ptr<T>(new T(std::forward<Args>(args)...));
235}
236
238
239} // namespace lagrange
Smart pointer with value semantics.
Definition: value_ptr.h:134
value_ptr< T > make_value_ptr(Args &&... args)
Helper function to create a value_ptr for a given type.
Definition: value_ptr.h:232
Main namespace for Lagrange.
Definition: AABBIGL.h:30
Definition: project.cpp:27