@@ -20,33 +20,26 @@ CZMQNotificationInterface::CZMQNotificationInterface() : pcontext(nullptr)
20
20
CZMQNotificationInterface::~CZMQNotificationInterface ()
21
21
{
22
22
Shutdown ();
23
-
24
- for (std::list<CZMQAbstractNotifier*>::iterator i=notifiers.begin (); i!=notifiers.end (); ++i)
25
- {
26
- delete *i;
27
- }
28
23
}
29
24
30
25
std::list<const CZMQAbstractNotifier*> CZMQNotificationInterface::GetActiveNotifiers () const
31
26
{
32
27
std::list<const CZMQAbstractNotifier*> result;
33
- for (const auto * n : notifiers) {
34
- result.push_back (n);
28
+ for (const auto & n : notifiers) {
29
+ result.push_back (n. get () );
35
30
}
36
31
return result;
37
32
}
38
33
39
34
CZMQNotificationInterface* CZMQNotificationInterface::Create ()
40
35
{
41
- CZMQNotificationInterface* notificationInterface = nullptr ;
42
36
std::map<std::string, CZMQNotifierFactory> factories;
43
- std::list<CZMQAbstractNotifier*> notifiers;
44
-
45
37
factories[" pubhashblock" ] = CZMQAbstractNotifier::Create<CZMQPublishHashBlockNotifier>;
46
38
factories[" pubhashtx" ] = CZMQAbstractNotifier::Create<CZMQPublishHashTransactionNotifier>;
47
39
factories[" pubrawblock" ] = CZMQAbstractNotifier::Create<CZMQPublishRawBlockNotifier>;
48
40
factories[" pubrawtx" ] = CZMQAbstractNotifier::Create<CZMQPublishRawTransactionNotifier>;
49
41
42
+ std::list<std::unique_ptr<CZMQAbstractNotifier>> notifiers;
50
43
for (const auto & entry : factories)
51
44
{
52
45
std::string arg (" -zmq" + entry.first );
@@ -58,23 +51,21 @@ CZMQNotificationInterface* CZMQNotificationInterface::Create()
58
51
notifier->SetType (entry.first );
59
52
notifier->SetAddress (address);
60
53
notifier->SetOutboundMessageHighWaterMark (static_cast <int >(gArgs .GetArg (arg + " hwm" , CZMQAbstractNotifier::DEFAULT_ZMQ_SNDHWM)));
61
- notifiers.push_back (notifier);
54
+ notifiers.emplace_back (notifier);
62
55
}
63
56
}
64
57
65
58
if (!notifiers.empty ())
66
59
{
67
- notificationInterface = new CZMQNotificationInterface ();
68
- notificationInterface->notifiers = notifiers;
60
+ std::unique_ptr<CZMQNotificationInterface> notificationInterface ( new CZMQNotificationInterface () );
61
+ notificationInterface->notifiers = std::move ( notifiers) ;
69
62
70
- if (!notificationInterface->Initialize ())
71
- {
72
- delete notificationInterface;
73
- notificationInterface = nullptr ;
63
+ if (notificationInterface->Initialize ()) {
64
+ return notificationInterface.release ();
74
65
}
75
66
}
76
67
77
- return notificationInterface ;
68
+ return nullptr ;
78
69
}
79
70
80
71
// Called at startup to conditionally set up ZMQ socket(s)
@@ -95,26 +86,15 @@ bool CZMQNotificationInterface::Initialize()
95
86
return false ;
96
87
}
97
88
98
- std::list<CZMQAbstractNotifier*>::iterator i=notifiers.begin ();
99
- for (; i!=notifiers.end (); ++i)
100
- {
101
- CZMQAbstractNotifier *notifier = *i;
102
- if (notifier->Initialize (pcontext))
103
- {
89
+ for (auto & notifier : notifiers) {
90
+ if (notifier->Initialize (pcontext)) {
104
91
LogPrint (BCLog::ZMQ, " zmq: Notifier %s ready (address = %s)\n " , notifier->GetType (), notifier->GetAddress ());
105
- }
106
- else
107
- {
92
+ } else {
108
93
LogPrint (BCLog::ZMQ, " zmq: Notifier %s failed (address = %s)\n " , notifier->GetType (), notifier->GetAddress ());
109
- break ;
94
+ return false ;
110
95
}
111
96
}
112
97
113
- if (i!=notifiers.end ())
114
- {
115
- return false ;
116
- }
117
-
118
98
return true ;
119
99
}
120
100
@@ -124,9 +104,7 @@ void CZMQNotificationInterface::Shutdown()
124
104
LogPrint (BCLog::ZMQ, " zmq: Shutdown notification interface\n " );
125
105
if (pcontext)
126
106
{
127
- for (std::list<CZMQAbstractNotifier*>::iterator i=notifiers.begin (); i!=notifiers.end (); ++i)
128
- {
129
- CZMQAbstractNotifier *notifier = *i;
107
+ for (auto & notifier : notifiers) {
130
108
LogPrint (BCLog::ZMQ, " zmq: Shutdown notifier %s at %s\n " , notifier->GetType (), notifier->GetAddress ());
131
109
notifier->Shutdown ();
132
110
}
@@ -141,9 +119,9 @@ void CZMQNotificationInterface::UpdatedBlockTip(const CBlockIndex *pindexNew, co
141
119
if (fInitialDownload || pindexNew == pindexFork) // In IBD or blocks were disconnected without any new ones
142
120
return ;
143
121
144
- for (std::list<CZMQAbstractNotifier*>::iterator i = notifiers.begin (); i!=notifiers.end (); )
122
+ for (auto i = notifiers.begin (); i!=notifiers.end (); )
145
123
{
146
- CZMQAbstractNotifier *notifier = *i ;
124
+ CZMQAbstractNotifier *notifier = i-> get () ;
147
125
if (notifier->NotifyBlock (pindexNew))
148
126
{
149
127
i++;
@@ -162,9 +140,9 @@ void CZMQNotificationInterface::TransactionAddedToMempool(const CTransactionRef&
162
140
// all the same external callback.
163
141
const CTransaction& tx = *ptx;
164
142
165
- for (std::list<CZMQAbstractNotifier*>::iterator i = notifiers.begin (); i!=notifiers.end (); )
143
+ for (auto i = notifiers.begin (); i!=notifiers.end (); )
166
144
{
167
- CZMQAbstractNotifier *notifier = *i ;
145
+ CZMQAbstractNotifier *notifier = i-> get () ;
168
146
if (notifier->NotifyTransaction (tx))
169
147
{
170
148
i++;
0 commit comments