Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
AsyncJSONLoader.cpp
Go to the documentation of this file.
1/**
2 * @file AsyncJSONLoader.cpp
3 * @brief Asynchronous JSON file loader implementation (Phase 7).
4 * @author Olympe Engine
5 * @date 2026-03-10
6 */
7
8#include "AsyncJSONLoader.h"
9
10#include <fstream>
11#include <stdexcept>
12
13#include "../../system/system_utils.h"
14
15namespace Olympe {
16
17// ============================================================================
18// Construction
19// ============================================================================
20
25
26// ============================================================================
27// LoadAsync
28// ============================================================================
29
30void AsyncJSONLoader::LoadAsync(const std::string& path)
31{
32 // If a previous load is still in flight, detach it (fire-and-forget).
33 // The background thread will finish on its own; we just discard the result.
34 if (m_Future.valid())
35 m_Future.wait();
36
38 m_LastError.clear();
40
41 m_Future = std::async(std::launch::async,
43 path);
44}
45
46// ============================================================================
47// GetState
48// ============================================================================
49
51{
53 {
54 if (!m_Future.valid())
55 {
57 return m_State;
58 }
59
60 // Poll without blocking.
61 std::future_status status =
62 m_Future.wait_for(std::chrono::seconds(0));
63
64 if (status == std::future_status::ready)
65 {
66 // Retrieve result — const_cast needed because future::get() is not const.
67 AsyncJSONLoader* self = const_cast<AsyncJSONLoader*>(this);
68 try
69 {
70 self->m_Result = m_Future.get();
71 self->m_State = AsyncLoadState::Ready;
72 }
73 catch (const std::exception& e)
74 {
75 self->m_LastError = e.what();
77 SYSTEM_LOG << "[AsyncJSONLoader] Load failed: " << e.what() << std::endl;
78 }
79 catch (...)
80 {
81 self->m_LastError = "Unknown error during JSON load.";
83 SYSTEM_LOG << "[AsyncJSONLoader] Load failed with unknown error." << std::endl;
84 }
85 }
86 }
87
88 return m_State;
89}
90
91// ============================================================================
92// Convenience state queries
93// ============================================================================
94
96{
98}
99
101{
103}
104
105// ============================================================================
106// GetResult
107// ============================================================================
108
115
116// ============================================================================
117// Reset
118// ============================================================================
119
121{
122 if (m_Future.valid())
123 m_Future.wait();
124
126 m_LastError.clear();
128}
129
130// ============================================================================
131// GetLastError
132// ============================================================================
133
134const std::string& AsyncJSONLoader::GetLastError() const
135{
136 return m_LastError;
137}
138
139// ============================================================================
140// LoadFile (background thread)
141// ============================================================================
142
144{
145 std::ifstream file(path);
146 if (!file.is_open())
147 throw std::runtime_error("Cannot open file: " + path);
148
150 file >> j;
151 return j;
152}
153
154} // namespace Olympe
Asynchronous JSON file loader (Phase 7).
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
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
#define SYSTEM_LOG