-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic_storage.hpp
95 lines (79 loc) · 2.47 KB
/
basic_storage.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
#ifndef BASIC_STORAGE_HPP
#define BASIC_STORAGE_HPP
#include <utility>
#include "polymorphic_movable.hpp"
#include "polymorphic_copyable.hpp"
template <int N, typename Base>
struct basic_storage
{
protected:
template <typename T, typename... Args>
T * unsafe_construct(Args &&... args)
{
return ::new (buffer_) T(std::forward<Args>(args)...);
}
template <typename U>
U * unsafe_pointer()
{
return reinterpret_cast<U *>(buffer_);
}
template <typename U>
U const * unsafe_pointer() const
{
return reinterpret_cast<U const *>(buffer_);
}
template <typename U>
void unsafe_destroy()
{
unsafe_pointer<U>()->~U();
}
Base * unsafe_base_pointer()
{
return unsafe_pointer<Base>();
}
Base const * unsafe_base_pointer() const
{
return unsafe_pointer<Base>();
}
void unsafe_destroy_base()
{
unsafe_destroy<Base>();
}
void move_construct_in_place_base(Base * other)
{
// Type information would be necessary to determine if we could move
// with move constructor of specific type.
if constexpr (std::is_base_of_v<polymorphic_movable, Base>)
{
// Assumption: Buffer is not initialized or constructor has been called before.
polymorphic_movable * m = reinterpret_cast<polymorphic_movable *>(other);
m->polymorphic_move_construct_in_place(buffer_);
}
else
{
static_assert(std::is_base_of_v<polymorphic_movable, Base>,
"Move-construction requires that Base implements polymorphic_movable");
}
}
void copy_construct_in_place_base(Base const * other, std::byte * storage)
{
if constexpr (std::is_base_of_v<polymorphic_copyable, Base>)
{
// Assumption: Buffer is not initialized or constructor has been called before.
polymorphic_copyable const * c = reinterpret_cast<polymorphic_copyable const *>(other);
c->polymorphic_copy_construct_in_place(storage);
}
else
{
static_assert(std::is_base_of_v<polymorphic_copyable, Base>,
"Copy-construction requires that Base implements polymorphic_copyable");
}
}
void copy_construct_in_place_base(Base const * other)
{
copy_construct_in_place_base(other, buffer_);
}
private:
alignas(Base) std::byte buffer_[N];
};
#endif