29 enum class OpForConsteval {
35 template<
typename SharedStorage, concepts::Allocator Alloc>
36 struct SharedStorageManage {
37 using Type = Method<SharedStorageManage, void(This&, SharedStorage*, Alloc&, OpForConsteval)>;
40 constexpr void operator()(T&, SharedStorage*, Alloc&, OpForConsteval)
const;
43 template<
typename SharedStorage, concepts::Allocator Alloc>
44 constexpr inline auto shared_storage_manage = SharedStorageManage<SharedStorage, Alloc> {};
47 sync::Atomic<usize> ref_count { 1 };
51 struct ObjectWithRefCount : RefCount {
52 ObjectWithRefCount(ObjectWithRefCount
const&) =
delete;
53 ObjectWithRefCount(ObjectWithRefCount&&) =
delete;
55 template<
typename... Args>
56 constexpr ObjectWithRefCount(Args&&... args) : object(util::forward<Args>(args)...) {}
58 constexpr auto to_object_pointer() -> T* {
return util::addressof(
object); }
64template<concepts::Allocator Alloc = platform::DefaultAllocator>
67 template<
typename, concepts::Allocator>
68 friend struct detail::SharedStorageManage;
84 template<
typename Any,
typename T,
typename... Args>
88 using Store = detail::ObjectWithRefCount<T>;
95 auto* pointer = *result;
98 self->m_pointer = pointer;
101 template<
typename T,
typename... Args>
104 using Store = detail::ObjectWithRefCount<T>;
107 self->m_pointer = pointer;
120 dest->m_pointer = source->m_pointer;
121 if (dest->m_pointer) {
123 auto const fp = vtable[
Manage {}];
124 fp(dest, dest, dest->m_allocator, detail::OpForConsteval::RefInc);
126 dest->fetch_add_ref_count();
137 template<concepts::VTableFor<Interface> VTable>
141 dest_vtable = source_vtable;
145 template<concepts::VTableFor<Interface> VTable>
149 dest_vtable = source_vtable;
154 if (self->m_pointer) {
155 auto const fp = vtable[
Manage {}];
157 fp(self, self, self->m_allocator, detail::OpForConsteval::RefDec);
159 if (self->fetch_sub_ref_count() == 1) {
160 fp(self, self, self->m_allocator, detail::OpForConsteval::Destroy);
163 self->m_pointer =
nullptr;
169 return static_cast<detail::ObjectWithRefCount<T>*
>(m_pointer)->to_object_pointer();
174 return static_cast<detail::ObjectWithRefCount<T> const*
>(m_pointer)->to_object_pointer();
178 constexpr explicit SharedStorage(
void* pointer) : m_pointer(pointer) {}
180 constexpr auto fetch_sub_ref_count() ->
usize {
181 auto& ref_count =
static_cast<detail::RefCount*
>(m_pointer)->ref_count;
185 constexpr auto fetch_add_ref_count() ->
usize {
186 auto& ref_count =
static_cast<detail::RefCount*
>(m_pointer)->ref_count;
190 void* m_pointer {
nullptr };
191 [[no_unique_address]] Alloc m_allocator {};
195 template<
typename SharedStorage, concepts::Allocator Alloc>
197 constexpr void SharedStorageManage<SharedStorage, Alloc>::operator()(T&, SharedStorage* storage, Alloc& allocator,
198 OpForConsteval op)
const {
199 auto* pointer =
static_cast<ObjectWithRefCount<T>*
>(storage->m_pointer);
202 case OpForConsteval::RefInc:
203 pointer->ref_count.fetch_add(1);
205 case OpForConsteval::RefDec:
206 if (pointer->ref_count.fetch_sub(1) == 1) {
Definition operations.h:11
Definition allocator.h:20
Definition vtable_for.h:17
StorageCategory
Definition storage_category.h:4
@ Copyable
Definition storage_category.h:10
meta::Type< AnyT< UserInterface, Storage, VTablePolicy > > Any
Definition any.h:302
@ Relaxed
Definition memory_order.h:7
@ AcquireRelease
Definition memory_order.h:11
size_t usize
Definition integers.h:33
constexpr auto exchange(T &object, U &&new_value) -> T
Definition exchange.h:8
constexpr auto destroy_at
Definition destroy_at.h:24
constexpr auto construct_at
Definition construct_at.h:27
constexpr auto as_fallible
Definition as_fallible.h:26
constexpr auto try_infallible
Definition try_infallible.h:31
Unexpected(E &&) -> Unexpected< meta::UnwrapRefDecay< E > >
constexpr auto allocate_one
Definition allocate_one.h:29
constexpr auto destroy
Definition destroy.h:35
constexpr auto in_place_type
Definition in_place_type.h:12
constexpr auto deallocate_one
Definition deallocate_one.h:27
meta::List< Manage > Interface
Definition shared_storage.h:72
static constexpr void copy_construct(concepts::VTableFor< Interface > auto const &vtable, SharedStorage *dest, SharedStorage const *source)
Definition shared_storage.h:118
constexpr auto down_cast() -> T *
Definition shared_storage.h:168
static constexpr void destroy(concepts::VTableFor< Interface > auto &vtable, SharedStorage *self)
Definition shared_storage.h:153
static constexpr auto storage_category() -> StorageCategory
Definition shared_storage.h:74
static constexpr void move_assign(VTable &dest_vtable, SharedStorage *dest, VTable &source_vtable, SharedStorage *source)
Definition shared_storage.h:146
constexpr auto down_cast() const -> T const *
Definition shared_storage.h:173
static constexpr void create(InPlaceType< Any >, meta::LikeExpected< CreationResult< T >, Any > &self, InPlaceType< T >, Args &&... args)
Definition shared_storage.h:86
static constexpr auto init(SharedStorage *self, InPlaceType< T >, Args &&... args)
Definition shared_storage.h:103
static constexpr auto creation_is_fallible(InPlaceType< T >) -> bool
Definition shared_storage.h:77
meta::Type< detail::SharedStorageManage< SharedStorage, Alloc > > Manage
Definition shared_storage.h:71
SharedStorage(SharedStorage const &)=default
auto operator=(SharedStorage const &) -> SharedStorage &=default
meta::AllocatorResult< Alloc > CreationResult
Definition shared_storage.h:82
static constexpr void move_construct(concepts::VTableFor< Interface > auto &vtable, SharedStorage *dest, SharedStorage *source)
Definition shared_storage.h:131
static constexpr void copy_assign(VTable &dest_vtable, SharedStorage *dest, VTable const &source_vtable, SharedStorage const *source)
Definition shared_storage.h:138
Definition in_place_type.h:5