Skip to content

Commit

Permalink
Allow re-targetting slots during move-construction.
Browse files Browse the repository at this point in the history
  • Loading branch information
jkuebart committed Jul 22, 2019
1 parent 003dca1 commit 87cae67
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions language/libs/pointers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,19 @@ class SlotRef::Owner
{
public:
Owner() = default;
Owner(Owner const&) = delete;

/**
* Take ownership of another owner's slot.
*
* @param owner The owner to be disowned.
*/

Owner(Owner&& owner) noexcept
: mRecord{owner.mRecord}
{
owner.mRecord = nullptr;
}

Owner& operator =(Owner const&) = delete;

~Owner() noexcept {
Expand All @@ -203,6 +215,20 @@ class SlotRef::Owner
return SlotRef{*mRecord};
}

/**
* Re-target the owned slot.
*
* @note Ignored when no slot is owned.
* @param target The new target.
*/

void retarget(void* const target)
{
if (mRecord) {
mRecord->target = target;
}
}

private:
Registry::Record* mRecord{nullptr};
};
Expand Down Expand Up @@ -282,8 +308,10 @@ class Target

Target(Target&& target) noexcept(std::is_nothrow_move_constructible_v<Value>)
: mValue{std::forward<decltype(target.mValue)>(target.mValue)}
, mOwner{}
{}
, mOwner{std::move(target.mOwner)}
{
mOwner.retarget(&mValue);
}

Target& operator =(Target const& target) noexcept(std::is_nothrow_copy_assignable_v<Value>)
{
Expand Down Expand Up @@ -371,7 +399,7 @@ int main()
for (int i{0}; i != 4; ++i) {
noisies.emplace_back(Target<Noisy>::forwardCtor{}, "goodbye");
}
std::cout << "vecRef=" << vecRef.get() << '\n';
std::cout << "vecRef=" << vecRef->name << '\n';

std::cout << '\n' << R"(return 0;)" << '\n';
return 0;
Expand Down

0 comments on commit 87cae67

Please sign in to comment.