41 #ifdef HAVE_CXA_DEMANGLE 54 #ifdef HAVE_CXA_DEMANGLE 57 char* demangled = abi::__cxa_demangle(
typeid(T).name(),
nullptr, &length, &status);
58 std::string demangled_string(demangled);
61 std::string demangled_string(
typeid(T).name());
63 return demangled_string;
84 virtual void set(
void** storage,
const void* v)
const = 0;
89 virtual void clear(
void** storage)
const = 0;
94 virtual std::string type()
const = 0;
100 virtual bool matches(
const std::type_info& ti)
const = 0;
107 virtual bool equals(
void** storage,
void** other_storage)
const = 0;
112 virtual std::string policy_name()
const = 0;
123 template <
typename T>
131 virtual void set(
void** storage,
const void* v)
const 133 *(storage) =
new T(*reinterpret_cast<T const*>(v));
139 virtual void clear(
void** storage)
const 141 delete reinterpret_cast<T*
>(*storage);
147 virtual std::string
type()
const 149 return demangledType<T>();
156 virtual bool matches(
const std::type_info& ti)
const 158 return typeid(T) == ti;
166 bool equals(
void** storage,
void** other_storage)
const 168 T typed_storage = *(
reinterpret_cast<T*
>(*storage));
169 T typed_other_storage = *(
reinterpret_cast<T*
>(*other_storage));
170 return typed_storage == typed_other_storage;
182 template <
typename T>
190 virtual void set(
void** storage,
const void* v)
const 192 *(storage) = const_cast<void*>(v);
198 virtual void clear(
void** storage)
const 205 virtual std::string
type()
const 207 return demangledType<T>();
214 virtual bool matches(
const std::type_info& ti)
const 216 return typeid(T) == ti;
224 bool equals(
void** storage,
void** other_storage)
const 226 T typed_storage = *(
reinterpret_cast<T*
>(*storage));
227 T typed_other_storage = *(
reinterpret_cast<T*
>(*other_storage));
228 return typed_storage == typed_other_storage;
240 template <
typename T>
244 static Policy policy;
248 template <
typename T>
252 static Policy policy;
274 template <
typename T>
277 policy->set(&storage, &v);
281 Any(
BaseAnyPolicy* the_policy,
void* the_storage) : policy(the_policy), storage(the_storage)
288 assert_same_policy_type(other.policy);
289 policy->set(&storage, other.storage);
298 assert_same_policy_type(other.policy);
299 policy->clear(&storage);
300 policy = other.policy;
301 policy->
set(&storage, other.storage);
322 policy->clear(&storage);
328 template <
typename T>
333 return *(
reinterpret_cast<T*
>(storage));
337 throw std::logic_error(
"Bad cast to " + demangledType<T>() +
338 " but the type is " + policy->type());
343 template <
typename T>
346 return (policy == owning_policy<T>()) || (policy == non_owning_policy<T>()) || same_type_fallback<T>();
350 template <
typename T>
353 return policy->matches(
typeid(T));
359 return same_type<Empty>();
365 if (policy->policy_type() != other_policy->
policy_type()) {
366 throw std::logic_error(
"The policies are different: " +
367 policy->policy_name() +
" and " +
379 void* lhs_storage = lhs.storage;
380 void* rhs_storage = rhs.storage;
381 return lhs.policy == rhs.policy &&
382 lhs.policy->
equals(&lhs_storage, &rhs_storage);
387 return !(lhs == rhs);
408 template <
typename T>
414 template <
typename T>
417 return Any(non_owning_policy<T>(), v);
428 template <
typename T>
virtual PolicyType policy_type() const
bool same_type_fallback() const
bool equals(void **storage, void **other_storage) const
bool operator==(const Any &lhs, const Any &rhs)
static BaseAnyPolicy * owning_policy()
virtual bool equals(void **storage, void **other_storage) const =0
virtual bool matches(const std::type_info &ti) const
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...
virtual void clear(void **storage) const
virtual std::string policy_name() const
Any erase_type_non_owning(T *v)
bool operator==(const Empty &other) const
virtual std::string type() const
T recall_type(const Any &any)
bool equals(void **storage, void **other_storage) const
static BaseAnyPolicy * non_owning_policy()
virtual std::string policy_name() const =0
bool operator!=(const Any &lhs, const Any &rhs)
virtual void clear(void **storage) const
virtual PolicyType policy_type() const =0
virtual PolicyType policy_type() const
virtual std::string policy_name() 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.
all of classes and functions are contained in the shogun namespace
std::string demangledType()
virtual std::string type() const
Any(BaseAnyPolicy *the_policy, void *the_storage)
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.