diff --git a/omnn/math/Valuable.cpp b/omnn/math/Valuable.cpp index 157109883..366250c0e 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 130385fd4..ad13b933d 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 0733a1792..de1c7dba2 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);