13USERVER_NAMESPACE_BEGIN
17namespace any_storage::impl {
19using Offset = std::size_t;
21template <
typename StorageTag>
22inline Offset data_offset{0};
24template <
typename StorageTag>
25inline std::size_t count{0};
27template <
typename StorageTag>
28Offset RegisterData(std::size_t size, std::size_t alignment)
noexcept {
29 data_offset<StorageTag> += (alignment - (data_offset<StorageTag> % alignment)) % alignment;
30 Offset result = data_offset<StorageTag>;
31 data_offset<StorageTag> += size;
37void AssertStaticRegistrationAllowed();
40void Delete(std::byte* data)
noexcept {
41 reinterpret_cast<T*>(data)->~T();
46template <
typename StorageTag>
49template <
typename StorageTag,
typename Data>
50class AnyStorageDataTag final {
52 AnyStorageDataTag()
noexcept
53 : number_(any_storage::impl::count<StorageTag>),
54 offset_(any_storage::impl::RegisterData<StorageTag>(
sizeof(Data),
alignof(Data))) {
55 static_assert(!std::is_reference_v<Data>);
56 static_assert(!std::is_const_v<Data>);
58 __STDCPP_DEFAULT_NEW_ALIGNMENT__ >=
alignof(Data),
59 "Overaligned data members are not supported by AnyStorage"
62 any_storage::impl::AssertStaticRegistrationAllowed();
66 const std::size_t number_;
67 const any_storage::impl::Offset offset_;
69 friend class AnyStorage<StorageTag>;
78template <
typename StorageTag>
79class AnyStorage final {
86 template <
typename Data>
87 const Data&
Get(
const AnyStorageDataTag<StorageTag, Data>& tag)
const;
91 template <
typename Data>
92 Data&
Get(
const AnyStorageDataTag<StorageTag, Data>& tag);
95 template <
typename Data>
96 Data&
Set(AnyStorageDataTag<StorageTag, Data> tag, Data data);
100 template <
typename Data,
typename... Args>
101 Data&
Emplace(
const AnyStorageDataTag<StorageTag, Data>& tag, Args&&... args);
105 template <
typename Data>
106 Data*
GetOptional(
const AnyStorageDataTag<StorageTag, Data>& tag)
noexcept;
110 template <
typename Data>
111 const Data*
GetOptional(
const AnyStorageDataTag<StorageTag, Data>& tag)
const noexcept;
114 template <
typename Data>
115 void Erase(
const AnyStorageDataTag<StorageTag, Data>& tag);
119 void (*deleter)(std::byte*)
noexcept;
123 AllocRecord* GetRecords()
noexcept;
125 static any_storage::impl::Offset CalcOffset()
noexcept;
127 std::unique_ptr<std::byte[]> raw_data_;
130template <
typename StorageTag>
131any_storage::impl::Offset AnyStorage<StorageTag>::CalcOffset()
noexcept {
132 const auto offset = any_storage::impl::data_offset<StorageTag>;
133 return ((offset +
alignof(AllocRecord) - 1) /
alignof(AllocRecord)) *
alignof(AllocRecord);
136template <
typename StorageTag>
137AnyStorage<StorageTag>::AnyStorage()
138 : raw_data_(
new std::byte[CalcOffset() +
sizeof(AllocRecord) * any_storage::impl::count<StorageTag>]) {
139 static_assert(std::is_trivial_v<AllocRecord>);
141 auto records = GetRecords();
142 for (std::size_t i = 0; i < any_storage::impl::count<StorageTag>; i++) {
143 auto& record = records[i];
144 record.deleter =
nullptr;
148template <
typename StorageTag>
149AnyStorage<StorageTag>::~AnyStorage() {
150 auto records = GetRecords();
151 for (std::size_t i = 0; i < any_storage::impl::count<StorageTag>; i++) {
152 auto& record = records[i];
153 if (record.deleter) record.deleter(&raw_data_[record.offset]);
158template <
typename Data>
159Data& AnyStorage<StorageTag>::Set(
const AnyStorageDataTag<StorageTag, Data> tag, Data data) {
160 auto number = tag.number_;
161 if (!GetRecords()[number].deleter)
return Emplace(tag, std::move(data));
163 auto offset = tag.offset_;
164 return *
reinterpret_cast<Data*>(&raw_data_[offset]) = std::move(data);
168template <
typename Data,
typename... Args>
169Data& AnyStorage<StorageTag>::
Emplace(
const AnyStorageDataTag<StorageTag, Data>& tag, Args&&... args) {
170 auto number = tag.number_;
171 auto& record = GetRecords()[number];
172 if (record.deleter) record.deleter(&raw_data_[tag.offset_]);
174 auto offset = tag.offset_;
175 auto ptr =
new (&raw_data_[offset]) Data(std::forward<Args>(args)...);
176 record = {&any_storage::impl::Delete<Data>, offset};
181template <
typename Data>
182Data& AnyStorage<StorageTag>::
Get(
const AnyStorageDataTag<StorageTag, Data>& tag) {
183 auto ptr = GetOptional(tag);
184 if (ptr)
return *ptr;
185 throw std::runtime_error(
"No data");
189template <
typename Data>
190const Data& AnyStorage<StorageTag>::
Get(
const AnyStorageDataTag<StorageTag, Data>& tag)
const {
192 return const_cast<AnyStorage<StorageTag>*>(
this)->Get<Data>(tag);
196template <
typename Data>
197Data* AnyStorage<StorageTag>::
GetOptional(
const AnyStorageDataTag<StorageTag, Data>& tag)
noexcept {
198 auto number = tag.number_;
199 auto offset = tag.offset_;
200 if (!GetRecords()[number].deleter)
return nullptr;
201 return reinterpret_cast<Data*>(&raw_data_[offset]);
205template <
typename Data>
206const Data* AnyStorage<StorageTag>::
GetOptional(
const AnyStorageDataTag<StorageTag, Data>& tag)
const noexcept {
208 return const_cast<AnyStorage*>(
this)->GetOptional<Data>(tag);
211template <
typename StorageTag>
212typename AnyStorage<StorageTag>::AllocRecord* AnyStorage<StorageTag>::GetRecords()
noexcept {
213 return reinterpret_cast<AllocRecord*>(&raw_data_[CalcOffset()]);