Commit b3cbe1d5 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook GitHub Bot

expand the erased_unique_ptr facility

Summary:
The interface:

  erased_unique_ptr
  to_erased_unique_ptr
  make_erased_unique
  copy_to_erased_unique_ptr
  empty_erased_unique_ptr

Reviewed By: iahs

Differential Revision: D26980776

fbshipit-source-id: c520993a2191e7f74c5056a4854e2b85731a09a0
parent 63ee5137
......@@ -427,22 +427,51 @@ std::shared_ptr<remove_cvref_t<T>> copy_to_shared_ptr(T&& t) {
return std::make_shared<remove_cvref_t<T>>(static_cast<T&&>(t));
}
/**
* to_erased_unique_ptr
*
* Wraps a raw pointer in a unique_ptr<void> with appropriate deleter.
* Makes it as easy to use as shared_ptr<void> when refcounting is not needed.
*/
// erased_unique_ptr
//
// A type-erased smart-ptr with unique ownership to a heap-allocated object.
using erased_unique_ptr = std::unique_ptr<void, void (*)(void*)>;
// to_erased_unique_ptr
//
// Converts an owning pointer to an object to an erased_unique_ptr.
template <typename T>
erased_unique_ptr to_erased_unique_ptr(T* ptr) {
erased_unique_ptr to_erased_unique_ptr(T* const ptr) noexcept {
return {ptr, detail::thunk::ruin<T>};
}
// to_erased_unique_ptr
//
// Converts an owning std::unique_ptr to an erased_unique_ptr.
template <typename T>
erased_unique_ptr to_erased_unique_ptr(std::unique_ptr<T> ptr) {
erased_unique_ptr to_erased_unique_ptr(std::unique_ptr<T> ptr) noexcept {
return to_erased_unique_ptr(ptr.release());
}
// make_erased_unique
//
// Allocate an object of the T on the heap, constructed with a..., and return
// an owning erased_unique_ptr to it.
template <typename T, typename... A>
erased_unique_ptr make_erased_unique(A&&... a) {
return to_erased_unique_ptr(std::make_unique<T>(static_cast<A&&>(a)...));
}
// copy_to_erased_unique_ptr
//
// Copy an object to the heap and return an owning erased_unique_ptr to it.
template <typename T>
erased_unique_ptr copy_to_erased_unique_ptr(T&& obj) {
return to_erased_unique_ptr(copy_to_unique_ptr(static_cast<T&&>(obj)));
}
// empty_erased_unique_ptr
//
// Return an empty erased_unique_ptr.
inline erased_unique_ptr empty_erased_unique_ptr() {
return {nullptr, nullptr};
}
/**
* SysAllocator
*
......
......@@ -175,7 +175,9 @@ TEST(copy_to_shared_ptr, example) {
}
TEST(to_erased_unique_ptr, example) {
erased_unique_ptr ptr = to_erased_unique_ptr(new int(42));
erased_unique_ptr ptr = empty_erased_unique_ptr();
ptr = to_erased_unique_ptr(new int(42));
EXPECT_EQ(42, *static_cast<int*>(ptr.get()));
ptr = to_erased_unique_ptr(new std::string("foo"));
......@@ -189,6 +191,12 @@ TEST(to_erased_unique_ptr, example) {
ptr = to_erased_unique_ptr(std::make_unique<int>(42));
EXPECT_EQ(42, *static_cast<int*>(ptr.get()));
ptr = make_erased_unique<std::string>(7, 'a');
EXPECT_EQ("aaaaaaa", *static_cast<std::string*>(ptr.get()));
ptr = copy_to_erased_unique_ptr(std::string("bbbbbbb"));
EXPECT_EQ("bbbbbbb", *static_cast<std::string*>(ptr.get()));
}
TEST(SysAllocator, equality) {
......
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