Repository navigation
Expand file tree
/
Copy pathResourceArray.h
More file actions
409 lines (358 loc) · 12.3 KB
/
Copy pathResourceArray.h
File metadata and controls
409 lines (358 loc) · 12.3 KB
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*
* Copyright (c) 2017 Ambroz Bizjak
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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.
*/
#ifndef AIPSTACK_RESOURCE_ARRAY_H
#define AIPSTACK_RESOURCE_ARRAY_H
#include <cstddef>
#include <type_traits>
#include <utility>
#include <aipstack/misc/ExceptionUtils.h>
namespace AIpStack {
/**
* Dummy class used to select a @ref ResourceArray constructor.
*/
class ResourceArrayInitSame {};
/**
* @addtogroup misc
* @{
*/
template<typename Elem, std::size_t Size>
class ResourceArray;
#ifndef IN_DOXYGEN
namespace ResourceArrayPrivate {
struct DefaultConstructMixinArg {};
template<bool Has>
struct DefaultConstructMixin
{
DefaultConstructMixin (DefaultConstructMixinArg) {}
};
template<>
struct DefaultConstructMixin<false>
{
DefaultConstructMixin (DefaultConstructMixinArg) {}
DefaultConstructMixin () = delete;
DefaultConstructMixin (DefaultConstructMixin const &) = default;
DefaultConstructMixin (DefaultConstructMixin &&) = default;
DefaultConstructMixin & operator= (DefaultConstructMixin const &) = default;
DefaultConstructMixin & operator= (DefaultConstructMixin &&) = default;
};
template<bool Has>
struct CopyConstructMixin {};
template<>
struct CopyConstructMixin<false>
{
CopyConstructMixin () = default;
CopyConstructMixin (CopyConstructMixin const &) = delete;
CopyConstructMixin (CopyConstructMixin &&) = default;
CopyConstructMixin & operator= (CopyConstructMixin const &) = default;
CopyConstructMixin & operator= (CopyConstructMixin &&) = default;
};
template<bool Has>
struct MoveConstructMixin {};
template<>
struct MoveConstructMixin<false>
{
MoveConstructMixin () = default;
MoveConstructMixin (MoveConstructMixin const &) = default;
MoveConstructMixin (MoveConstructMixin &&) = delete;
MoveConstructMixin & operator= (MoveConstructMixin const &) = default;
MoveConstructMixin & operator= (MoveConstructMixin &&) = default;
};
template<bool Has>
struct CopyAssignMixin {};
template<>
struct CopyAssignMixin<false>
{
CopyAssignMixin () = default;
CopyAssignMixin (CopyAssignMixin const &) = default;
CopyAssignMixin (CopyAssignMixin &&) = default;
CopyAssignMixin & operator= (CopyAssignMixin const &) = delete;
CopyAssignMixin & operator= (CopyAssignMixin &&) = default;
};
template<bool Has>
struct MoveAssignMixin {};
template<>
struct MoveAssignMixin<false>
{
MoveAssignMixin () = default;
MoveAssignMixin (MoveAssignMixin const &) = default;
MoveAssignMixin (MoveAssignMixin &&) = default;
MoveAssignMixin & operator= (MoveAssignMixin const &) = default;
MoveAssignMixin & operator= (MoveAssignMixin &&) = delete;
};
template<typename Elem, std::size_t Size>
class ArrayBase
{
friend ResourceArray<Elem, Size>;
using Storage = std::aligned_storage_t<sizeof(Elem), alignof(Elem)>;
static_assert(sizeof(Storage) == sizeof(Elem));
private:
Storage m_arr[Size];
public:
ArrayBase ()
{
std::size_t i;
AIPSTACK_TRY {
for (i = 0; i < Size; i++) {
new(elem_ptr(i)) Elem();
}
}
AIPSTACK_CATCH(..., {
destruct_elems(i);
throw;
})
}
ArrayBase (ArrayBase const &other)
{
std::size_t i;
AIPSTACK_TRY {
for (i = 0; i < Size; i++) {
new(elem_ptr(i)) Elem(*other.elem_ptr(i));
}
}
AIPSTACK_CATCH(..., {
destruct_elems(i);
throw;
})
}
ArrayBase (ArrayBase &&other)
{
std::size_t i;
AIPSTACK_TRY {
for (i = 0; i < Size; i++) {
new(elem_ptr(i)) Elem(std::move(*other.elem_ptr(i)));
}
}
AIPSTACK_CATCH(..., {
destruct_elems(i);
throw;
})
}
template<typename ...Args>
ArrayBase (ResourceArrayInitSame, Args const & ... args)
{
std::size_t i;
AIPSTACK_TRY {
for (i = 0; i < Size; i++) {
new(elem_ptr(i)) Elem(args...);
}
}
AIPSTACK_CATCH(..., {
destruct_elems(i);
throw;
})
}
~ArrayBase ()
{
destruct_elems(Size);
}
ArrayBase & operator= (ArrayBase const &other)
{
for (std::size_t i = 0; i < Size; i++) {
elem_ptr(i)->operator=(*other.elem_ptr(i));
}
return *this;
}
ArrayBase & operator= (ArrayBase &&other)
{
for (std::size_t i = 0; i < Size; i++) {
elem_ptr(i)->operator=(std::move(*other.elem_ptr(i)));
}
return *this;
}
private:
inline Elem * elem_ptr (std::size_t index)
{
return reinterpret_cast<Elem *>(&m_arr) + index;
}
inline Elem const * elem_ptr (std::size_t index) const
{
return reinterpret_cast<Elem const *>(&m_arr) + index;
}
void destruct_elems (std::size_t count)
{
for (std::size_t i = count; i > 0; i--) {
elem_ptr(i - 1)->Elem::~Elem();
}
}
};
}
#endif
/**
* Container for a statically-sized array.
*
* This class contains a statically-sized array of a specific type. It allows construction
* by constructing all array elements using the same arguments. This is why this class
* exists as opposed to using `std::array`.
*
* This class emulates the C++ default definitions of various special functions:
* - Default constructor: defined if `Elem` is default-constructible, otherwise deleted.
* - Copy-constructor: defined if `Elem` is copy-constructible, otherwise deleted.
* - Move-constructor: defined if `Elem` is move-constructible, otherwise deleted.
* - Copy-assignment operator: defined if `Elem` is copy-assignable, otherwise deleted.
* - Move-assignment operator: defined if `Elem` is move-assignable, otherwise deleted.
*
* The behavior also emulates the default C++ behavior. In particular:
* - All the three constructors construct the array elements in order using the
* corresponding type of constructor for `Elem`. If construction of any element throws,
* any successfully constructed elements are first destructed and the exception is then
* re-throwed.
* - The two assignment operators assign the array elements in order using the corresponding
* type of assignment operator.
* - The destructor destructs the array elements in reverse order.
*
* This class provides element access via the array operator and supports iteration over
* array elements using range-based for loops.
*
* @tparam Elem Type of array elements.
* @tparam Size Number of array elements. Must be positive.
*/
template<typename Elem, std::size_t Size>
class ResourceArray
#ifndef IN_DOXYGEN
:private ResourceArrayPrivate::ArrayBase<Elem, Size>,
private ResourceArrayPrivate::DefaultConstructMixin<std::is_default_constructible_v<Elem>>,
private ResourceArrayPrivate::CopyConstructMixin<std::is_copy_constructible_v<Elem>>,
private ResourceArrayPrivate::MoveConstructMixin<std::is_move_constructible_v<Elem>>,
private ResourceArrayPrivate::CopyAssignMixin<std::is_copy_assignable_v<Elem>>,
private ResourceArrayPrivate::MoveAssignMixin<std::is_move_assignable_v<Elem>>
#endif
{
public:
/**
* Default constructor (possibly deleted).
*/
ResourceArray () = default;
/**
* Construct array elements using the given construction arguments for each element.
*
* @tparam Args Types of arguments used for constructing elements.
* @param args Arguments used for constructing each element (all these are used for each
* element). Note that they are given by and passed to element constructors by
* const reference.
*/
template<typename ...Args>
ResourceArray (ResourceArrayInitSame, Args const & ... args) :
ResourceArrayPrivate::ArrayBase<Elem, Size>(ResourceArrayInitSame(), args...),
ResourceArrayPrivate::DefaultConstructMixin<std::is_default_constructible_v<Elem>>(ResourceArrayPrivate::DefaultConstructMixinArg())
{
}
/**
* Return a reference to the element at the given index (non-const).
*
* @param index Index of element. Must be less than `Size`.
* @return Reference to the element at index `index`.
*/
inline Elem & operator[] (std::size_t index)
{
return *this->elem_ptr(index);
}
/**
* Return a reference to the element at the given index (const).
*
* @param index Index of element. Must be less than `Size`.
* @return Reference to the element at index `index`.
*/
inline Elem const & operator[] (std::size_t index) const
{
return *this->elem_ptr(index);
}
/**
* Return a pointer to the array of elements (non-const).
*
* @return Pointer to the array of elements.
*/
inline Elem * data ()
{
return this->elem_ptr(0);
}
/**
* Return a pointer to the array of elements (const).
*
* @return Pointer to the array of elements.
*/
inline Elem const * data () const
{
return this->elem_ptr(0);
}
/**
* Return the number of elements.
*
* @return `Size`
*/
inline constexpr static std::size_t size ()
{
return Size;
}
/**
* Iterator type (pointer to element).
*/
using iterator = Elem *;
/**
* Const iterator type (const pointer to element).
*/
using const_iterator = Elem const *;
/**
* Return the begin iterator (non-const).
*
* This is equivalent to @ref data().
*
* @return Begin iterator (pointer to start of array).
*/
inline iterator begin ()
{
return this->elem_ptr(0);
}
/**
* Return the begin iterator (const).
*
* This is equivalent to @ref data() const.
*
* @return Begin iterator (pointer to start of array).
*/
inline const_iterator begin () const
{
return this->elem_ptr(0);
}
/**
* Return the end iterator (non-const).
*
* @return End iterator (pointer to end of array).
*/
inline iterator end ()
{
return this->elem_ptr(Size);
}
/**
* Return the end iterator (const).
*
* @return End iterator (pointer to end of array).
*/
inline const_iterator end () const
{
return this->elem_ptr(Size);
}
};
/** @} */
}
#endif