Skip to content

Commit 7f2ad1b

Browse files
committed
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.
1 parent b93b9d5 commit 7f2ad1b

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

src/zmq/zmqabstractnotifier.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@
77

88
#include <zmq/zmqconfig.h>
99

10+
#include <util/memory.h>
11+
12+
#include <memory>
13+
1014
class CBlockIndex;
1115
class CZMQAbstractNotifier;
1216

13-
typedef CZMQAbstractNotifier* (*CZMQNotifierFactory)();
17+
using CZMQNotifierFactory = std::unique_ptr<CZMQAbstractNotifier> (*)();
1418

1519
class CZMQAbstractNotifier
1620
{
@@ -21,9 +25,9 @@ class CZMQAbstractNotifier
2125
virtual ~CZMQAbstractNotifier();
2226

2327
template <typename T>
24-
static CZMQAbstractNotifier* Create()
28+
static std::unique_ptr<CZMQAbstractNotifier> Create()
2529
{
26-
return new T();
30+
return MakeUnique<T>();
2731
}
2832

2933
std::string GetType() const { return type; }

src/zmq/zmqnotificationinterface.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ CZMQNotificationInterface* CZMQNotificationInterface::Create()
4545
std::string arg("-zmq" + entry.first);
4646
if (gArgs.IsArgSet(arg))
4747
{
48-
CZMQNotifierFactory factory = entry.second;
49-
std::string address = gArgs.GetArg(arg, "");
50-
CZMQAbstractNotifier *notifier = factory();
48+
const auto& factory = entry.second;
49+
const std::string address = gArgs.GetArg(arg, "");
50+
std::unique_ptr<CZMQAbstractNotifier> notifier = factory();
5151
notifier->SetType(entry.first);
5252
notifier->SetAddress(address);
5353
notifier->SetOutboundMessageHighWaterMark(static_cast<int>(gArgs.GetArg(arg + "hwm", CZMQAbstractNotifier::DEFAULT_ZMQ_SNDHWM)));
54-
notifiers.emplace_back(notifier);
54+
notifiers.push_back(std::move(notifier));
5555
}
5656
}
5757

0 commit comments

Comments
 (0)