/
smart_ptr.hpp
255 lines (204 loc) · 5.91 KB
/
smart_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
#ifndef SMART_PTR_HPP_DE65D1E8_C925_40F7_905A_B7E3FF40B486
#define SMART_PTR_HPP_DE65D1E8_C925_40F7_905A_B7E3FF40B486
#pragma once
/*
smart_ptr.hpp
*/
/*
Copyright © 2014 Far Group
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the authors may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "preprocessor.hpp"
#include "range.hpp"
#include "utility.hpp"
//----------------------------------------------------------------------------
template<typename T, size_t MinStaticSize, REQUIRES(std::is_trivially_copyable_v<T>)>
class array_ptr: public span<T>
{
public:
NONCOPYABLE(array_ptr);
array_ptr() noexcept
{
m_Buffer.template emplace<static_type>();
init_span(0);
}
explicit array_ptr(size_t const Size, bool Init = false):
array_ptr()
{
reset(Size, Init);
}
array_ptr(array_ptr&& rhs) noexcept
{
move_from(rhs);
}
array_ptr& operator=(array_ptr&& rhs) noexcept
{
return move_from(rhs);
}
void reset(size_t const Size = 0, bool const Init = false)
{
if (Size > StaticSize)
{
auto& DynamicBuffer = m_Buffer.template emplace<dynamic_type>();
// We don't need a strong guarantee here, so it's better to reduce memory usage
DynamicBuffer.reset();
DynamicBuffer.reset(Init? new T[Size]() : new T[Size]);
}
else
{
m_Buffer.template emplace<static_type>();
}
init_span(Size);
}
[[nodiscard]]
explicit operator bool() const noexcept
{
return !this->empty();
}
[[nodiscard]]
T& operator*() const noexcept
{
assert(!this->empty());
return *this->data();
}
private:
using dynamic_type = std::unique_ptr<T[]>;
constexpr static size_t StaticSize = std::max(MinStaticSize, sizeof(dynamic_type) / sizeof(T));
using static_type = std::array<T, StaticSize>;
void init_span(size_t const Size) noexcept
{
static_cast<span<T>&>(*this) =
{
std::visit(overload
{
[](static_type& Data){ return Data.data(); },
[](dynamic_type& Data){ return Data.get(); }
}, m_Buffer),
Size
};
}
bool is_dynamic(size_t const Size) const noexcept
{
return Size > StaticSize;
}
bool is_dynamic() const noexcept
{
return is_dynamic(this->size());
}
array_ptr& move_from(array_ptr& rhs) noexcept
{
m_Buffer = std::move(rhs.m_Buffer);
init_span(rhs.size());
rhs.init_span(0);
return *this;
}
mutable std::variant<static_type, dynamic_type> m_Buffer;
};
template<size_t Size = 1>
using wchar_t_ptr_n = array_ptr<wchar_t, Size>;
template<size_t Size = 1>
using char_ptr_n = array_ptr<char, Size>;
using wchar_t_ptr = wchar_t_ptr_n<1>;
using char_ptr = char_ptr_n<1>;
template<typename T, size_t Size = 1, REQUIRES(std::is_trivially_copyable_v<T>)>
class block_ptr: public array_ptr<std::byte, Size>
{
public:
NONCOPYABLE(block_ptr);
MOVABLE(block_ptr);
using array_ptr<std::byte, Size>::array_ptr;
block_ptr() noexcept = default;
[[nodiscard]]
decltype(auto) data() const noexcept
{
assert(this->size() >= sizeof(T));
return edit_as<T*>(array_ptr<std::byte, Size>::data());
}
[[nodiscard]]
decltype(auto) operator->() const noexcept { return data(); }
[[nodiscard]]
decltype(auto) operator*() const noexcept {return *data();}
};
template <typename T>
class unique_ptr_with_ondestroy
{
public:
~unique_ptr_with_ondestroy() { OnDestroy(); }
[[nodiscard]]
decltype(auto) get() const noexcept { return ptr.get(); }
[[nodiscard]]
decltype(auto) operator->() const noexcept { return ptr.operator->(); }
[[nodiscard]]
decltype(auto) operator*() const { return *ptr; }
[[nodiscard]]
explicit operator bool() const noexcept { return ptr.operator bool(); }
decltype(auto) operator=(std::unique_ptr<T>&& value) noexcept { OnDestroy(); ptr = std::move(value); return *this; }
private:
void OnDestroy() { if (ptr) ptr->OnDestroy(); }
std::unique_ptr<T> ptr;
};
namespace detail
{
struct file_closer
{
void operator()(FILE* Object) const noexcept
{
fclose(Object);
}
};
}
using file_ptr = std::unique_ptr<FILE, detail::file_closer>;
template<typename T>
class ptr_setter
{
public:
NONCOPYABLE(ptr_setter);
explicit ptr_setter(T& Ptr): m_Ptr(&Ptr) {}
~ptr_setter() { m_Ptr->reset(m_RawPtr); }
[[nodiscard]]
auto operator&() { return &m_RawPtr; }
private:
T* m_Ptr;
typename T::pointer m_RawPtr{};
};
template<auto acquire, auto release, typename owner>
[[nodiscard]]
auto make_raii_wrapper(owner* Owner)
{
std::invoke(acquire, Owner);
struct releaser
{
void operator()(owner* const OwnerPtr) const
{
std::invoke(release, OwnerPtr);
}
};
return std::unique_ptr<owner, releaser>(Owner);
}
namespace detail
{
struct nop_deleter { void operator()(void*) const noexcept {} };
}
template<class T>
using movable_ptr = std::unique_ptr<T, detail::nop_deleter>;
#endif // SMART_PTR_HPP_DE65D1E8_C925_40F7_905A_B7E3FF40B486