10export import :type_info;
16 requires (not std::same_as<std::remove_reference_t<T>, any>)
18 using type = std::remove_const_t<std::remove_reference_t<T>>;
20 destructor_ = [](
void* ptr) {
delete static_cast<type*
>(ptr); };
27 destructor_ = [](
void*) {
33 requires (not std::same_as<std::remove_reference_t<T>,
any>)
35 using type = std::remove_const_t<std::remove_reference_t<T>>;
37 destructor_ = [](
void* ptr) {
delete static_cast<T*
>(ptr); };
42 requires (not std::same_as<std::remove_reference_t<T>,
any>)
44 using type = std::remove_const_t<std::remove_reference_t<T>>;
45 data_ =
new type(std::forward<T>(t));
46 destructor_ = [](
void* ptr) {
delete static_cast<T*
>(ptr); };
50 template<
typename T,
typename... Args>
51 requires (not std::is_reference_v<T>)
52 static any
make(Args &&... args) {
53 using type = std::remove_const_t<std::remove_reference_t<T>>;
54 auto* ptr =
new type(std::forward<Args>(args)...);
59 requires (not std::same_as<std::remove_reference_t<T>,
any>)
60 static any
make(
const T &value) {
61 using type = std::remove_const_t<std::remove_reference_t<T>>;
62 auto* ptr =
new type(value);
69 a.type_info_ = &t_info;
78 any(
const any &other) {
79 type_info_ = other.type_info_;
80 if (type_info_ !=
nullptr) {
81 data_ = type_info_->make_copy_of(other.data_);
83 destructor_ = other.destructor_;
87 if (data_ !=
nullptr) {
91 type_info_ = other.type_info_;
92 if (type_info_ !=
nullptr) {
93 type_info_->assign_copy_of(other.data_, data_);
95 destructor_ = other.destructor_;
101 if (type_info_ ==
nullptr)
return false;
102 return type_info_->is_type<T>();
106 if (type_info_ ==
nullptr)
return false;
107 return type_info_->id() == tinfo.
id();
112 if (not
is<T>()) {
throw std::bad_cast(); }
113 return *
static_cast<T*
>(data_);
117 const T &
as()
const {
118 if (not
is<T>()) {
throw std::bad_cast(); }
119 return *
static_cast<T*
>(data_);
123 if (type_info_ ==
nullptr) {
throw std::bad_typeid(); }
128 return data_ ==
nullptr or type_info_ ==
nullptr;
140 if (data_ ==
nullptr) {
141 return type_info_ == other.type_info_;
143 return type_info_ == other.type_info_ and type_info_->
equality(data_, other.data_);
149 std::function<void(
void*)> destructor_;
158 type_info_ =
nullptr;
162 requires (not std::same_as<std::remove_reference_t<T>,
any_ref> and not std::same_as<
163 std::remove_reference_t<T>,
any>)
165 using type = std::remove_const_t<std::remove_reference_t<T>>;
171 requires (not std::same_as<std::remove_reference_t<T>,
any_ref> and not std::same_as<
172 std::remove_reference_t<T>,
any>)
174 using type = std::remove_const_t<std::remove_reference_t<T>>;
182 a.type_info_ = &t_info;
187 a.data_ = owner_any.
data();
188 a.type_info_ = &owner_any.
type();
197 type_info_ = other.type_info_;
198 if (type_info_ !=
nullptr) {
204 if (data_ !=
nullptr) {
207 type_info_ = other.type_info_;
208 if (type_info_ !=
nullptr) {
216 if (type_info_ ==
nullptr)
return false;
217 return type_info_->is_type<T>();
221 if (type_info_ ==
nullptr)
return false;
222 return type_info_->id() == tinfo.
id();
227 if (not
is<T>()) {
throw std::bad_cast(); }
228 return *
static_cast<T*
>(data_);
232 const T &
as()
const {
233 if (not
is<T>()) {
throw std::bad_cast(); }
234 return *
static_cast<T*
>(data_);
238 if (type_info_ ==
nullptr) {
throw std::bad_typeid(); }
243 return data_ ==
nullptr or type_info_ ==
nullptr;
bool is(const refl::type_info &tinfo) const
any_ref & operator=(const any_ref &other)
const void * data() const
any_ref(const any_ref &other)
const type_info & type() const
any_ref(refl::any &owner_any)
any_ref(const refl::type_info &t_info, void *ptr)
static any make(Args &&... args)
const void * data() const
static any make(const T &value)
any & operator=(const any &other)
static any make(const refl::type_info &t_info, void *ptr)
bool is(const refl::type_info &tinfo) const
bool operator==(const any &other) const
const type_info & type() const
bool equality(const void *lhs, const void *rhs) const
void * make_copy_of(const void *ptr) const
static const type_info & from()