Repository navigation
Expand file tree
/
Copy pathAggregateEventHandler.h
More file actions
76 lines (66 loc) · 2.37 KB
/
Copy pathAggregateEventHandler.h
File metadata and controls
76 lines (66 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#pragma once
#include <memory>
#include <vector>
#include "Disruptor/IEventHandler.h"
#include "Disruptor/ILifecycleAware.h"
namespace Disruptor
{
/**
* An aggregate collection of IEventHandler<T> that get called in sequence for each event.
* \tparam T event implementation storing the data for sharing during exchange or parallel coordination of an event
*/
template <class T>
class AggregateEventHandler : public IEventHandler< T >, public ILifecycleAware
{
public:
/**
* Construct an aggregate collection of IEventHandler<T>/> to be called in sequence.
*
* \param eventHandlers to be called in sequence
*/
explicit AggregateEventHandler(const std::vector< std::shared_ptr< IEventHandler< T > > >& eventHandlers = {})
: m_eventHandlers(eventHandlers)
{
}
/**
* Called when a publisher has committed an event to the RingBuffer<T>
*
* \param data Data committed to the RingBuffer<T>/>
* \param sequence Sequence number committed to theRingBuffer<T>/>
* \param endOfBatch flag to indicate if this is the last event in a batch from theRingBuffer<T>/>
*/
void onEvent(T& data, std::int64_t sequence, bool endOfBatch) override
{
for (auto i = 0u; i < m_eventHandlers.size(); ++i)
{
m_eventHandlers[i]->onEvent(data, sequence, endOfBatch);
}
}
/**
* Called once on thread start before first event is available.
*/
void onStart() override
{
for (auto&& eventHandler : m_eventHandlers)
{
auto handler = std::dynamic_pointer_cast< ILifecycleAware >(eventHandler);
if (handler != nullptr)
handler->onStart();
}
}
/**
* Called once just before the thread is shutdown.
*/
void onShutdown() override
{
for (auto&& eventHandler : m_eventHandlers)
{
auto handler = std::dynamic_pointer_cast< ILifecycleAware >(eventHandler);
if (handler != nullptr)
handler->onShutdown();
}
}
private:
std::vector< std::shared_ptr< IEventHandler< T > > > m_eventHandlers;
};
} // namespace Disruptor