From 83fcabb1fc1acff5d115f07253f801fc3f7cca9a Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Thu, 1 Jun 2023 09:26:48 +0300 Subject: [PATCH] Add a helper function for clearing a Variant --- source/core/variant.h | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) 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); } -- 2.43.0