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
7
8namespace bb::avm2::simulation {
9
10template <typename Event> class EventEmitterInterface {
11 public:
12 using Container = std::vector<Event>;
13
14 virtual ~EventEmitterInterface() = default;
15 // Pushes the event to the event container.
16 virtual void emit(Event&& event) = 0;
17};
18
19template <typename Event> class EventEmitter : public EventEmitterInterface<Event> {
20 public:
21 using Container = std::vector<Event>;
22
23 virtual ~EventEmitter() = default;
24 void emit(Event&& event) override { events.push_back(std::move(event)); };
25
26 const Container& get_events() const { return events; }
27 // Transfers ownership of the events to the caller (clears the internal container).
29
30 private:
32};
33
34// This is an EventEmitter that eagerly deduplicates events based on a provided key.
35template <typename Event> class DeduplicatingEventEmitter : public EventEmitter<Event> {
36 public:
37 virtual ~DeduplicatingEventEmitter() = default;
38
39 void emit(Event&& event) override
40 {
41 typename Event::Key key = event.get_key();
42 if (!elements_seen.contains(key)) {
43 elements_seen.insert(key);
45 }
46 };
47 // Transfers ownership of the events to the caller (clears the internal container).
53
54 private:
56};
57
58template <typename Event> class NoopEventEmitter : public EventEmitterInterface<Event> {
59 public:
60 using Container = std::vector<Event>;
61
62 virtual ~NoopEventEmitter() = default;
63
64 void emit(Event&&) override {};
65 // TODO: Get rid of this.
67};
68
69// This is an event emitter which only emits events once (it actually just _sets_ an event).
70// This is meant for a special Execution use case.
71template <typename Event> class OneShotEventEmitter : public EventEmitterInterface<Event> {
72 public:
74 : event(event)
75 {}
76 virtual ~OneShotEventEmitter() = default;
77 void emit(Event&& event) override
78 {
79 assert(!has_emitted);
80 has_emitted = true;
81 this->event = event;
82 }
83
84 private:
85 bool has_emitted = false;
86 Event& event;
87};
88
89} // namespace bb::avm2::simulation
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