Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
ConditionPresetRegistry.h
Go to the documentation of this file.
1/**
2 * @file ConditionPresetRegistry.h
3 * @brief Global registry for ConditionPreset objects, with CRUD, persistence, and validation.
4 * @author Olympe Engine
5 * @date 2026-03-16
6 *
7 * @details
8 * ConditionPresetRegistry owns all ConditionPreset instances for a project.
9 * It is persisted to and loaded from
10 * `./Blueprints/Presets/condition_presets.json`.
11 *
12 * Typical usage:
13 * @code
14 * ConditionPresetRegistry registry;
15 * registry.Load("./Blueprints/Presets/condition_presets.json");
16 *
17 * ConditionPreset p;
18 * p.name = "Condition #1";
19 * p.left = Operand::CreateVariable("mHealth");
20 * p.op = ComparisonOp::LessEqual;
21 * p.right = Operand::CreateConst(2.0);
22 * std::string id = registry.CreatePreset(p);
23 *
24 * registry.Save("./Blueprints/Presets/condition_presets.json");
25 * @endcode
26 *
27 * C++14 compliant — no std::optional, structured bindings, std::filesystem.
28 */
29
30#pragma once
31
32#include <map>
33#include <string>
34#include <vector>
35#include "ConditionPreset.h"
36#include "../../third_party/nlohmann/json.hpp"
37
38namespace Olympe {
39
40/**
41 * @class ConditionPresetRegistry
42 * @brief Manages the global pool of ConditionPreset objects.
43 *
44 * @details
45 * Presets are stored in insertion order using a std::map keyed by UUID.
46 * An ordered list (m_order) preserves display order in the UI.
47 */
49public:
50
52
53 // Non-copyable (owns presets)
56
57 // -----------------------------------------------------------------------
58 // CRUD
59 // -----------------------------------------------------------------------
60
61 /**
62 * @brief Adds a preset to the registry.
63 *
64 * If `preset.id` is empty a new UUID is generated. If the ID already
65 * exists the operation is a no-op and the existing ID is returned.
66 *
67 * @param preset Preset to add (id may be empty — will be assigned).
68 * @return The UUID of the stored preset.
69 */
70 std::string CreatePreset(const ConditionPreset& preset);
71
72 /**
73 * @brief Returns a mutable pointer to the preset, or nullptr if not found.
74 * @param id Preset UUID.
75 */
76 ConditionPreset* GetPreset(const std::string& id);
77
78 /**
79 * @brief Returns a const pointer to the preset, or nullptr if not found.
80 * @param id Preset UUID.
81 */
82 const ConditionPreset* GetPreset(const std::string& id) const;
83
84 /**
85 * @brief Replaces the data of an existing preset (id must already exist).
86 *
87 * If the ID is not found, logs an error and returns without modifying state.
88 * The preset's id field in `updated` is forced to match `id`.
89 *
90 * @param id Preset UUID to update.
91 * @param updated New preset data.
92 */
93 void UpdatePreset(const std::string& id, const ConditionPreset& updated);
94
95 /**
96 * @brief Removes a preset from the registry.
97 *
98 * If the ID is not found, the call is a no-op.
99 * @param id Preset UUID to remove.
100 */
101 void DeletePreset(const std::string& id);
102
103 /**
104 * @brief Creates an independent copy of an existing preset with a new UUID.
105 *
106 * The copy's name is suffixed with " (Copy)".
107 * Returns the new UUID, or an empty string if the source ID is not found.
108 *
109 * @param id Source preset UUID.
110 * @return UUID of the newly created duplicate.
111 */
112 std::string DuplicatePreset(const std::string& id);
113
114 // -----------------------------------------------------------------------
115 // Query
116 // -----------------------------------------------------------------------
117
118 /**
119 * @brief Returns all preset UUIDs in display order.
120 */
121 std::vector<std::string> GetAllPresetIDs() const;
122
123 /**
124 * @brief Returns the total number of presets in the registry.
125 */
126 size_t GetPresetCount() const;
127
128 /**
129 * @brief Returns UUIDs of all presets whose name contains a substring.
130 *
131 * Case-sensitive search.
132 * @param substring Search string.
133 */
134 std::vector<std::string> FindPresetsByName(const std::string& substring) const;
135
136 /**
137 * @brief Returns all presets whose name contains the filter substring.
138 *
139 * If filter is empty, returns all presets in display order.
140 * Case-sensitive search.
141 * @param filter Search string (empty = match all).
142 * @return Vector of matching ConditionPreset objects.
143 */
144 std::vector<ConditionPreset> GetFilteredPresets(const std::string& filter) const;
145
146 // -----------------------------------------------------------------------
147 // Validation
148 // -----------------------------------------------------------------------
149
150 /**
151 * @brief Returns true when a preset with the given UUID exists.
152 */
153 bool ValidatePresetID(const std::string& id) const;
154
155 /**
156 * @brief Returns all error messages accumulated since the last Clear().
157 */
158 std::vector<std::string> GetAllErrors() const;
159
160 /**
161 * @brief Clears all presets and resets error state.
162 */
163 void Clear();
164
165 // -----------------------------------------------------------------------
166 // Persistence
167 // -----------------------------------------------------------------------
168
169 /**
170 * @brief Loads presets from a JSON file (clears existing data first).
171 *
172 * Expected format:
173 * @code{.json}
174 * {
175 * "version": 1,
176 * "presets": [ ... ]
177 * }
178 * @endcode
179 *
180 * @param filepath Path to the JSON file.
181 * @return true on success, false on file/parse error.
182 */
183 bool Load(const std::string& filepath);
184
185 /**
186 * @brief Loads presets from a vector of ConditionPreset objects (clears existing data first).
187 *
188 * Phase 24 — Supports graph-embedded presets. This method is used when presets
189 * are deserialized from a blueprint JSON instead of from an external file.
190 * Each preset's UUID is preserved from the preset object itself.
191 *
192 * @param presets Vector of ConditionPreset objects to load.
193 */
194 void LoadFromPresetList(const std::vector<ConditionPreset>& presets);
195
196 /**
197 * @brief Saves all presets to a JSON file.
198 *
199 * Creates the file if it does not exist; overwrites if it does.
200 * Parent directories must already exist.
201 *
202 * @param filepath Destination path.
203 * @return true on success, false on write error.
204 */
205 bool Save(const std::string& filepath) const;
206
207private:
208
209 /// @brief Generates a new UUID suitable for a preset ID.
210 static std::string GenerateID();
211
212 std::map<std::string, ConditionPreset> m_presets; ///< UUID -> ConditionPreset
213 std::vector<std::string> m_order; ///< UUIDs in insertion order
214 mutable std::vector<std::string> m_errors; ///< Accumulated error messages
215};
216
217} // namespace Olympe
Defines ConditionPreset — a reusable, globally-stored condition expression.
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
Manages the global pool of ConditionPreset objects.
void LoadFromPresetList(const std::vector< ConditionPreset > &presets)
Loads presets from a vector of ConditionPreset objects (clears existing data first).
size_t GetPresetCount() const
Returns the total number of presets in the registry.
void DeletePreset(const std::string &id)
Removes a preset from the registry.
static std::string GenerateID()
Generates a new UUID suitable for a preset ID.
std::map< std::string, ConditionPreset > m_presets
UUID -> ConditionPreset.
std::vector< std::string > m_order
UUIDs in insertion order.
void UpdatePreset(const std::string &id, const ConditionPreset &updated)
Replaces the data of an existing preset (id must already exist).
void Clear()
Clears all presets and resets error state.
ConditionPresetRegistry(const ConditionPresetRegistry &)=delete
std::vector< std::string > GetAllPresetIDs() const
Returns all preset UUIDs in display order.
bool ValidatePresetID(const std::string &id) const
Returns true when a preset with the given UUID exists.
std::vector< std::string > m_errors
Accumulated error messages.
std::string CreatePreset(const ConditionPreset &preset)
Adds a preset to the registry.
std::string DuplicatePreset(const std::string &id)
Creates an independent copy of an existing preset with a new UUID.
bool Save(const std::string &filepath) const
Saves all presets to a JSON file.
std::vector< ConditionPreset > GetFilteredPresets(const std::string &filter) const
Returns all presets whose name contains the filter substring.
std::vector< std::string > GetAllErrors() const
Returns all error messages accumulated since the last Clear().
bool Load(const std::string &filepath)
Loads presets from a JSON file (clears existing data first).
ConditionPresetRegistry & operator=(const ConditionPresetRegistry &)=delete
std::vector< std::string > FindPresetsByName(const std::string &substring) const
Returns UUIDs of all presets whose name contains a substring.
ConditionPreset * GetPreset(const std::string &id)
Returns a mutable pointer to the preset, or nullptr if not found.
< Provides AssetID and INVALID_ASSET_ID
A globally-stored, reusable condition expression.