Skip to content

Commit

Permalink
Add implicit default c-tor for PrimaryPtrRef
Browse files Browse the repository at this point in the history
Summary: For to passing over empty refs and default constructing non-owning references to the owning PrimaryPtr.

Reviewed By: ot

Differential Revision: D48320199

fbshipit-source-id: 3ab09a1b4281b65989accd633214377d043cd5b8
  • Loading branch information
stanionascu authored and facebook-github-bot committed Aug 17, 2023
1 parent 50e90ae commit ab45d9b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
2 changes: 2 additions & 0 deletions folly/experimental/PrimaryPtr.h
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,8 @@ PrimaryPtr<T> exchange(PrimaryPtr<T>& x, PrimaryPtr<T>&& newVal) noexcept {
template <typename T>
class PrimaryPtrRef {
public:
PrimaryPtrRef() = default;

// Attempts to lock a pointer. Returns null if pointer is not set or if
// join() was called or cleanup() work was started (even if the call to join()
// hasn't returned yet or the cleanup() work has not completed yet).
Expand Down
16 changes: 16 additions & 0 deletions folly/experimental/test/PrimaryPtrTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,19 @@ TEST(PrimaryPtrTest, Moves) {
b.join();
c.join();
}

TEST(PrimaryPtrTest, DefaultConstructPtrRef) {
auto ptr = std::make_unique<Primed>();
auto primaryPtr = folly::PrimaryPtr<Primed>{std::move(ptr)};

folly::PrimaryPtrRef<Primed> primaryPtrRef;
EXPECT_FALSE((bool)primaryPtrRef.lock());

primaryPtrRef = primaryPtr.ref();
EXPECT_TRUE((bool)primaryPtrRef.lock());
EXPECT_EQ(primaryPtrRef.lock(), primaryPtr.lock());

primaryPtr.join();
EXPECT_FALSE((bool)primaryPtr);
EXPECT_EQ(primaryPtrRef.lock().get(), nullptr);
}

0 comments on commit ab45d9b

Please sign in to comment.