From: Mikko Rasa Date: Thu, 1 Jun 2023 06:26:48 +0000 (+0300) Subject: Add a helper function for clearing a Variant X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=83fcabb1fc1acff5d115f07253f801fc3f7cca9a;p=libs%2Fcore.git Add a helper function for clearing a Variant --- diff --git a/source/core/variant.h b/source/core/variant.h index c0b0aeb..fccf891 100644 --- a/source/core/variant.h +++ b/source/core/variant.h @@ -38,13 +38,15 @@ public: template Variant(const T &v) { assign(v); } Variant(const Variant &v) { copy_from(v); } - ~Variant() { if(funcs) funcs->destroy(storage); } + ~Variant() { clear(); } template Variant &operator=(const T &v) { assign(v); return *this; } Variant &operator=(const Variant &v) { if(&v!=this) copy_from(v); return *this; } + void clear(); + private: template void assign(const T &); @@ -133,23 +135,25 @@ private: }; -template -inline void Variant::assign(const T &v) +inline void Variant::clear() { if(funcs) funcs->destroy(storage); + funcs = nullptr; +} +template +inline void Variant::assign(const T &v) +{ + clear(); funcs = get_functions::type>(); create(storage, v); } inline void Variant::copy_from(const Variant &v) { - if(funcs) - funcs->destroy(storage); - - funcs = v.funcs; - if(funcs) + clear(); + if((funcs = v.funcs)) funcs->clone(storage, v.storage); }