Skip to content

Commit

Permalink
fix my vector events nonsense
Browse files Browse the repository at this point in the history
  • Loading branch information
fcarreiro committed Jan 8, 2025
1 parent 38b7a3e commit dcaa1a5
Showing 1 changed file with 32 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
#pragma once

#include <list>
#include <vector>

namespace bb::avm2::simulation {

template <typename Event> class EventEmitterInterface {
public:
// TODO: DO NOT DO THIS. We shouldn't prescribe the base container.
using Container = std::vector<Event>;

virtual ~EventEmitterInterface() = default;
virtual Event& emit(Event&& event) = 0;
// Pushes the event to the event container.
virtual void emit(Event&& event) = 0;
// Transfers ownership of the events to the caller (clears the internal container).
virtual Container dump_events() = 0;
};

template <typename Event> class EventEmitter : public EventEmitterInterface<Event> {
public:
using Container = EventEmitterInterface<Event>::Container;
using Container = std::vector<Event>;

EventEmitter() = default;
virtual ~EventEmitter() = default;
Event& emit(Event&& event) override
{
events.push_back(std::move(event));
return events.back();
};
void emit(Event&& event) override { events.push_back(std::move(event)); };

const Container& get_events() const { return events; }
Container dump_events() override { return std::move(events); }
Expand All @@ -33,15 +30,37 @@ template <typename Event> class EventEmitter : public EventEmitterInterface<Even
Container events;
};

// TODO: one for all?
// This is an event emmiter that offers stable references to the events.
// It lets you access the last event that was emitted.
// Note: this is currently unused but it might be needed for the execution trace (calls).
// Reconsider its existence in a while.
template <typename Event> class StableEventEmitter : public EventEmitterInterface<Event> {
public:
using Container = std::list<Event>;

virtual ~StableEventEmitter() = default;
void emit(Event&& event) override { events.push_back(std::move(event)); };
Event& last() { return events.back(); }

const Container& get_events() const { return events; }
std::vector<Event> dump_events() override
{
std::vector<Event> result(events.begin(), events.end());
events.clear();
return result;
}

private:
Container events;
};

template <typename Event> class NoopEventEmitter : public EventEmitterInterface<Event> {
public:
using Container = EventEmitterInterface<Event>::Container;
using Container = std::vector<Event>;

NoopEventEmitter() = default;
virtual ~NoopEventEmitter() = default;

Event& emit(Event&& event) override { return event; };
void emit(Event&&) override{};
Container dump_events() override { return {}; }
};

Expand Down

0 comments on commit dcaa1a5

Please sign in to comment.