Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
AsyncJSONLoader.h
Go to the documentation of this file.
1/**
2 * @file AsyncJSONLoader.h
3 * @brief Asynchronous JSON file loader (Phase 7).
4 * @author Olympe Engine
5 * @date 2026-03-10
6 *
7 * @details
8 * AsyncJSONLoader launches a background thread (via std::future) to parse a
9 * JSON file without blocking the editor UI thread. Poll IsReady() / HasFailed()
10 * each frame; when ready, call GetResult() to obtain the parsed JSON object.
11 *
12 * C++14 compliant — no std::optional, structured bindings, std::filesystem.
13 */
14
15#pragma once
16
17#include <string>
18#include <future>
19
20#include "../../third_party/nlohmann/json.hpp"
21
22namespace Olympe {
23
24// ============================================================================
25// AsyncLoadState
26// ============================================================================
27
28/**
29 * @enum AsyncLoadState
30 * @brief Lifecycle state of an AsyncJSONLoader.
31 */
32enum class AsyncLoadState {
33 Idle, ///< No load has been requested
34 Loading, ///< Background thread is running
35 Ready, ///< Result is available — call GetResult()
36 Failed ///< Load or parse failed — call GetLastError()
37};
38
39// ============================================================================
40// AsyncJSONLoader
41// ============================================================================
42
43/**
44 * @class AsyncJSONLoader
45 * @brief Loads and parses a JSON file on a background thread.
46 *
47 * Typical usage:
48 * @code
49 * AsyncJSONLoader loader;
50 * loader.LoadAsync("data/graph.json");
51 * // ... each frame ...
52 * if (loader.IsReady())
53 * {
54 * nlohmann::json j = loader.GetResult();
55 * loader.Reset();
56 * }
57 * @endcode
58 */
60public:
61
63
64 // -----------------------------------------------------------------------
65 // Control
66 // -----------------------------------------------------------------------
67
68 /**
69 * @brief Starts loading @p path on a background thread.
70 * Any previous result is discarded.
71 */
72 void LoadAsync(const std::string& path);
73
74 /**
75 * @brief Returns the current state of the loader.
76 * Transitions: Idle -> Loading -> Ready | Failed.
77 */
79
80 /**
81 * @brief Returns true if the result is available (state == Ready).
82 */
83 bool IsReady() const;
84
85 /**
86 * @brief Returns true if the load failed (state == Failed).
87 */
88 bool HasFailed() const;
89
90 /**
91 * @brief Returns the parsed JSON if ready; returns an empty object otherwise.
92 *
93 * @note Calling GetResult() does NOT reset the loader. Call Reset()
94 * explicitly when you are done with the result.
95 */
97
98 /**
99 * @brief Resets state to Idle and clears the result / error.
100 */
101 void Reset();
102
103 /**
104 * @brief Returns the last error message (non-empty when state == Failed).
105 */
106 const std::string& GetLastError() const;
107
108private:
109
110 /**
111 * @brief Opens, reads, and parses the JSON file; called on the background thread.
112 * @return Parsed JSON object; throws on error.
113 */
114 static nlohmann::json LoadFile(const std::string& path);
115
116 // Mutable because GetState()/IsReady() need to poll the future.
118 std::string m_LastError;
120 mutable std::future<nlohmann::json> m_Future;
121};
122
123} // namespace Olympe
Loads and parses a JSON file on a background thread.
AsyncLoadState GetState() const
Returns the current state of the loader.
const std::string & GetLastError() const
Returns the last error message (non-empty when state == Failed).
std::future< nlohmann::json > m_Future
bool HasFailed() const
Returns true if the load failed (state == Failed).
nlohmann::json GetResult()
Returns the parsed JSON if ready; returns an empty object otherwise.
void Reset()
Resets state to Idle and clears the result / error.
void LoadAsync(const std::string &path)
Starts loading path on a background thread.
static nlohmann::json LoadFile(const std::string &path)
Opens, reads, and parses the JSON file; called on the background thread.
bool IsReady() const
Returns true if the result is available (state == Ready).
< Provides AssetID and INVALID_ASSET_ID
AsyncLoadState
Lifecycle state of an AsyncJSONLoader.
@ Loading
Background thread is running.
@ Failed
Load or parse failed — call GetLastError()
@ Idle
No load has been requested.
@ Ready
Result is available — call GetResult()
nlohmann::json json