Commit 0fe785ba authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by facebook-github-bot-4

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<T, D>` rather than just `unique_ptr<T>`.

Reviewed By: @lbrandy

Differential Revision: D2438217
parent 5db15d56
...@@ -99,8 +99,8 @@ struct static_function_deleter { ...@@ -99,8 +99,8 @@ struct static_function_deleter {
* *
* using T = foobar::cpp2::FooBarServiceAsyncClient; * using T = foobar::cpp2::FooBarServiceAsyncClient;
*/ */
template <typename T> template <typename T, typename D>
std::shared_ptr<T> to_shared_ptr(std::unique_ptr<T>&& ptr) { std::shared_ptr<T> to_shared_ptr(std::unique_ptr<T, D>&& ptr) {
return std::shared_ptr<T>(std::move(ptr)); return std::shared_ptr<T>(std::move(ptr));
} }
......
...@@ -54,13 +54,26 @@ TEST(static_function_deleter, nullptr) { ...@@ -54,13 +54,26 @@ TEST(static_function_deleter, nullptr) {
std::unique_ptr<disposable, disposable_deleter>(nullptr); std::unique_ptr<disposable, disposable_deleter>(nullptr);
} }
TEST(shared_ptr, example) { TEST(to_shared_ptr, example) {
auto uptr = make_unique<std::string>("hello"); auto uptr = make_unique<std::string>("hello");
auto sptr = to_shared_ptr(std::move(uptr)); auto sptr = to_shared_ptr(std::move(uptr));
EXPECT_EQ(nullptr, uptr); EXPECT_EQ(nullptr, uptr);
EXPECT_EQ("hello", *sptr); EXPECT_EQ("hello", *sptr);
} }
TEST(to_shared_ptr, example_with_dtor) {
bool disposed = false;
using disposable_deleter =
static_function_deleter<disposable, &disposable::dispose>;
auto uptr =
make_unique<disposable, disposable_deleter>([&] { disposed = true; });
EXPECT_FALSE(disposed);
auto sptr = to_shared_ptr(std::move(uptr));
EXPECT_FALSE(disposed);
sptr = nullptr;
EXPECT_TRUE(disposed);
}
template <std::size_t> struct T {}; template <std::size_t> struct T {};
template <std::size_t> struct S {}; template <std::size_t> struct S {};
template <std::size_t> struct P {}; template <std::size_t> struct P {};
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment