Iros
 
Loading...
Searching...
No Matches
ref_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"
10
11namespace di::any {
13public:
15
16 constexpr static auto storage_category() -> StorageCategory { return StorageCategory::Reference; }
17
18 template<typename T>
19 constexpr static auto creation_is_fallible(InPlaceType<T>) -> bool {
20 return false;
21 }
22
23 template<typename>
24 using CreationResult = void;
25
26 constexpr RefStorage() : m_pointer(nullptr) {}
27
28 RefStorage(RefStorage const&) = default;
29 auto operator=(RefStorage const&) -> RefStorage& = default;
30
31 template<concepts::Object T, concepts::ConvertibleTo<T&> U>
32 requires(!concepts::Const<T>)
33 constexpr static void init(RefStorage* self, InPlaceType<T&>, U&& u) {
34 self->m_pointer = util::addressof(static_cast<T&>(util::forward<U>(u)));
35 }
36
37 template<concepts::Object T, concepts::ConvertibleTo<T const&> U>
38 constexpr static void init(RefStorage* self, InPlaceType<T const&>, T const& u) {
39 self->m_const_pointer = util::addressof(static_cast<T const&>(util::forward<U>(u)));
40 }
41
42 template<concepts::LanguageFunction T, concepts::ConvertibleTo<T*> U>
43 constexpr static void init(RefStorage* self, InPlaceType<T*>, T* u) {
44 self->m_function_pointer = reinterpret_cast<void (*)()>(static_cast<T*>(util::forward<U>(u)));
45 }
46
47 ~RefStorage() = default;
48
49 template<typename T>
50 constexpr auto down_cast() const -> T* {
51 if constexpr (concepts::Const<T>) {
52 return static_cast<T*>(m_const_pointer);
53 } else if constexpr (concepts::Object<T>) {
54 return static_cast<T*>(m_pointer);
55 } else {
56 return reinterpret_cast<T*>(m_function_pointer);
57 }
58 }
59
60private:
61 union {
62 void* m_pointer;
63 void const* m_const_pointer;
65 };
66};
67}
RefStorage(RefStorage const &)=default
constexpr RefStorage()
Definition ref_storage.h:26
void(* m_function_pointer)()
Definition ref_storage.h:64
static constexpr void init(RefStorage *self, InPlaceType< T * >, T *u)
Definition ref_storage.h:43
void CreationResult
Definition ref_storage.h:24
auto operator=(RefStorage const &) -> RefStorage &=default
static constexpr auto creation_is_fallible(InPlaceType< T >) -> bool
Definition ref_storage.h:19
static constexpr auto storage_category() -> StorageCategory
Definition ref_storage.h:16
static constexpr void init(RefStorage *self, InPlaceType< T & >, U &&u)
Definition ref_storage.h:33
static constexpr void init(RefStorage *self, InPlaceType< T const & >, T const &u)
Definition ref_storage.h:38
void * m_pointer
Definition ref_storage.h:62
void const * m_const_pointer
Definition ref_storage.h:63
meta::List<> Interface
Definition ref_storage.h:14
constexpr auto down_cast() const -> T *
Definition ref_storage.h:50
Definition language.h:18
Definition language.h:370
Definition any.h:18
StorageCategory
Definition storage_category.h:4
@ Reference
Definition storage_category.h:5
Definition list.h:77
Definition in_place_type.h:5