]> git.tdb.fi Git - libs/core.git/commitdiff
Add a helper function for clearing a Variant
authorMikko Rasa <tdb@tdb.fi>
Thu, 1 Jun 2023 06:26:48 +0000 (09:26 +0300)
committerMikko Rasa <tdb@tdb.fi>
Thu, 1 Jun 2023 06:26:48 +0000 (09:26 +0300)
source/core/variant.h

index c0b0aebc3e901171fd9b60af95c8fe5826fd33d3..fccf891850f8045a528ad723b7fd781c7df72c09 100644 (file)
@@ -38,13 +38,15 @@ public:
        template<typename T>
        Variant(const T &v) { assign(v); }
        Variant(const Variant &v) { copy_from(v); }
-       ~Variant() { if(funcs) funcs->destroy(storage); }
+       ~Variant() { clear(); }
 
        template<typename T>
        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<typename T>
        void assign(const T &);
@@ -133,23 +135,25 @@ private:
 };
 
 
-template<typename T>
-inline void Variant::assign(const T &v)
+inline void Variant::clear()
 {
        if(funcs)
                funcs->destroy(storage);
+       funcs = nullptr;
+}
 
+template<typename T>
+inline void Variant::assign(const T &v)
+{
+       clear();
        funcs = get_functions<typename std::remove_cv<T>::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);
 }