-
Notifications
You must be signed in to change notification settings - Fork 0
/
bounded_storage.hpp
71 lines (55 loc) · 1.64 KB
/
bounded_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
#ifndef CLOSED_STORAGE_HPP
#define CLOSED_STORAGE_HPP
#include <concepts>
#include <type_traits>
#include "basic_storage.hpp"
template <typename T, int N>
concept storage_size_at_most = sizeof(T) <= N;
template <int N, storage_size_at_most<N> Base>
struct bounded_storage : basic_storage<N, Base>
{
template <typename Derived, typename... Args>
bounded_storage(std::type_identity<Derived> w, Args &&... args)
{
super::template unsafe_construct<Derived>(std::forward<Args>(args)...);
}
~bounded_storage()
{
super::unsafe_destroy_base();
}
bounded_storage(bounded_storage && other) noexcept
{
super::move_construct_in_place_base(other.pointer());
}
bounded_storage(bounded_storage const & other)
{
super::copy_construct_in_place_base(other.pointer());
}
bounded_storage & operator=(bounded_storage const & other)
{
super::unsafe_destroy_base();
super::copy_construct_in_place_base(other.pointer());
}
bounded_storage & operator=(bounded_storage && other) noexcept
{
super::unsafe_destroy_base();
super::move_construct_in_place_base(other.pointer());
}
template <std::derived_from<Base> Derived, typename... Args>
Derived * emplace(Args &&... args)
{
super::unsafe_destroy_base();
return super::template unsafe_construct<Derived>(std::forward<Args>(args)...);
}
Base * pointer()
{
return super::unsafe_base_pointer();
}
Base const * pointer() const
{
return super::unsafe_base_pointer();
}
private:
typedef basic_storage<N, Base> super;
};
#endif