Iros
 
Loading...
Searching...
No Matches
inline_storage.h
Go to the documentation of this file.
1#pragma once
2
5#include "di/meta/algorithm.h"
6#include "di/meta/language.h"
8#include "di/types/prelude.h"
9#include "di/util/addressof.h"
11#include "di/util/destroy_at.h"
12#include "di/util/exchange.h"
13#include "di/util/move.h"
14
15namespace di::any {
16namespace detail {
17 template<typename InlineStorage>
20
21 template<typename T>
22 void operator()(T&, InlineStorage*) const;
23 };
24
25 template<typename InlineStorage>
27}
28
29template<size_t inline_size, size_t inline_align>
31public:
34
35 constexpr static auto storage_category() -> StorageCategory { return StorageCategory::MoveOnly; }
36
37 template<typename T>
38 constexpr static auto creation_is_fallible(InPlaceType<T>) -> bool {
39 return false;
40 }
41
42 template<typename>
43 using CreationResult = void;
44
45 constexpr InlineStorage() = default;
46
47 InlineStorage(InlineStorage const&) = delete;
48 auto operator=(InlineStorage const&) -> InlineStorage& = delete;
49
50 template<typename T, typename... Args>
51 requires(sizeof(T) <= inline_size && alignof(T) <= inline_align && concepts::MoveConstructible<T> &&
53 constexpr static void init(InlineStorage* self, InPlaceType<T>, Args&&... args) {
54 util::construct_at(self->down_cast<T>(), util::forward<Args>(args)...);
55 }
56
57 ~InlineStorage() = default;
58
59 constexpr static void move_construct(concepts::VTableFor<Interface> auto& vtable, InlineStorage* dest,
60 InlineStorage* source) {
61 if (!vtable.empty()) {
62 auto const fp = vtable[Manage {}];
63 fp(dest, source);
64
65 vtable.reset();
66 }
67 }
68
69 template<concepts::VTableFor<Interface> VTable>
70 constexpr static void move_assign(VTable& dest_vtable, InlineStorage* dest, VTable& source_vtable,
71 InlineStorage* source) {
72 destroy(dest_vtable, dest);
73 dest_vtable = source_vtable;
74 move_construct(source_vtable, dest, source);
75 }
76
77 constexpr static void destroy(concepts::VTableFor<Interface> auto& vtable, InlineStorage* self) {
78 if (!vtable.empty()) {
79 auto const fp = vtable[Manage {}];
80 fp(self, nullptr);
81
82 vtable.reset();
83 }
84 }
85
86 template<typename T>
87 auto down_cast() -> T* {
88 return static_cast<T*>(address());
89 }
90
91 template<typename T>
92 auto down_cast() const -> T const* {
93 return static_cast<T const*>(address());
94 }
95
96private:
97 auto address() -> void* { return static_cast<void*>(util::addressof(m_storage[0])); }
98 auto address() const -> void const* { return static_cast<void const*>(util::addressof(m_storage[0])); }
99
100 alignas(inline_align) di::Byte m_storage[inline_size];
101};
102
103namespace detail {
104 template<typename InlineStorage>
105 template<typename T>
107 if (b) {
108 // Move from b into a.
109 auto* b_value = b->template down_cast<T>();
110 util::construct_at(util::addressof(a), util::move(*b_value));
111 util::destroy_at(b_value);
112 } else {
113 // Just destroy a.
114 util::destroy_at(util::addressof(a));
115 }
116 }
117}
118}
Definition operations.h:11
Definition operations.h:43
Definition vtable_for.h:17
Definition any.h:19
constexpr auto inline_storage_manage
Definition inline_storage.h:26
Definition any.h:18
StorageCategory
Definition storage_category.h:4
@ MoveOnly
Definition storage_category.h:9
T::Type Type
Definition core.h:26
std::byte Byte
Definition byte.h:63
constexpr auto destroy_at
Definition destroy_at.h:24
constexpr auto construct_at
Definition construct_at.h:27
constexpr auto destroy
Definition destroy.h:35
Definition inline_storage.h:30
static constexpr void move_construct(concepts::VTableFor< Interface > auto &vtable, InlineStorage *dest, InlineStorage *source)
Definition inline_storage.h:59
meta::List< Manage > Interface
Definition inline_storage.h:33
static constexpr auto creation_is_fallible(InPlaceType< T >) -> bool
Definition inline_storage.h:38
auto down_cast() -> T *
Definition inline_storage.h:87
static constexpr void move_assign(VTable &dest_vtable, InlineStorage *dest, VTable &source_vtable, InlineStorage *source)
Definition inline_storage.h:70
void CreationResult
Definition inline_storage.h:43
auto down_cast() const -> T const *
Definition inline_storage.h:92
static constexpr void destroy(concepts::VTableFor< Interface > auto &vtable, InlineStorage *self)
Definition inline_storage.h:77
meta::Type< detail::InlineStorageManage< InlineStorage > > Manage
Definition inline_storage.h:32
auto operator=(InlineStorage const &) -> InlineStorage &=delete
InlineStorage(InlineStorage const &)=delete
static constexpr void init(InlineStorage *self, InPlaceType< T >, Args &&... args)
Definition inline_storage.h:53
static constexpr auto storage_category() -> StorageCategory
Definition inline_storage.h:35
constexpr InlineStorage()=default
Definition inline_storage.h:18
void operator()(T &, InlineStorage *) const
Definition inline_storage.h:106
Method< InlineStorageManage, void(This &, InlineStorage *)> Type
Definition inline_storage.h:19
Definition core.h:5
Definition in_place_type.h:5
Definition method.h:7
Definition this.h:4