From 9781c2a0a49b5cfc5905f71fa53ca9071cdd1e65 Mon Sep 17 00:00:00 2001 From: Sergii Kryvonos Date: Mon, 17 Mar 2025 10:07:16 +0100 Subject: [PATCH] Clone-on-write aware std::move --- omnn/math/Valuable.cpp | 11 ++++++++++- omnn/math/Valuable.h | 2 ++ omnn/math/ValuableDescendantContract.h | 5 ++++- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/omnn/math/Valuable.cpp b/omnn/math/Valuable.cpp index 157109883f..366250c0e8 100644 --- a/omnn/math/Valuable.cpp +++ b/omnn/math/Valuable.cpp @@ -154,6 +154,11 @@ namespace omnn::math { } } + Valuable&& Valuable::move(Valuable&& value) noexcept { + value.clone_on_write(); + return static_cast(value); + } + bool Valuable::IsSubObject(const Valuable& o) const { if (exp) return exp->IsSubObject(o); @@ -193,7 +198,7 @@ namespace omnn::math { if (exp) return exp; else - LOG_AND_IMPLEMENT("SharedFromThis for " << *this); + return weak_from_this().lock(); } Valuable::Valuable(const Valuable& v, ValuableDescendantMarker) @@ -3427,6 +3432,10 @@ namespace std return v.Sqrt(); } + ::omnn::math::Valuable&& move(::omnn::math::Valuable&& value) { + return ::omnn::math::Valuable::move(static_cast<::omnn::math::Valuable&&>(value)); + } + ::omnn::math::Valuable tanh(const ::omnn::math::Valuable& value) { return value.Tanh(); diff --git a/omnn/math/Valuable.h b/omnn/math/Valuable.h index 130385fd4e..ad13b933dc 100644 --- a/omnn/math/Valuable.h +++ b/omnn/math/Valuable.h @@ -61,6 +61,7 @@ namespace std { omnn::math::Valuable abs(const omnn::math::Valuable&); omnn::math::Valuable log(const omnn::math::Valuable&); omnn::math::Valuable pow(const omnn::math::Valuable&, const omnn::math::Valuable&); +omnn::math::Valuable&& move(omnn::math::Valuable&&); omnn::math::Valuable sqrt(const omnn::math::Valuable&); omnn::math::Valuable tanh(const omnn::math::Valuable&); @@ -170,6 +171,7 @@ class Valuable constexpr const encapsulated_instance& getInst() const { return exp; } void MarkNotOptimized(); + static Valuable&& move(Valuable&&) noexcept; /// /// Depends on optimizing goals, the view may set different. diff --git a/omnn/math/ValuableDescendantContract.h b/omnn/math/ValuableDescendantContract.h index 0733a17925..de1c7dba25 100644 --- a/omnn/math/ValuableDescendantContract.h +++ b/omnn/math/ValuableDescendantContract.h @@ -140,7 +140,10 @@ namespace math { Valuable::encapsulated_instance SharedFromThis() override { auto allocated = getAllocSize(); - auto ptr = std::make_shared(std::move(as())); + auto ptr = Valuable::SharedFromThis(); + if (!ptr) { + ptr = std::make_shared(std::move(Ref())); + } Valuable::~Valuable(); // ensure proper resource reallocation before reinitializing base part of the object new (this) Valuable(std::static_pointer_cast(ptr)); setAllocSize(allocated);