From 0fe785ba8cc9aa178e6c96b389fe585a023b43e6 Mon Sep 17 00:00:00 2001 From: Yedidya Feldblum Date: Mon, 14 Sep 2015 10:21:23 -0700 Subject: [PATCH] Support unique_ptr instances with custom deleters in to_shared_ptr Summary: [Folly] Support `unique_ptr` instances with custom deleters in `to_shared_ptr`. We now support `unique_ptr` rather than just `unique_ptr`. Reviewed By: @lbrandy Differential Revision: D2438217 --- folly/Memory.h | 4 ++-- folly/test/MemoryTest.cpp | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/folly/Memory.h b/folly/Memory.h index cf32e743..b58edf20 100644 --- a/folly/Memory.h +++ b/folly/Memory.h @@ -99,8 +99,8 @@ struct static_function_deleter { * * using T = foobar::cpp2::FooBarServiceAsyncClient; */ -template -std::shared_ptr to_shared_ptr(std::unique_ptr&& ptr) { +template +std::shared_ptr to_shared_ptr(std::unique_ptr&& ptr) { return std::shared_ptr(std::move(ptr)); } diff --git a/folly/test/MemoryTest.cpp b/folly/test/MemoryTest.cpp index 03c53ee7..a62cd11e 100644 --- a/folly/test/MemoryTest.cpp +++ b/folly/test/MemoryTest.cpp @@ -54,13 +54,26 @@ TEST(static_function_deleter, nullptr) { std::unique_ptr(nullptr); } -TEST(shared_ptr, example) { +TEST(to_shared_ptr, example) { auto uptr = make_unique("hello"); auto sptr = to_shared_ptr(std::move(uptr)); EXPECT_EQ(nullptr, uptr); EXPECT_EQ("hello", *sptr); } +TEST(to_shared_ptr, example_with_dtor) { + bool disposed = false; + using disposable_deleter = + static_function_deleter; + auto uptr = + make_unique([&] { disposed = true; }); + EXPECT_FALSE(disposed); + auto sptr = to_shared_ptr(std::move(uptr)); + EXPECT_FALSE(disposed); + sptr = nullptr; + EXPECT_TRUE(disposed); +} + template struct T {}; template struct S {}; template struct P {}; -- 2.34.1