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

DelayedDestructionUniquePtr

Summary: To ease writing and reading IO code.

Reviewed By: iahs

Differential Revision: D32220718

fbshipit-source-id: db695eb33a60aae0db1b34991bd7d5ce33489f86
parent cfbc23fa
...@@ -25,8 +25,7 @@ namespace folly { ...@@ -25,8 +25,7 @@ namespace folly {
// generic TimerFD based timeout manager // generic TimerFD based timeout manager
class TimerFDTimeoutManager : public TimerFD { class TimerFDTimeoutManager : public TimerFD {
public: public:
using UniquePtr = using UniquePtr = DelayedDestructionUniquePtr<TimerFDTimeoutManager>;
std::unique_ptr<TimerFDTimeoutManager, DelayedDestruction::Destructor>;
using SharedPtr = std::shared_ptr<TimerFDTimeoutManager>; using SharedPtr = std::shared_ptr<TimerFDTimeoutManager>;
public: public:
......
...@@ -35,9 +35,7 @@ class AsyncPipeReader : public EventHandler, ...@@ -35,9 +35,7 @@ class AsyncPipeReader : public EventHandler,
public AsyncReader, public AsyncReader,
public DelayedDestruction { public DelayedDestruction {
public: public:
typedef std:: using UniquePtr = folly::DelayedDestructionUniquePtr<AsyncPipeReader>;
unique_ptr<AsyncPipeReader, folly::DelayedDestruction::Destructor>
UniquePtr;
static UniquePtr newReader( static UniquePtr newReader(
folly::EventBase* eventBase, NetworkSocket pipeFd) { folly::EventBase* eventBase, NetworkSocket pipeFd) {
...@@ -96,9 +94,7 @@ class AsyncPipeWriter : public EventHandler, ...@@ -96,9 +94,7 @@ class AsyncPipeWriter : public EventHandler,
public AsyncWriter, public AsyncWriter,
public DelayedDestruction { public DelayedDestruction {
public: public:
typedef std:: using UniquePtr = folly::DelayedDestructionUniquePtr<AsyncPipeWriter>;
unique_ptr<AsyncPipeWriter, folly::DelayedDestruction::Destructor>
UniquePtr;
static UniquePtr newWriter( static UniquePtr newWriter(
folly::EventBase* eventBase, NetworkSocket pipeFd) { folly::EventBase* eventBase, NetworkSocket pipeFd) {
......
...@@ -16,6 +16,8 @@ ...@@ -16,6 +16,8 @@
#pragma once #pragma once
#include <memory>
#include <folly/io/async/DelayedDestructionBase.h> #include <folly/io/async/DelayedDestructionBase.h>
namespace folly { namespace folly {
...@@ -111,4 +113,14 @@ class DelayedDestruction : public DelayedDestructionBase { ...@@ -111,4 +113,14 @@ class DelayedDestruction : public DelayedDestructionBase {
delete this; delete this;
} }
}; };
template <typename T>
using DelayedDestructionUniquePtr =
std::unique_ptr<T, DelayedDestruction::Destructor>;
template <typename T, typename... A>
DelayedDestructionUniquePtr<T> makeDelayedDestructionUniquePtr(A&&... a) {
return DelayedDestructionUniquePtr<T>{new T(static_cast<A&&>(a)...)};
}
} // namespace folly } // namespace folly
...@@ -83,6 +83,8 @@ class NotificationQueue { ...@@ -83,6 +83,8 @@ class NotificationQueue {
*/ */
class Consumer : public DelayedDestruction, private EventHandler { class Consumer : public DelayedDestruction, private EventHandler {
public: public:
using UniquePtr = DelayedDestructionUniquePtr<Consumer>;
enum : uint16_t { kDefaultMaxReadAtOnce = 10 }; enum : uint16_t { kDefaultMaxReadAtOnce = 10 };
Consumer() Consumer()
...@@ -92,8 +94,7 @@ class NotificationQueue { ...@@ -92,8 +94,7 @@ class NotificationQueue {
// create a consumer in-place, without the need to build new class // create a consumer in-place, without the need to build new class
template <typename TCallback> template <typename TCallback>
static std::unique_ptr<Consumer, DelayedDestruction::Destructor> make( static UniquePtr make(TCallback&& callback);
TCallback&& callback);
/** /**
* messageAvailable() will be invoked whenever a new * messageAvailable() will be invoked whenever a new
...@@ -888,17 +889,11 @@ struct notification_queue_consumer_wrapper ...@@ -888,17 +889,11 @@ struct notification_queue_consumer_wrapper
template <typename MessageT> template <typename MessageT>
template <typename TCallback> template <typename TCallback>
std::unique_ptr< auto NotificationQueue<MessageT>::Consumer::make(TCallback&& callback)
typename NotificationQueue<MessageT>::Consumer, -> UniquePtr {
DelayedDestruction::Destructor> using CB = typename std::decay<TCallback>::type;
NotificationQueue<MessageT>::Consumer::make(TCallback&& callback) { using W = detail::notification_queue_consumer_wrapper<MessageT, CB>;
return std::unique_ptr< return makeDelayedDestructionUniquePtr<W>(std::forward<TCallback>(callback));
NotificationQueue<MessageT>::Consumer,
DelayedDestruction::Destructor>(
new detail::notification_queue_consumer_wrapper<
MessageT,
typename std::decay<TCallback>::type>(
std::forward<TCallback>(callback)));
} }
} // namespace folly } // namespace folly
...@@ -378,10 +378,8 @@ void QueueTest::destroyCallback() { ...@@ -378,10 +378,8 @@ void QueueTest::destroyCallback() {
// This way one consumer will be destroyed from inside its messageAvailable() // This way one consumer will be destroyed from inside its messageAvailable()
// callback, and one consume will be destroyed when it isn't inside // callback, and one consume will be destroyed when it isn't inside
// messageAvailable(). // messageAvailable().
std::unique_ptr<DestroyTestConsumer, DelayedDestruction::Destructor> auto consumer1 = makeDelayedDestructionUniquePtr<DestroyTestConsumer>();
consumer1(new DestroyTestConsumer); auto consumer2 = makeDelayedDestructionUniquePtr<DestroyTestConsumer>();
std::unique_ptr<DestroyTestConsumer, DelayedDestruction::Destructor>
consumer2(new DestroyTestConsumer);
std::function<void(int)> fn = [&](int) { std::function<void(int)> fn = [&](int) {
consumer1 = nullptr; consumer1 = nullptr;
consumer2 = nullptr; consumer2 = nullptr;
......
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