From 6cf176900f0c89e20557742dbcd1174799440f8f Mon Sep 17 00:00:00 2001 From: Rye Mutt Date: Thu, 4 Jul 2024 05:02:46 -0400 Subject: Add move construction/assignment support for LLSD type --- indra/llcommon/llsd.cpp | 37 ++++++++++++++++++++++++++----------- indra/llcommon/llsd.h | 21 ++++++++++++++------- 2 files changed, 40 insertions(+), 18 deletions(-) (limited to 'indra/llcommon') diff --git a/indra/llcommon/llsd.cpp b/indra/llcommon/llsd.cpp index 0576182c98..2bbe06e72f 100644 --- a/indra/llcommon/llsd.cpp +++ b/indra/llcommon/llsd.cpp @@ -106,6 +106,9 @@ public: static void reset(Impl*& var, Impl* impl); ///< safely set var to refer to the new impl (possibly shared) + static void move(Impl*& var, Impl*& impl); + ///< safely move impl from one object to another + static Impl& safe( Impl*); static const Impl& safe(const Impl*); ///< since a NULL Impl* is used for undefined, this ensures there is @@ -232,7 +235,7 @@ namespace }; - class ImplBoolean + class ImplBoolean final : public ImplBase { public: @@ -255,7 +258,7 @@ namespace { return mValue ? "true" : ""; } - class ImplInteger + class ImplInteger final : public ImplBase { public: @@ -273,7 +276,7 @@ namespace { return llformat("%d", mValue); } - class ImplReal + class ImplReal final : public ImplBase { public: @@ -297,7 +300,7 @@ namespace { return llformat("%lg", mValue); } - class ImplString + class ImplString final : public ImplBase { public: @@ -357,7 +360,7 @@ namespace } - class ImplUUID + class ImplUUID final : public ImplBase { public: @@ -371,7 +374,7 @@ namespace }; - class ImplDate + class ImplDate final : public ImplBase { public: @@ -398,7 +401,7 @@ namespace }; - class ImplURI + class ImplURI final : public ImplBase { public: @@ -412,7 +415,7 @@ namespace }; - class ImplBinary + class ImplBinary final : public ImplBase { public: @@ -425,7 +428,7 @@ namespace }; - class ImplMap : public LLSD::Impl + class ImplMap final : public LLSD::Impl { private: typedef std::map> DataMap; @@ -539,7 +542,6 @@ namespace DataMap::iterator i = mData.lower_bound(k); if (i == mData.end() || mData.key_comp()(k, i->first)) { - return mData.emplace_hint(i, std::make_pair(k, LLSD()))->second; } @@ -577,7 +579,7 @@ namespace { //std::cout << " " << (*iter).first << ": " << (*iter).second << std::endl; Impl::calcStats((*iter).second, type_counts, share_counts); - iter++; + ++iter; } // Add in the values for this map @@ -774,6 +776,16 @@ void LLSD::Impl::reset(Impl*& var, Impl* impl) var = impl; } +void LLSD::Impl::move(Impl*& var, Impl*& impl) +{ + if (var && var->mUseCount != STATIC_USAGE_COUNT && --var->mUseCount == 0) + { + delete var; // destroy var if usage falls to 0 and not static + } + var = impl; // Steal impl to var without incrementing use since this is a move + impl = nullptr; // null out old-impl pointer +} + LLSD::Impl& LLSD::Impl::safe(Impl* impl) { static Impl theUndefined(STATIC_USAGE_COUNT); @@ -954,6 +966,9 @@ LLSD::~LLSD() { FREE_LLSD_OBJECT; Impl::reset(impl, 0) LLSD::LLSD(const LLSD& other) : impl(0) { ALLOC_LLSD_OBJECT; assign(other); } void LLSD::assign(const LLSD& other) { Impl::assign(impl, other.impl); } +LLSD::LLSD(LLSD&& other) noexcept : impl(nullptr) { ALLOC_LLSD_OBJECT; Impl::move(impl, other.impl); } +void LLSD::assign(LLSD&& other) { Impl::move(impl, other.impl); } +LLSD& LLSD::operator=(LLSD&& other) noexcept { Impl::move(impl, other.impl); return *this; } void LLSD::clear() { Impl::assignUndefined(impl); } diff --git a/indra/llcommon/llsd.h b/indra/llcommon/llsd.h index 77f1606554..781e8d58e9 100644 --- a/indra/llcommon/llsd.h +++ b/indra/llcommon/llsd.h @@ -161,6 +161,13 @@ public: //@} + /** @name Movable */ + //@{ + LLSD(LLSD&& other) noexcept; + void assign(LLSD&& other); + LLSD& operator=(LLSD&& other) noexcept; + //@} + void clear(); ///< resets to Undefined @@ -330,14 +337,14 @@ public: LLSD& operator[](const std::string_view); LLSD& operator[](const char* c) - { - return c ? (*this)[std::string_view(c)] : *this; - } + { + return c ? (*this)[std::string_view(c)] : *this; + } const LLSD& operator[](const std::string_view) const; - const LLSD& operator[](const char* c) const - { - return c ? (*this)[std::string_view(c)] : *this; - } + const LLSD& operator[](const char* c) const + { + return c ? (*this)[std::string_view(c)] : *this; + } //@} /** @name Array Values */ -- cgit v1.2.3