-
Notifications
You must be signed in to change notification settings - Fork 0
refactor: remove switch case from Parser::buildComponent() #14
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
Merged
MichalSzandar
merged 6 commits into
feat/parser-implementation
from
refactor/parser-refactor
May 25, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
62df364
refactor: replace switch case in Parser::buildComponent with Componen…
MichalSzandar 9f94b3c
fix: format code
MichalSzandar 1fb818e
refactor: move SceneObject functions implementation to seperate .cpp …
MichalSzandar 3e3b3e3
fix: format code
MichalSzandar bb4acfe
refactor: throw error if component creator function has already been …
MichalSzandar 22d00da
feat: create macro for registering components
MichalSzandar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| #ifndef COMPONENTREGISTRY_HPP | ||
| #define COMPONENTREGISTRY_HPP | ||
|
|
||
| #include <functional> | ||
| #include <memory> | ||
| #include <unordered_map> | ||
|
|
||
| class Component; | ||
| namespace NeuronIDE { | ||
| class Component; | ||
| } | ||
|
|
||
| using ComponentCreatorFunc = std::function<std::unique_ptr<Component>(const NeuronIDE::Component&)>; | ||
|
|
||
| class ComponentRegistry { | ||
| public: | ||
| ComponentRegistry(const ComponentRegistry&) = delete; | ||
| ComponentRegistry& operator=(const ComponentRegistry&) = delete; | ||
|
|
||
| ComponentRegistry(ComponentRegistry&&) = delete; | ||
| ComponentRegistry& operator=(ComponentRegistry&&) = delete; | ||
|
|
||
| static ComponentRegistry& instance() { | ||
| static ComponentRegistry instance; | ||
| return instance; | ||
| } | ||
|
|
||
| void registerCreator(int typeId, ComponentCreatorFunc creator); | ||
|
|
||
| std::unique_ptr<Component> build(const NeuronIDE::Component& protoComp); | ||
|
MichalSzandar marked this conversation as resolved.
|
||
|
|
||
| private: | ||
| ComponentRegistry() = default; | ||
|
|
||
| std::unordered_map<int, ComponentCreatorFunc> creators; | ||
| }; | ||
|
|
||
| #define COMPONENT_REGISTRATION_CONCAT_IMPL(x, y) x##y | ||
| #define COMPONENT_REGISTRATION_CONCAT(x, y) COMPONENT_REGISTRATION_CONCAT_IMPL(x, y) | ||
|
|
||
| #define REGISTER_COMPONENT(typeId, creatorFunc) \ | ||
| namespace { \ | ||
| struct COMPONENT_REGISTRATION_CONCAT(ComponentRegistrar_, __LINE__) { \ | ||
| COMPONENT_REGISTRATION_CONCAT(ComponentRegistrar_, __LINE__)() { \ | ||
| ComponentRegistry::instance().registerCreator(static_cast<int>(typeId), creatorFunc);\ | ||
| } \ | ||
| }; \ | ||
| static COMPONENT_REGISTRATION_CONCAT(ComponentRegistrar_, __LINE__) \ | ||
| COMPONENT_REGISTRATION_CONCAT(global_registrar_, __LINE__); \ | ||
| } | ||
|
|
||
| #endif // COMPONENTREGISTRY_HPP | ||
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| #include "scene/SceneObject.hpp" | ||
|
|
||
| #include <utility> | ||
|
|
||
| #include "scene/components/Component.hpp" | ||
|
|
||
| SceneObject::SceneObject(std::string n, bool visible) : name(std::move(n)), isVisible(visible) { | ||
| std::cout << " [SceneObject] Utworzono obiekt: " << name << "\n"; | ||
| } | ||
|
|
||
| void SceneObject::setTransform(Transform t) { transform = t; } | ||
|
|
||
| void SceneObject::addComponent(std::unique_ptr<Component> comp) { | ||
| components.push_back(std::move(comp)); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| #include "scene/components/BlinkComponent.hpp" | ||
|
|
||
| #include "neuronide.pb.h" | ||
| #include "scene/components/ComponentRegistry.hpp" | ||
|
|
||
| void BlinkComponent::setFrequency(double freq) { blinkFrequencyHz = freq; } | ||
|
|
||
| std::unique_ptr<Component> BlinkComponent::createBlinker(const NeuronIDE::Component& protoComp) { | ||
| return std::make_unique<BlinkComponent>(protoComp.blinker().blink_frequency_hz()); | ||
| } | ||
|
|
||
| REGISTER_COMPONENT(NeuronIDE::Component::kBlinker, BlinkComponent::createBlinker) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| #include "scene/components/ComponentRegistry.hpp" | ||
|
|
||
| #include <stdexcept> | ||
| #include <utility> | ||
|
|
||
| #include "neuronide.pb.h" | ||
| #include "scene/components/Component.hpp" | ||
|
|
||
| void ComponentRegistry::registerCreator(int typeId, ComponentCreatorFunc creator) { | ||
| if (creators.find(typeId) != creators.end()) { | ||
| throw std::runtime_error("Creator for this typeId is already registered."); | ||
| } | ||
| creators[typeId] = std::move(creator); | ||
| } | ||
|
|
||
| std::unique_ptr<Component> ComponentRegistry::build(const NeuronIDE::Component& protoComp) { | ||
| auto activeCase = protoComp.component_type_case(); | ||
|
|
||
| int typeId = static_cast<int>(activeCase); | ||
|
|
||
| auto it = creators.find(typeId); | ||
| if (it != creators.end()) { | ||
| return it->second(protoComp); | ||
| } | ||
|
|
||
| return nullptr; | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.