Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
BlackboardVariablePresetRegistry.h
Go to the documentation of this file.
1/**
2 * @file BlackboardVariablePresetRegistry.h
3 * @brief Singleton registry for blackboard variable presets loaded from JSON config.
4 * @author Olympe Engine
5 * @date 2026-03-15
6 *
7 * @details
8 * BlackboardVariablePresetRegistry loads the pool of available blackboard
9 * variable presets from `Assets/Config/BlackboardVariablePresets.json`.
10 * It exposes O(1) lookup by name, category filtering, and metadata access.
11 *
12 * Use the singleton via Instance() at editor startup. Call LoadFromFile()
13 * once to populate. The registry is read-only after loading.
14 *
15 * C++14 compliant — no std::optional, structured bindings, std::filesystem.
16 */
17
18#pragma once
19
20#include <string>
21#include <vector>
22#include <unordered_map>
23#include <mutex>
24
25namespace Olympe {
26
27/**
28 * @struct BlackboardVariablePreset
29 * @brief Metadata for a single blackboard variable preset entry.
30 */
32 std::string name; ///< Variable key (e.g. "targetDistance")
33 std::string type; ///< Type string as declared in JSON (e.g. "Float")
34 std::string description; ///< Human-readable description
35 std::string category; ///< Category label (e.g. "Targeting", "Movement")
36 std::string defaultValue;///< Default value as string (e.g. "9999.0", "false")
37};
38
39/**
40 * @class BlackboardVariablePresetRegistry
41 * @brief Singleton registry for global blackboard variable presets.
42 *
43 * @details
44 * Loaded from `Assets/Config/BlackboardVariablePresets.json` at editor startup.
45 * Provides O(1) lookup by name via internal index map.
46 * Category queries are O(n) on the preset list but cached on first access.
47 *
48 * Thread-safety: read-only after LoadFromFile() — no locking required.
49 */
51public:
52
53 /**
54 * @brief Returns the singleton instance.
55 * @return Reference to the singleton.
56 */
58
59 // Non-copyable, non-movable (singleton)
62
63 /**
64 * @brief Loads presets from a JSON file.
65 *
66 * Clears any previously loaded data before loading.
67 * On parse error, logs and returns false; registry remains empty.
68 *
69 * @param filePath Absolute or relative path to the JSON preset file.
70 * @return true on success, false on file/parse error.
71 */
72 bool LoadFromFile(const std::string& filePath);
73
74 /**
75 * @brief Returns all loaded presets, sorted by name.
76 *
77 * Thread-safe: protected by an internal mutex so that concurrent calls
78 * from a FileWatcher thread and the Save path cannot race.
79 * @return Const reference to the internal preset list.
80 */
81 const std::vector<BlackboardVariablePreset>& GetAllPresets() const;
82
83 /**
84 * @brief Returns a pointer to the preset with the given name, or nullptr.
85 * @param name Variable name to look up (case-sensitive).
86 * @return Pointer to preset if found, nullptr otherwise.
87 */
88 const BlackboardVariablePreset* GetPreset(const std::string& name) const;
89
90 /**
91 * @brief Returns all presets belonging to a given category.
92 * @param category Category label to filter by (case-sensitive).
93 * @return Vector of matching presets (may be empty).
94 */
95 std::vector<BlackboardVariablePreset> GetByCategory(const std::string& category) const;
96
97 /**
98 * @brief Returns all unique category names present in the registry.
99 * @return Sorted vector of category strings.
100 */
101 std::vector<std::string> GetAllCategories() const;
102
103 /**
104 * @brief Returns true if a preset with the given name exists.
105 * @param name Variable name to test.
106 */
107 bool HasPreset(const std::string& name) const;
108
109 /**
110 * @brief Returns the number of loaded presets.
111 */
112 size_t GetCount() const;
113
114 /**
115 * @brief Clears all loaded presets and resets the registry.
116 */
117 void Clear();
118
119private:
120
122
123 mutable std::mutex m_mutex; ///< Guards m_presets during concurrent access
124 std::vector<BlackboardVariablePreset> m_presets;
125 std::unordered_map<std::string, size_t> m_nameIndex; ///< name -> index in m_presets
126};
127
128} // namespace Olympe
Singleton registry for global blackboard variable presets.
const BlackboardVariablePreset * GetPreset(const std::string &name) const
Returns a pointer to the preset with the given name, or nullptr.
std::mutex m_mutex
Guards m_presets during concurrent access.
std::vector< BlackboardVariablePreset > m_presets
size_t GetCount() const
Returns the number of loaded presets.
const std::vector< BlackboardVariablePreset > & GetAllPresets() const
Returns all loaded presets, sorted by name.
BlackboardVariablePresetRegistry(const BlackboardVariablePresetRegistry &)=delete
static BlackboardVariablePresetRegistry & Instance()
Returns the singleton instance.
std::unordered_map< std::string, size_t > m_nameIndex
name -> index in m_presets
bool LoadFromFile(const std::string &filePath)
Loads presets from a JSON file.
std::vector< BlackboardVariablePreset > GetByCategory(const std::string &category) const
Returns all presets belonging to a given category.
BlackboardVariablePresetRegistry & operator=(const BlackboardVariablePresetRegistry &)=delete
bool HasPreset(const std::string &name) const
Returns true if a preset with the given name exists.
void Clear()
Clears all loaded presets and resets the registry.
std::vector< std::string > GetAllCategories() const
Returns all unique category names present in the registry.
< Provides AssetID and INVALID_ASSET_ID
Metadata for a single blackboard variable preset entry.
std::string category
Category label (e.g. "Targeting", "Movement")
std::string type
Type string as declared in JSON (e.g. "Float")
std::string name
Variable key (e.g. "targetDistance")
std::string description
Human-readable description.
std::string defaultValue
Default value as string (e.g. "9999.0", "false")