From 7f2ad1b9acef4ccc1b3e1a9f551416235d95cbfd Mon Sep 17 00:00:00 2001 From: Daniel Kraft Date: Tue, 17 Jul 2018 12:36:22 +0200 Subject: [PATCH] Use std::unique_ptr for CZMQNotifierFactory. Instead of returning a raw pointer from CZMQNotifierFactory and implicitly requiring the caller to know that it has to take ownership, return a std::unique_ptr to make this explicit. This also changes the typedef for CZMQNotifierFactory to use the new C++11 using syntax, which makes it (a little) less cryptic. --- src/zmq/zmqabstractnotifier.h | 10 +++++++--- src/zmq/zmqnotificationinterface.cpp | 8 ++++---- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/zmq/zmqabstractnotifier.h b/src/zmq/zmqabstractnotifier.h index 887dde7b271..8377a26d3ad 100644 --- a/src/zmq/zmqabstractnotifier.h +++ b/src/zmq/zmqabstractnotifier.h @@ -7,10 +7,14 @@ #include +#include + +#include + class CBlockIndex; class CZMQAbstractNotifier; -typedef CZMQAbstractNotifier* (*CZMQNotifierFactory)(); +using CZMQNotifierFactory = std::unique_ptr (*)(); class CZMQAbstractNotifier { @@ -21,9 +25,9 @@ public: virtual ~CZMQAbstractNotifier(); template - static CZMQAbstractNotifier* Create() + static std::unique_ptr Create() { - return new T(); + return MakeUnique(); } std::string GetType() const { return type; } diff --git a/src/zmq/zmqnotificationinterface.cpp b/src/zmq/zmqnotificationinterface.cpp index 449651e65f6..9e9acdc568a 100644 --- a/src/zmq/zmqnotificationinterface.cpp +++ b/src/zmq/zmqnotificationinterface.cpp @@ -45,13 +45,13 @@ CZMQNotificationInterface* CZMQNotificationInterface::Create() std::string arg("-zmq" + entry.first); if (gArgs.IsArgSet(arg)) { - CZMQNotifierFactory factory = entry.second; - std::string address = gArgs.GetArg(arg, ""); - CZMQAbstractNotifier *notifier = factory(); + const auto& factory = entry.second; + const std::string address = gArgs.GetArg(arg, ""); + std::unique_ptr notifier = factory(); notifier->SetType(entry.first); notifier->SetAddress(address); notifier->SetOutboundMessageHighWaterMark(static_cast(gArgs.GetArg(arg + "hwm", CZMQAbstractNotifier::DEFAULT_ZMQ_SNDHWM))); - notifiers.emplace_back(notifier); + notifiers.push_back(std::move(notifier)); } }