-
Notifications
You must be signed in to change notification settings - Fork 0
Feat/markeremitter component #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
0e69178
a70d52b
99be3bf
d221afb
2a00cc4
926523a
0a371fc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -45,6 +45,7 @@ CTestTestfile.cmake | |
| _deps/ | ||
|
|
||
| # IDEs and Editors | ||
| .clangd | ||
| .vscode/ | ||
| .idea/ | ||
| *.swp | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| #ifndef DELEGATE_HPP | ||
| #define DELEGATE_HPP | ||
|
|
||
| #include <algorithm> | ||
| #include <vector> | ||
|
|
||
| #include "events/EventListener.hpp" | ||
|
|
||
| struct Context; | ||
|
|
||
| class Delegate { | ||
| public: | ||
| Delegate() = default; | ||
| ~Delegate() = default; | ||
|
|
||
| Delegate(const Delegate&) = delete; | ||
| Delegate& operator=(const Delegate&) = delete; | ||
| Delegate(Delegate&&) = delete; | ||
| Delegate& operator=(Delegate&&) = delete; | ||
|
|
||
| void subscribe(EventListener* listener) { listeners.push_back(listener); } | ||
|
|
||
| void unsubscribe(EventListener* listener) { | ||
| listeners.erase(std::remove(listeners.begin(), listeners.end(), listener), listeners.end()); | ||
| } | ||
|
|
||
| void invoke(const Context& ctx) { | ||
| for (auto* listener : listeners) { | ||
| listener->onEventTriggered(ctx); | ||
| } | ||
| } | ||
|
|
||
| private: | ||
| // NOLINTNEXTLINE(cppcoreguidelines-non-private-member-variables-in-classes) | ||
| std::vector<EventListener*> listeners; | ||
| }; | ||
|
|
||
| #endif // DELEGATE_HPP |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| #ifndef EVENTLISTENER_HPP | ||
| #define EVENTLISTENER_HPP | ||
|
|
||
| struct Context; | ||
|
|
||
| class EventListener { | ||
| public: | ||
| EventListener() = default; | ||
| virtual ~EventListener() = default; | ||
|
|
||
| EventListener(const EventListener&) = delete; | ||
| EventListener& operator=(const EventListener&) = delete; | ||
| EventListener(EventListener&&) = delete; | ||
| EventListener& operator=(EventListener&&) = delete; | ||
|
|
||
| virtual void onEventTriggered(const Context& ctx) = 0; | ||
| }; | ||
|
|
||
| #endif // EVENTLISTENER_HPP |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| #ifndef MARKEREVENTLINK_HPP | ||
| #define MARKEREVENTLINK_HPP | ||
|
|
||
| #include <string> | ||
| #include <utility> | ||
|
|
||
| #include "data_structures/Context.hpp" | ||
| #include "events/EventListener.hpp" | ||
|
|
||
| class MarkerEventLink : public EventListener { | ||
| public: | ||
| explicit MarkerEventLink(std::string name) : eventName(std::move(name)) {} | ||
|
|
||
| void onEventTriggered(const Context& ctx) override { | ||
| if (ctx.markers != nullptr) { | ||
| ctx.markers->push_back(eventName); | ||
| } | ||
| } | ||
|
|
||
| private: | ||
| std::string eventName; | ||
| }; | ||
|
|
||
| #endif // MARKEREVENTLINK_HPP |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| #ifndef MARKEREMITTERCOMPONENT_HPP | ||
| #define MARKEREMITTERCOMPONENT_HPP | ||
|
|
||
| #include <memory> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| #include "events/MarkerEventLink.hpp" | ||
| #include "scene/components/Component.hpp" | ||
|
|
||
| class Delegate; | ||
| class Scene; | ||
|
|
||
| namespace NeuronIDE { | ||
| class Component; | ||
| } | ||
|
|
||
| class MarkerEmitterComponent : public Component { | ||
| public: | ||
| struct Binding { | ||
| std::string targetObject; | ||
| std::string targetEvent; | ||
| std::string markerName; | ||
| }; | ||
|
|
||
| MarkerEmitterComponent(const std::shared_ptr<SceneObject>& owner, | ||
| std::vector<Binding> bindings); | ||
| ~MarkerEmitterComponent() override; | ||
|
|
||
| MarkerEmitterComponent(const MarkerEmitterComponent&) = delete; | ||
| MarkerEmitterComponent& operator=(const MarkerEmitterComponent&) = delete; | ||
| MarkerEmitterComponent(MarkerEmitterComponent&&) = delete; | ||
| MarkerEmitterComponent& operator=(MarkerEmitterComponent&&) = delete; | ||
|
|
||
| void update(const Context& context) override; | ||
| void render(SDL_Renderer* renderer) override; | ||
| void onSceneReady(Scene& scene) override; | ||
|
|
||
| static std::unique_ptr<Component> createMarkerEmitter( | ||
| const NeuronIDE::Component& protoComp, const std::shared_ptr<SceneObject>& owner); | ||
|
|
||
| private: | ||
| struct ActiveSubscription { | ||
| std::unique_ptr<MarkerEventLink> link; | ||
| Delegate* delegate = nullptr; | ||
| }; | ||
|
|
||
| std::vector<Binding> bindings; | ||
| std::vector<ActiveSubscription> activeSubscriptions; | ||
| }; | ||
|
|
||
| #endif // MARKEREMITTERCOMPONENT_HPP | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| #include "scene/components/MarkerEmitterComponent.hpp" | ||
|
|
||
| #include "events/Delegate.hpp" | ||
| #include "neuronide.pb.h" | ||
| #include "scene/Scene.hpp" | ||
| #include "scene/components/ComponentRegistry.hpp" | ||
|
|
||
| MarkerEmitterComponent::MarkerEmitterComponent(const std::shared_ptr<SceneObject>& owner, | ||
| std::vector<Binding> bindings) | ||
| : Component(owner), bindings(std::move(bindings)) {} | ||
|
|
||
| MarkerEmitterComponent::~MarkerEmitterComponent() { | ||
| for (auto& sub : activeSubscriptions) { | ||
| sub.delegate->unsubscribe(sub.link.get()); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that MarkerEmitter component contains raw pointer to delegate from another component that may also be from a different object, so if that object gets destroyed before object that contains MarkerEmitter, than this line will cause heap-use-after-free. maybe we should consider making subscription bidirectional? |
||
| } | ||
| } | ||
|
|
||
| void MarkerEmitterComponent::update(const Context& context) { (void)context; } | ||
|
|
||
| void MarkerEmitterComponent::render(SDL_Renderer* renderer) { (void)renderer; } | ||
|
|
||
| void MarkerEmitterComponent::onSceneReady(Scene& scene) { | ||
| for (const auto& binding : bindings) { | ||
| auto& delegate = scene.resolveEvents(binding.targetObject, binding.targetEvent); | ||
|
|
||
| auto link = std::make_unique<MarkerEventLink>(binding.markerName); | ||
| delegate.subscribe(link.get()); | ||
|
|
||
| activeSubscriptions.push_back({std::move(link), &delegate}); | ||
| } | ||
| } | ||
|
|
||
| std::unique_ptr<Component> MarkerEmitterComponent::createMarkerEmitter( | ||
| const NeuronIDE::Component& protoComp, const std::shared_ptr<SceneObject>& owner) { | ||
| std::vector<Binding> bindings; | ||
| for (const auto& protoBinding : protoComp.marker_emitter().bindings()) { | ||
| bindings.push_back({protoBinding.target_object(), protoBinding.target_event(), | ||
| protoBinding.marker_name()}); | ||
| } | ||
| return std::make_unique<MarkerEmitterComponent>(owner, std::move(bindings)); | ||
| } | ||
|
|
||
| REGISTER_COMPONENT(NeuronIDE::Component::kMarkerEmitter, | ||
| MarkerEmitterComponent::createMarkerEmitter) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One decision worth talking about is storing "Binding"s and "ActiveSubscription"s separately. ActiveSubscription is a resolved binding, and I think we should think if the types shouldn't be combined, having uninitialized fields that get initialized onSceneReady. The downside of that approach is more complicated logic if any rebinding is to happen. I have chosen this one at first, as the clear separation of a POD type and more complicated resolved type easily mimics the config/runtime separation and allows for rebinding, refreshes etc. later