#include <nap/resourcemanager.h>
Public Member Functions | |
ResourceManager (nap::Core &core) | |
~ResourceManager () | |
bool | loadFile (const std::string &filename, utility::ErrorState &errorState) |
bool | loadFile (const std::string &filename, const std::string &externalChangedFile, utility::ErrorState &errorState) |
rtti::ObjectPtr< rtti::Object > | findObject (const std::string &id) const |
template<class T > | |
rtti::ObjectPtr< T > | findObject (const std::string &id) const |
template<class T > | |
std::vector< rtti::ObjectPtr< T > > | getObjects () const |
rtti::ObjectPtr< rtti::Object > | createObject (const rtti::TypeInfo &type) |
template<typename T > | |
rtti::ObjectPtr< T > | createObject () |
void | checkForFileChanges () |
rtti::Factory & | getFactory () |
void | watchDirectory (const std::string &directory) |
Public Attributes | |
nap::Signal | mPreResourcesLoadedSignal |
nap::Signal | mPostResourcesLoadedSignal |
The resource manager is responsible for loading a JSON file that contains all the resources that are necessary for an application to run.
When loading a JSON file all the objects declared inside that file are created and initialized by the resource manager. These objects are called 'resources'. Every loaded resource is owned by the resource manager. This means that the lifetime of a resource is fully managed by the resource manager and not by the client. The resource manager also updates the content in real-time when a change to the loaded JSON file is detected.
Every resource has a unique identifier, as declared by the 'mID' property. The name of the object is required to be unique. De-serialization will fail when a duplicate object ID is discovered.
The most important task of a resource is to tell the resource manager if initialization succeeded. If initialization of a resource fails the resource manager will halt execution, return an error message and as a result stop further execution of a program.
loadFile() is automatically called by the nap::AppRunner on startup, using the data file linked to by the nap::ProjectInfo
ResourceManager | ( | nap::Core & | core | ) |
~ResourceManager | ( | ) |
void checkForFileChanges | ( | ) |
Function that runs the file monitor to check for changes. If changes are found in files that were loaded by the manager, reloading and real-time updating of data takes place.
rtti::ObjectPtr<T> createObject | ( | ) |
Creates an object of type T and adds it to the manager.
rtti::ObjectPtr<rtti::Object> createObject | ( | const rtti::TypeInfo & | type | ) |
Creates an object and adds it to the manager.
type | object type to create. |
rtti::ObjectPtr<rtti::Object> findObject | ( | const std::string & | id | ) | const |
Find an object by object ID. Returns null if not found.
id | unique id of the object to find. |
rtti::ObjectPtr<T> findObject | ( | const std::string & | id | ) | const |
Find an object of type T. Returns null if not found.
Example:
id | the unique id of the object to find. |
rtti::Factory& getFactory | ( | ) |
std::vector< rtti::ObjectPtr< T > > getObjects |
Get all objects of a particular type.
bool loadFile | ( | const std::string & | filename, |
const std::string & | externalChangedFile, | ||
utility::ErrorState & | errorState | ||
) |
bool loadFile | ( | const std::string & | filename, |
utility::ErrorState & | errorState | ||
) |
Helper that calls loadFile() without additional modified objects. See loadFile() comments for a full description. The file should be located in the 'data' folder (current working directory) of your application.
filename | name of JSON resource file to load, for example: "default.json" |
errorState | contains the error when the load operation fails. |
void watchDirectory | ( | const std::string & | directory | ) |
All files linked to by the application that reside in the current working directory will be monitored. If a file change is detected to any of the files, the resource manager will attempt to hot-load the changes directly into the running application
directory | the directory to monitor |
nap::Signal mPostResourcesLoadedSignal |
Signal that is emitted after a file has been successfully loaded
nap::Signal mPreResourcesLoadedSignal |
Signal that is emitted when a file is about to be loaded