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

Allow re-targetting slots during move-construction. #15

Merged
merged 2 commits into from
Jul 23, 2019
Merged
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
55 changes: 48 additions & 7 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 All @@ -211,7 +237,8 @@ class SlotRef::Owner
/**
* I provide a typed pointer for a pointer stored in the registry.
*
* I become invalid when that entity expires.
* My target changes when the slot is re-targetted and I become invalid
* when it is cleared.
*/

template<typename Value>
Expand Down Expand Up @@ -282,8 +309,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 @@ -318,8 +347,20 @@ class Target
}

/**
* @return A volatile pointer to the Value of this object which expires
* when it is destroyed.
* The pointer-like objects created by this method behave as follows:
* 1) They refer to the value held by this Target instance.
* 2) They expire (i.e. become equivalent to nullptr) when this Target
* instance is destroyed.
* 3) When this Target instance appears as the source of a
* move-construction, all previously created pointers behave as if they
* were created by the newly constructed Target.
*
* @note New pointers created *after* this Target appeared as the source of
* a move-construction will again refer to *this* instance.
* @note This Target instance appearing as the source of a move-assignment
* has no effect on any pointers.
*
* @return A volatile pointer to the Value of this object.
*/

VolatilePtr<Value> ptr()
Expand Down Expand Up @@ -371,7 +412,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