From ec5732e265dd7dcaf00f92235706f4af689dabe2 Mon Sep 17 00:00:00 2001
From: Timm Fitschen <t.fitschen@indiscale.com>
Date: Thu, 23 Jun 2022 16:24:07 +0200
Subject: [PATCH] MAINT: use smart pointer instead of raw

---
 CMakeLists.txt               |  2 +-
 include/caosdb/utility.h     |  5 +++--
 src/caosdb/configuration.cpp |  2 +-
 src/caosdb/utility.cpp       | 24 ++++++++++--------------
 4 files changed, 15 insertions(+), 18 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 84738ea..09d03a7 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -323,7 +323,7 @@ if(_LINTING)
     else()
         message(STATUS "clang-tidy: ${clang_tidy}")
         set(_CMAKE_CXX_CLANG_TIDY_CHECKS
-            "--checks=*,-fuchsia-*,-llvmlibc-*,-readability-convert-member-functions-to-static,-cppcoreguidelines-pro-bounds-array-to-pointer-decay,-hicpp-no-array-decay,-llvm-else-after-return,-readability-else-after-return,-modernize-use-trailing-return-type,-bugprone-branch-clone,-altera-*,-cppcoreguidelines-macro-usage,-*-avoid-c-arrays,-cppcoreguidelines-owning-memory")
+            "--checks=*,-fuchsia-*,-llvmlibc-*,-readability-convert-member-functions-to-static,-cppcoreguidelines-pro-bounds-array-to-pointer-decay,-hicpp-no-array-decay,-llvm-else-after-return,-readability-else-after-return,-modernize-use-trailing-return-type,-bugprone-branch-clone,-altera-*,-cppcoreguidelines-macro-usage,-*-avoid-c-arrays")
         set(_CMAKE_C_CLANG_TIDY_CHECKS "${_CMAKE_CXX_CLANG_TIDY_CHECKS}")
         set(_CMAKE_CXX_CLANG_TIDY "${clang_tidy}"
             "--header-filter=caosdb/.*[^\(\.pb\.h\)]$"
diff --git a/include/caosdb/utility.h b/include/caosdb/utility.h
index 0fab625..1a6bcf2 100644
--- a/include/caosdb/utility.h
+++ b/include/caosdb/utility.h
@@ -26,6 +26,7 @@
 #include <cstdlib>            // for getenv
 #include <filesystem>         // for path
 #include <fstream>            // for basic_istream<>::__ist...
+#include <memory>             // for shared_ptr
 #include <string>             // for string, operator+, cha...
 
 namespace caosdb::utility {
@@ -110,7 +111,7 @@ public:
    * By calling this constructor the ownership of the `wrapped` parameter is
    * transferred to this object.
    */
-  JsonValue(void *wrapped) : wrapped(wrapped) {}
+  JsonValue(void *wrapped);
   /**
    * Destructor.
    *
@@ -162,7 +163,7 @@ public:
    * An object which represents a JSON value. The object's class is an
    * implementation detail.
    */
-  void *wrapped;
+  std::shared_ptr<void> wrapped;
 };
 
 /**
diff --git a/src/caosdb/configuration.cpp b/src/caosdb/configuration.cpp
index 5d1874a..410351f 100644
--- a/src/caosdb/configuration.cpp
+++ b/src/caosdb/configuration.cpp
@@ -45,7 +45,7 @@
 #include <string>                        // for string, operator+
 #include <utility>                       // for move
 
-#define WRAPPED_JSON_CONFIGURATION(obj) (static_cast<value *>((obj)->json_configuration.wrapped))
+#define WRAPPED_JSON_CONFIGURATION(obj) (static_cast<value *>((obj)->json_configuration.wrapped.get()))
 
 #define GET_CONNECTIONS                                                                            \
   if (this->json_configuration.IsNull()) {                                                         \
diff --git a/src/caosdb/utility.cpp b/src/caosdb/utility.cpp
index 8617138..9b5b5ec 100644
--- a/src/caosdb/utility.cpp
+++ b/src/caosdb/utility.cpp
@@ -144,19 +144,17 @@ auto load_json_file(const path &json_file) -> JsonValue {
   return {new value(_load_json_file(json_file))};
 }
 
+JsonValue::JsonValue(void *wrapped) {
+  this->wrapped = std::make_shared<value>(*static_cast<value *>(wrapped));
+}
+
 JsonValue::~JsonValue() { this->Reset(); }
 
-auto JsonValue::Reset() -> void {
-  void *tmp = this->wrapped;
-  this->wrapped = nullptr;
-  if (tmp != nullptr) {
-    delete static_cast<value *>(tmp);
-  }
-}
+auto JsonValue::Reset() -> void { this->wrapped .reset(); }
 
-JsonValue::JsonValue(const JsonValue &other) : wrapped(nullptr) {
+JsonValue::JsonValue(const JsonValue &other) {
   if (!other.IsNull()) {
-    this->wrapped = new value(*static_cast<value *>(other.wrapped));
+    this->wrapped = std::make_shared<value>(*static_cast<value *>(other.wrapped.get()));
   }
 }
 
@@ -164,7 +162,7 @@ auto JsonValue::operator=(const JsonValue &other) -> JsonValue & {
   if (this != &other) {
     Reset();
     if (!other.IsNull()) {
-      this->wrapped = new value(*static_cast<value *>(other.wrapped));
+      this->wrapped = std::make_shared<value>(*static_cast<value *>(other.wrapped.get()));
     }
   }
   return *this;
@@ -172,15 +170,13 @@ auto JsonValue::operator=(const JsonValue &other) -> JsonValue & {
 
 JsonValue::JsonValue(JsonValue &&other) noexcept : wrapped(nullptr) {
   if (!other.IsNull()) {
-    this->wrapped = other.wrapped;
-    other.wrapped = nullptr;
+    this->wrapped = std::move(other.wrapped);
   }
 }
 
 auto JsonValue::operator=(JsonValue &&other) noexcept -> JsonValue & {
   if (this != &other) {
-    this->wrapped = other.wrapped;
-    other.wrapped = nullptr;
+    this->wrapped = std::move(other.wrapped);
   }
   return *this;
 }
-- 
GitLab