Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
Public Member Functions | Static Private Member Functions | Private Attributes | List of all members
Olympe::AsyncJSONLoader Class Reference

Loads and parses a JSON file on a background thread. More...

#include <AsyncJSONLoader.h>

+ Collaboration diagram for Olympe::AsyncJSONLoader:

Public Member Functions

 AsyncJSONLoader ()
 
void LoadAsync (const std::string &path)
 Starts loading path on a background thread.
 
AsyncLoadState GetState () const
 Returns the current state of the loader.
 
bool IsReady () const
 Returns true if the result is available (state == Ready).
 
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.
 
const std::string & GetLastError () const
 Returns the last error message (non-empty when state == Failed).
 

Static Private Member Functions

static nlohmann::json LoadFile (const std::string &path)
 Opens, reads, and parses the JSON file; called on the background thread.
 

Private Attributes

AsyncLoadState m_State
 
std::string m_LastError
 
nlohmann::json m_Result
 
std::future< nlohmann::jsonm_Future
 

Detailed Description

Loads and parses a JSON file on a background thread.

Typical usage:

loader.LoadAsync("data/graph.json");
// ... each frame ...
if (loader.IsReady())
{
nlohmann::json j = loader.GetResult();
loader.Reset();
}
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
Loads and parses a JSON file on a background thread.
void LoadAsync(const std::string &path)
Starts loading path on a background thread.
nlohmann::json json

Definition at line 59 of file AsyncJSONLoader.h.

Constructor & Destructor Documentation

◆ AsyncJSONLoader()

Olympe::AsyncJSONLoader::AsyncJSONLoader ( )

Definition at line 21 of file AsyncJSONLoader.cpp.

Member Function Documentation

◆ GetLastError()

const std::string & Olympe::AsyncJSONLoader::GetLastError ( ) const

Returns the last error message (non-empty when state == Failed).

Definition at line 134 of file AsyncJSONLoader.cpp.

References m_LastError.

◆ GetResult()

nlohmann::json Olympe::AsyncJSONLoader::GetResult ( )

Returns the parsed JSON if ready; returns an empty object otherwise.

Note
Calling GetResult() does NOT reset the loader. Call Reset() explicitly when you are done with the result.

Definition at line 109 of file AsyncJSONLoader.cpp.

References GetState(), m_Result, and Olympe::Ready.

+ Here is the call graph for this function:

◆ GetState()

AsyncLoadState Olympe::AsyncJSONLoader::GetState ( ) const

Returns the current state of the loader.

Transitions: Idle -> Loading -> Ready | Failed.

Definition at line 50 of file AsyncJSONLoader.cpp.

References Olympe::Failed, GetComponentTypeID_Static(), Olympe::Loading, m_Future, m_Result, m_State, Olympe::Ready, and SYSTEM_LOG.

Referenced by GetResult(), HasFailed(), and IsReady().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ HasFailed()

bool Olympe::AsyncJSONLoader::HasFailed ( ) const

Returns true if the load failed (state == Failed).

Definition at line 100 of file AsyncJSONLoader.cpp.

References Olympe::Failed, and GetState().

+ Here is the call graph for this function:

◆ IsReady()

bool Olympe::AsyncJSONLoader::IsReady ( ) const

Returns true if the result is available (state == Ready).

Definition at line 95 of file AsyncJSONLoader.cpp.

References GetState(), and Olympe::Ready.

+ Here is the call graph for this function:

◆ LoadAsync()

void Olympe::AsyncJSONLoader::LoadAsync ( const std::string &  path)

Starts loading path on a background thread.

Any previous result is discarded.

Definition at line 30 of file AsyncJSONLoader.cpp.

References LoadFile(), Olympe::Loading, m_Future, m_LastError, m_Result, and m_State.

+ Here is the call graph for this function:

◆ LoadFile()

nlohmann::json Olympe::AsyncJSONLoader::LoadFile ( const std::string &  path)
staticprivate

Opens, reads, and parses the JSON file; called on the background thread.

Returns
Parsed JSON object; throws on error.

Definition at line 143 of file AsyncJSONLoader.cpp.

References GetComponentTypeID_Static().

Referenced by LoadAsync().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Reset()

void Olympe::AsyncJSONLoader::Reset ( )

Resets state to Idle and clears the result / error.

Definition at line 120 of file AsyncJSONLoader.cpp.

References Olympe::Idle, m_Future, m_LastError, m_Result, and m_State.

Member Data Documentation

◆ m_Future

std::future<nlohmann::json> Olympe::AsyncJSONLoader::m_Future
mutableprivate

Definition at line 120 of file AsyncJSONLoader.h.

Referenced by GetState(), LoadAsync(), and Reset().

◆ m_LastError

std::string Olympe::AsyncJSONLoader::m_LastError
private

Definition at line 118 of file AsyncJSONLoader.h.

Referenced by GetLastError(), LoadAsync(), and Reset().

◆ m_Result

nlohmann::json Olympe::AsyncJSONLoader::m_Result
private

Definition at line 119 of file AsyncJSONLoader.h.

Referenced by GetResult(), GetState(), LoadAsync(), and Reset().

◆ m_State

AsyncLoadState Olympe::AsyncJSONLoader::m_State
mutableprivate

Definition at line 117 of file AsyncJSONLoader.h.

Referenced by GetState(), LoadAsync(), and Reset().


The documentation for this class was generated from the following files: