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