Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
AtomicTaskRegistry.cpp
Go to the documentation of this file.
1/**
2 * @file AtomicTaskRegistry.cpp
3 * @brief Implementation of AtomicTaskRegistry singleton.
4 * @author Olympe Engine
5 * @date 2026-02-22
6 *
7 * @details
8 * C++14 compliant - no C++17/20 features.
9 */
10
11#include "AtomicTaskRegistry.h"
12#include "../system/system_utils.h"
13
14namespace Olympe {
15
16// ============================================================================
17// Singleton accessor
18// ============================================================================
19
21{
22 static AtomicTaskRegistry s_instance;
23 return s_instance;
24}
25
26// ============================================================================
27// Register
28// ============================================================================
29
31{
32 m_factories[id] = std::move(factory);
33}
34
35// ============================================================================
36// Create
37// ============================================================================
38
39std::unique_ptr<IAtomicTask> AtomicTaskRegistry::Create(const std::string& id) const
40{
41 auto it = m_factories.find(id);
42 if (it != m_factories.end())
43 {
44 return it->second();
45 }
46
47 // ---------------------------------------------------------------------------
48 // Legacy ID migration: support both "Task_Foo" (legacy) and "Foo" (short).
49 //
50 // If the requested ID was not found directly, attempt the complementary form:
51 // - Short ID ("MoveToLocation") -> try legacy form ("Task_MoveToLocation")
52 // - Legacy ID ("Task_MoveToLocation") -> try short form ("MoveToLocation")
53 //
54 // This allows existing task graphs that use legacy IDs to continue working
55 // unchanged while new graphs may use the shorter editor-friendly form.
56 // ---------------------------------------------------------------------------
57 static const std::string prefix = "Task_";
58
59 if (id.size() > prefix.size()
60 && id.compare(0, prefix.size(), prefix) == 0)
61 {
62 // id starts with "Task_": try without the prefix (short form)
63 std::string shortId = id.substr(prefix.size());
64 auto it2 = m_factories.find(shortId);
65 if (it2 != m_factories.end())
66 {
67 SYSTEM_LOG << "[AtomicTaskRegistry] Legacy ID '" << id
68 << "' resolved to short ID '" << shortId << "'\n";
69 return it2->second();
70 }
71 }
72 else
73 {
74 // id does not start with "Task_": try adding the prefix (legacy form)
75 std::string legacyId = prefix + id;
76 auto it2 = m_factories.find(legacyId);
77 if (it2 != m_factories.end())
78 {
79 SYSTEM_LOG << "[AtomicTaskRegistry] Short ID '" << id
80 << "' resolved to legacy ID '" << legacyId << "'\n";
81 return it2->second();
82 }
83 }
84
85 return nullptr;
86}
87
88// ============================================================================
89// IsRegistered
90// ============================================================================
91
92bool AtomicTaskRegistry::IsRegistered(const std::string& id) const
93{
94 return m_factories.find(id) != m_factories.end();
95}
96
97// ============================================================================
98// GetAllTaskIDs
99// ============================================================================
100
101std::vector<std::string> AtomicTaskRegistry::GetAllTaskIDs() const
102{
103 std::vector<std::string> ids;
104 ids.reserve(m_factories.size());
105 for (auto it = m_factories.begin(); it != m_factories.end(); ++it)
106 {
107 ids.push_back(it->first);
108 }
109 return ids;
110}
111
112} // namespace Olympe
Singleton registry for atomic task factories.
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
Singleton registry mapping task IDs to factory functions.
std::vector< std::string > GetAllTaskIDs() const
Returns a vector of all registered task IDs.
bool IsRegistered(const std::string &id) const
Returns true if a factory is registered for id.
std::unique_ptr< IAtomicTask > Create(const std::string &id) const
Creates a new instance of the task identified by id.
static AtomicTaskRegistry & Get()
Returns the singleton instance.
std::unordered_map< std::string, FactoryFn > m_factories
void Register(const std::string &id, FactoryFn factory)
Registers a factory function under the given task ID.
std::function< std::unique_ptr< IAtomicTask >()> FactoryFn
Factory function type: returns a heap-allocated IAtomicTask.
< Provides AssetID and INVALID_ASSET_ID
#define SYSTEM_LOG