diff --git a/CMakeLists.txt b/CMakeLists.txt index 84738ea197786ab8c9f3f73371e6b3c20c386ef4..09d03a7a78fc5b9bd3b49f242a32d97debccfe3a 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 0fab6250ec5746ec57f8be1c63387736ac03fe80..1a6bcf2e6c164ab9a42a701aa7e9ba5a2769d1fc 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 5d1874a74e23f6f85bed6f180ca2a52ecb23ec79..410351f6f7b26ce8d3fb910998cc48b792b9a080 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 8617138ab6a4f8dac6bcc016ed15bcdddd59fbb4..9b5b5ec056193dbe3a1cdd77451b4b645f503073 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; }