Skip to content
Open
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
5 changes: 3 additions & 2 deletions language/libs/noisy.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <string>
#include <string_view>

class Noisy
{
Expand All @@ -8,8 +9,8 @@ class Noisy
std::cout << "constructed without name\n";
}

explicit Noisy(std::string const& name)
: name{name}
explicit Noisy(std::string_view const& nm)
: name{nm}
{
std::cout << "constructed " << name << '\n';
}
Expand Down
41 changes: 40 additions & 1 deletion language/libs/pointers.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <cassert>
#include <deque>
#include <iostream>
#include <string>
#include <string_view>
#include <type_traits>
#include <vector>
#include "noisy.hpp"
Expand Down Expand Up @@ -245,6 +245,10 @@ template<typename Value>
class VolatilePtr
{
public:
VolatilePtr()
: mSlotRef{SlotRef::Owner{}.get(nullptr)}
{}

constexpr VolatilePtr(SlotRef const& slotRef) noexcept
: mSlotRef{slotRef}
{}
Expand Down Expand Up @@ -393,6 +397,27 @@ VolatilePtr<Noisy> foo()
return stackRef;
}

struct Producer
{
Target<Noisy> create(std::string_view const& name)
{
auto result{Target<Noisy>::create(name)};
// lastCreated will follow the returned Target.
lastCreated = result.ptr();
return result;
}

void consume(Target<Noisy> target) const
{
std::cout <<
"consume(target=" << &*target <<
") lastCreated=" << lastCreated.get() <<
'\n';
}

VolatilePtr<Noisy> lastCreated{};
};

int main()
{
std::cout << '\n' << R"(auto stackRef{foo()};)" << '\n';
Expand All @@ -414,6 +439,20 @@ int main()
}
std::cout << "vecRef=" << vecRef->name << '\n';

{
Producer producer{};
std::cout << '\n' << R"(auto const result{producer.create("producer")};)" << '\n';
auto result{producer.create("producer")};
std::cout <<
"result=" << result->name <<
" lastCreated=" << producer.lastCreated->name <<
'\n';
// lastCreated will follow the moved Target.
std::cout << R"(producer.consume(std::move(result));)" << '\n';
producer.consume(std::move(result));
std::cout << "lastCreated=" << producer.lastCreated.get() << '\n';
}

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