From 7ae1e80fd6626ce07262656a7d822e68004754ae Mon Sep 17 00:00:00 2001 From: m-holger Date: Sun, 5 Feb 2023 18:53:55 +0000 Subject: Change JSON::Members::value to std::unique_ptr --- include/qpdf/JSON.hh | 10 +++++----- libqpdf/JSON.cc | 30 ++++++++++++++---------------- 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/include/qpdf/JSON.hh b/include/qpdf/JSON.hh index 8cd4e2fe..b368df99 100644 --- a/include/qpdf/JSON.hh +++ b/include/qpdf/JSON.hh @@ -425,7 +425,7 @@ class JSON std::function fn; }; - JSON(std::shared_ptr); + JSON(std::unique_ptr); static bool checkSchemaInternal( JSON_value* this_v, @@ -443,13 +443,13 @@ class JSON ~Members() = default; private: - Members(std::shared_ptr); + Members(std::unique_ptr); Members(Members const&) = delete; - std::shared_ptr value; + std::unique_ptr value; // start and end are only populated for objects created by parse - qpdf_offset_t start; - qpdf_offset_t end; + qpdf_offset_t start{0}; + qpdf_offset_t end{0}; }; std::shared_ptr m; diff --git a/libqpdf/JSON.cc b/libqpdf/JSON.cc index a5df6b91..cb60eabc 100644 --- a/libqpdf/JSON.cc +++ b/libqpdf/JSON.cc @@ -9,15 +9,13 @@ #include #include -JSON::Members::Members(std::shared_ptr value) : - value(value), - start(0), - end(0) +JSON::Members::Members(std::unique_ptr value) : + value(std::move(value)) { } -JSON::JSON(std::shared_ptr value) : - m(new Members(value)) +JSON::JSON(std::unique_ptr value) : + m(new Members(std::move(value))) { } @@ -278,7 +276,7 @@ JSON::encode_string(std::string const& str) JSON JSON::makeDictionary() { - return JSON(std::make_shared()); + return JSON(std::make_unique()); } JSON @@ -286,7 +284,7 @@ JSON::addDictionaryMember(std::string const& key, JSON const& val) { if (auto* obj = dynamic_cast(this->m->value.get())) { return obj->members[encode_string(key)] = - val.m->value ? val.m->value : std::make_shared(); + val.m->value ? val : makeNull(); } else { throw std::runtime_error( "JSON::addDictionaryMember called on non-dictionary"); @@ -311,7 +309,7 @@ JSON::checkDictionaryKeySeen(std::string const& key) JSON JSON::makeArray() { - return JSON(std::make_shared()); + return JSON(std::make_unique()); } JSON @@ -332,43 +330,43 @@ JSON::addArrayElement(JSON const& val) JSON JSON::makeString(std::string const& utf8) { - return JSON(std::make_shared(utf8)); + return JSON(std::make_unique(utf8)); } JSON JSON::makeInt(long long int value) { - return JSON(std::make_shared(value)); + return JSON(std::make_unique(value)); } JSON JSON::makeReal(double value) { - return JSON(std::make_shared(value)); + return JSON(std::make_unique(value)); } JSON JSON::makeNumber(std::string const& encoded) { - return JSON(std::make_shared(encoded)); + return JSON(std::make_unique(encoded)); } JSON JSON::makeBool(bool value) { - return JSON(std::make_shared(value)); + return JSON(std::make_unique(value)); } JSON JSON::makeNull() { - return JSON(std::make_shared()); + return JSON(std::make_unique()); } JSON JSON::makeBlob(std::function fn) { - return JSON(std::make_shared(fn)); + return JSON(std::make_unique(fn)); } bool -- cgit v1.2.3-54-g00ecf