static_function_deleter.
[folly.git] / folly / test / MemoryTest.cpp
index c9a2b0ea6d95c3bd70717cd76852f729b25f5ce1..03c53ee7508474af90e2a910b3b6b7ec0cd980af 100644 (file)
 
 using namespace folly;
 
+namespace {
+class disposable {
+ public:
+  explicit disposable(std::function<void()> onDispose) :
+    onDispose_(std::move(onDispose)) {}
+  static void dispose(disposable* f) {
+    ASSERT_NE(nullptr, f);
+    f->onDispose_();
+    delete f;
+  }
+ private:
+  std::function<void()> onDispose_;
+};
+}
+
+TEST(static_function_deleter, example) {
+  size_t count = 0;
+  using disposable_deleter =
+    static_function_deleter<disposable, &disposable::dispose>;
+  make_unique<disposable, disposable_deleter>([&] { ++count; });
+  EXPECT_EQ(1, count);
+}
+
+TEST(static_function_deleter, nullptr) {
+  using disposable_deleter =
+    static_function_deleter<disposable, &disposable::dispose>;
+  std::unique_ptr<disposable, disposable_deleter>(nullptr);
+}
+
 TEST(shared_ptr, example) {
   auto uptr = make_unique<std::string>("hello");
   auto sptr = to_shared_ptr(std::move(uptr));