Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clone-on-write aware std::move #1217

Merged
merged 1 commit into from
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion omnn/math/Valuable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ namespace omnn::math {
}
}

Valuable&& Valuable::move(Valuable&& value) noexcept {
value.clone_on_write();
return static_cast<Valuable&&>(value);
}

bool Valuable::IsSubObject(const Valuable& o) const {
if (exp)
return exp->IsSubObject(o);
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions omnn/math/Valuable.h
Original file line number Diff line number Diff line change
Expand Up @@ -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&);

Expand Down Expand Up @@ -170,6 +171,7 @@ class Valuable
constexpr const encapsulated_instance& getInst() const { return exp; }
void MarkNotOptimized();

static Valuable&& move(Valuable&&) noexcept;

/// <summary>
/// Depends on optimizing goals, the view may set different.
Expand Down
5 changes: 4 additions & 1 deletion omnn/math/ValuableDescendantContract.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,10 @@ namespace math {

Valuable::encapsulated_instance SharedFromThis() override {
auto allocated = getAllocSize();
auto ptr = std::make_shared<Chld>(std::move(as<Chld>()));
auto ptr = Valuable::SharedFromThis();
if (!ptr) {
ptr = std::make_shared<Chld>(std::move(Ref()));
}
Valuable::~Valuable(); // ensure proper resource reallocation before reinitializing base part of the object
new (this) Valuable(std::static_pointer_cast<Valuable>(ptr));
setAllocSize(allocated);
Expand Down
Loading