54 char* demangled = abi::__cxa_demangle(
typeid(T).name(),
nullptr, &length, &status);
55 std::string demangled_string(demangled);
57 return demangled_string;
73 virtual void set(
void** storage,
const void* v)
const = 0;
78 virtual void clear(
void** storage)
const = 0;
83 virtual std::string
type()
const = 0;
89 virtual bool matches(
const std::type_info& ti)
const = 0;
96 virtual bool equals(
void** storage,
void** other_storage)
const = 0;
102 template <
typename T>
110 virtual void set(
void** storage,
const void* v)
const
112 *(storage) =
new T(*reinterpret_cast<T const*>(v));
118 virtual void clear(
void** storage)
const
120 delete reinterpret_cast<T*
>(*storage);
126 virtual std::string
type()
const
128 return demangledType<T>();
135 virtual bool matches(
const std::type_info& ti)
const
137 return typeid(T) == ti;
145 bool equals(
void** storage,
void** other_storage)
const
147 T typed_storage = *(
reinterpret_cast<T*
>(*storage));
148 T typed_other_storage = *(
reinterpret_cast<T*
>(*other_storage));
149 return typed_storage == typed_other_storage;
166 Any() : policy(select_policy<
Empty>()), storage(nullptr)
171 template <
typename T>
172 explicit Any(
const T& v) : policy(select_policy<T>()), storage(nullptr)
174 policy->
set(&storage, &v);
178 Any(
const Any& other) : policy(other.policy), storage(nullptr)
180 policy->
set(&storage, other.storage);
189 policy->
clear(&storage);
190 policy = other.policy;
191 policy->
set(&storage, other.storage);
212 policy->
clear(&storage);
218 template <
typename T>
223 return *(
reinterpret_cast<T*
>(storage));
227 throw std::logic_error(
"Bad cast to " + demangledType<T>() +
228 " but the type is " + policy->
type());
233 template <
typename T>
236 return (policy == select_policy<T>()) || same_type_fallback<T>();
240 template <
typename T>
243 return policy->
matches(
typeid(T));
249 return same_type<Empty>();
252 template <
typename T>
256 static Policy policy;
260 BaseAnyPolicy* policy;
266 void* lhs_storage = lhs.storage;
267 void* rhs_storage = rhs.storage;
268 return lhs.policy == rhs.policy and
269 lhs.policy->
equals(&lhs_storage, &rhs_storage);
274 return !(lhs == rhs);
295 template <
typename T>
309 template <
typename T>
virtual void clear(void **storage) const
friend bool operator!=(const Any &lhs, const Any &rhs)
bool operator==(const Any &lhs, const Any &rhs)
virtual bool equals(void **storage, void **other_storage) const =0
virtual bool matches(const std::type_info &ti) const
Allows to store objects of arbitrary types by using a BaseAnyPolicy and provides a type agnostic API...
T recall_type(const Any &any)
bool same_type_fallback() const
virtual bool matches(const std::type_info &ti) const =0
bool operator!=(const Any &lhs, const Any &rhs)
virtual std::string type() const
bool operator==(const Empty &other) const
virtual void set(void **storage, const void *v) const
Any & operator=(const Any &other)
An interface for a policy to store a value. Value can be any data like primitive data-types, shogun objects, etc. Policy defines how to handle this data. It works with a provided memory region and is able to set value, clear it and return the type-name as string.
virtual void clear(void **storage) const =0
all of classes and functions are contained in the shogun namespace
virtual std::string type() const =0
std::string demangledType()
bool equals(void **storage, void **other_storage) const
friend bool operator==(const Any &lhs, const Any &rhs)
virtual void set(void **storage, const void *v) const =0
Any erase_type(const T &v)
This is one concrete implementation of policy that uses void pointers to store values.