Original author: Alex Wilson.
This commit is contained in:
Roman Donchenko 2013-10-02 16:06:56 +04:00
parent 78c4ffec99
commit 4c19e0273a

View File

@ -44,6 +44,7 @@ struct base_any_policy
virtual void clone(void* const* src, void** dest) = 0;
virtual void move(void* const* src, void** dest) = 0;
virtual void* get_value(void** src) = 0;
virtual void* get_value(void* const * src) = 0;
virtual ::size_t get_size() = 0;
virtual const std::type_info& type() = 0;
virtual void print(std::ostream& out, void* const* src) = 0;
@ -69,6 +70,7 @@ struct small_any_policy : typed_base_any_policy<T>
virtual void clone(void* const* src, void** dest) { *dest = *src; }
virtual void move(void* const* src, void** dest) { *dest = *src; }
virtual void* get_value(void** src) { return reinterpret_cast<void*>(src); }
virtual void* get_value(void* const * src) { return const_cast<void *>( reinterpret_cast<const void*>(src) ); }
virtual void print(std::ostream& out, void* const* src) { out << *reinterpret_cast<T const*>(src); }
};
@ -93,6 +95,7 @@ struct big_any_policy : typed_base_any_policy<T>
**reinterpret_cast<T**>(dest) = **reinterpret_cast<T* const*>(src);
}
virtual void* get_value(void** src) { return *src; }
virtual void* get_value(void* const * src) { return const_cast<void*>(*src); }
virtual void print(std::ostream& out, void* const* src) { out << *reinterpret_cast<T const*>(*src); }
};
@ -257,8 +260,7 @@ public:
const T& cast() const
{
if (policy->type() != typeid(T)) throw anyimpl::bad_any_cast();
void* obj = const_cast<void*>(object);
T* r = reinterpret_cast<T*>(policy->get_value(&obj));
T* r = reinterpret_cast<T*>(policy->get_value(&object));
return *r;
}