X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fcore%2Fvariant.h;h=c0b0aebc3e901171fd9b60af95c8fe5826fd33d3;hb=6af098355f2b037a6f8084f9f6c741e462ffa614;hp=934a9e539276e3ad406a6b05b00e01cd3d964ca0;hpb=db3397e3b7b9839714ce28d9df2a8f226f2e58b2;p=libs%2Fcore.git diff --git a/source/core/variant.h b/source/core/variant.h index 934a9e5..c0b0aeb 100644 --- a/source/core/variant.h +++ b/source/core/variant.h @@ -5,17 +5,18 @@ #include #include #include "meta.h" +#include "mspcore_api.h" namespace Msp { -class type_mismatch: public std::runtime_error +class MSPCORE_API type_mismatch: public std::runtime_error { public: type_mismatch(const std::type_info &, const std::type_info &); }; -class Variant +class MSPCORE_API Variant { public: static constexpr unsigned INTERNAL_SIZE = 2*sizeof(void *); @@ -61,9 +62,9 @@ public: const T &value() const { return const_cast(this)->get(); } template - bool has_type() const { return funcs==get_functions::type>(); } + bool has_type() const { return type_equals(funcs, get_functions::type>()); } - bool has_same_type(const Variant &v) const { return (funcs && funcs==v.funcs); } + bool has_same_type(const Variant &v) const { return type_equals(funcs, v.funcs); } template DEPRECATED bool check_type() const { return has_type(); } @@ -77,6 +78,8 @@ public: operator T() const { return value(); } private: + static bool type_equals(const Functions *, const Functions *); + template static constexpr bool is_small() { return (sizeof(T)<=INTERNAL_SIZE && alignof(T)<=alignof(void *)); } @@ -131,13 +134,13 @@ private: template -inline void Variant::assign(const T &value) +inline void Variant::assign(const T &v) { if(funcs) funcs->destroy(storage); funcs = get_functions::type>(); - create(storage, value); + create(storage, v); } inline void Variant::copy_from(const Variant &v) @@ -162,6 +165,16 @@ inline T &Variant::get() return **reinterpret_cast(storage); } +inline bool Variant::type_equals(const Functions *funcs1, const Functions *funcs2) +{ + if(!funcs1 || !funcs2) + return false; + else if(funcs1==funcs2) + return true; + else + return funcs1->get_type()==funcs2->get_type(); +} + template inline const Variant::Functions *Variant::get_functions() {