From daf7ce59c7b62601a233c33c1d36811d785ef41e Mon Sep 17 00:00:00 2001
From: Timm Fitschen <t.fitschen@indiscale.com>
Date: Wed, 22 Jun 2022 23:32:19 +0200
Subject: [PATCH] Implement move for JsonValue

---
 include/caosdb/utility.h |  4 ++--
 src/caosdb/utility.cpp   | 24 +++++++++++++++++++++---
 2 files changed, 23 insertions(+), 5 deletions(-)

diff --git a/include/caosdb/utility.h b/include/caosdb/utility.h
index 43586da..d7497e4 100644
--- a/include/caosdb/utility.h
+++ b/include/caosdb/utility.h
@@ -137,14 +137,14 @@ public:
    *
    * Also moves the `wrapped` object.
    */
-  JsonValue(JsonValue &&other) = default;
+  JsonValue(JsonValue &&other);
 
   /**
    * Move Assigment.
    *
    * Also moves the `wrapped` object.
    */
-  auto operator=(JsonValue &&other) -> JsonValue & = default;
+  auto operator=(JsonValue &&other) -> JsonValue &;
 
   /**
    * Return true if the `wrapped` object is the nullptr.
diff --git a/src/caosdb/utility.cpp b/src/caosdb/utility.cpp
index 7aed716..58d7c97 100644
--- a/src/caosdb/utility.cpp
+++ b/src/caosdb/utility.cpp
@@ -147,10 +147,11 @@ auto load_json_file(const path &json_file) -> JsonValue {
 JsonValue::~JsonValue() { this->Reset(); }
 
 auto JsonValue::Reset() -> void {
-  if (!IsNull()) {
-    delete static_cast<value *>(this->wrapped);
-  }
+  void * tmp = std::move(this->wrapped);
   this->wrapped = nullptr;
+  if (tmp != nullptr) {
+    delete static_cast<value *>(tmp);
+  }
 }
 
 JsonValue::JsonValue(const JsonValue &other) : wrapped(nullptr) {
@@ -169,4 +170,21 @@ auto JsonValue::operator=(const JsonValue &other) -> JsonValue & {
   return *this;
 }
 
+JsonValue::JsonValue(JsonValue &&other) : wrapped(nullptr) {
+  std::cout << "MoveConstructor" << std::endl;
+  if (!other.IsNull()) {
+    this->wrapped = other.wrapped;
+    other.wrapped = nullptr;
+  }
+}
+
+auto JsonValue::operator=(JsonValue &&other) -> JsonValue & {
+  std::cout << "MoveAssignment" << std::endl;
+  if (this != &other) {
+    this->wrapped = other.wrapped;
+    other.wrapped = nullptr;
+  }
+  return *this;
+}
+
 } // namespace caosdb::utility
-- 
GitLab