Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
event_emitter.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cassert>
4#include <vector>
5
8
9namespace bb::avm2::simulation {
10
11template <typename Event> class EventEmitterInterface {
12 public:
13 using Container = std::vector<Event>;
14
15 virtual ~EventEmitterInterface() = default;
16 // Pushes the event to the event container.
17 virtual void emit(Event&& event) = 0;
18};
19
20template <typename Event> class EventEmitter : public EventEmitterInterface<Event> {
21 public:
22 using Container = std::vector<Event>;
23
24 virtual ~EventEmitter() = default;
25 void emit(Event&& event) override { events.push_back(std::move(event)); };
26
27 const Container& get_events() const { return events; }
28 // Transfers ownership of the events to the caller (clears the internal container).
30
31 private:
33};
34
35// This is an EventEmitter that eagerly deduplicates events based on a provided key.
36template <typename Event> class DeduplicatingEventEmitter : public EventEmitter<Event> {
37 public:
38 virtual ~DeduplicatingEventEmitter() = default;
39
40 void emit(Event&& event) override
41 {
42 typename Event::Key key = event.get_key();
43 if (!elements_seen.contains(key)) {
44 elements_seen.insert(key);
46 }
47 };
48 // Transfers ownership of the events to the caller (clears the internal container).
54
55 private:
57};
58
59template <typename Event> class NoopEventEmitter : public EventEmitterInterface<Event> {
60 public:
61 using Container = std::vector<Event>;
62
63 virtual ~NoopEventEmitter() = default;
64
65 void emit(Event&&) override {};
66 // TODO: Get rid of this.
68};
69
70// This is an event emitter which only emits events once (it actually just _sets_ an event).
71// This is meant for a special Execution use case.
72template <typename Event> class OneShotEventEmitter : public EventEmitterInterface<Event> {
73 public:
75 : event(event)
76 {}
77 virtual ~OneShotEventEmitter() = default;
78 void emit(Event&& event) override
79 {
80 BB_ASSERT(!has_emitted, "Event already emitted");
81 has_emitted = true;
82 this->event = event;
83 }
84
85 private:
86 bool has_emitted = false;
87 Event& event;
88};
89
90} // namespace bb::avm2::simulation
#define BB_ASSERT(expression,...)
Definition assert.hpp:80
unordered_flat_set< typename Event::Key > elements_seen
EventEmitter< Event >::Container dump_events()
void emit(Event &&event) override
const Container & get_events() const
virtual void emit(Event &&event)=0
EventEmitter< Event >::Container dump_events()
::ankerl::unordered_dense::set< Key > unordered_flat_set
Definition set.hpp:11
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
simulation::PublicDataTreeReadWriteEvent event