NAP
Public Member Functions | Public Attributes | List of all members
ResourceManager Class Referencefinal

#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::ObjectfindObject (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::ObjectcreateObject (const rtti::TypeInfo &type)
 
template<typename T >
rtti::ObjectPtr< T > createObject ()
 
void checkForFileChanges ()
 
rtti::FactorygetFactory ()
 
void watchDirectory (const std::string &directory)
 

Public Attributes

nap::Signal mPreResourcesLoadedSignal
 
nap::Signal mPostResourcesLoadedSignal
 

Description

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

Collaboration diagram for ResourceManager:
[legend]

Constructor & Destructor Documentation

◆ ResourceManager()

◆ ~ResourceManager()

Member Function Documentation

◆ checkForFileChanges()

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.

◆ createObject() [1/2]

rtti::ObjectPtr<T> createObject ( )

Creates an object of type T and adds it to the manager.

Returns
the newly created object.

◆ createObject() [2/2]

rtti::ObjectPtr<rtti::Object> createObject ( const rtti::TypeInfo type)

Creates an object and adds it to the manager.

Parameters
typeobject type to create.
Returns
newly created object.

◆ findObject() [1/2]

rtti::ObjectPtr<rtti::Object> findObject ( const std::string &  id) const

Find an object by object ID. Returns null if not found.

Parameters
idunique id of the object to find.
Returns
the object, nullptr if not found.

◆ findObject() [2/2]

rtti::ObjectPtr<T> findObject ( const std::string &  id) const

Find an object of type T. Returns null if not found.

Example:

mResourceManager = getCore().getResourceManager();
auto window = mResourceManager->findObject<nap::RenderWindow>("Window0");
Parameters
idthe unique id of the object to find.
Returns
the object of type T, nullptr if not found.

◆ getFactory()

rtti::Factory& getFactory ( )
Returns
object capable of creating objects with custom construction parameters.

◆ getObjects()

std::vector< rtti::ObjectPtr< T > > getObjects

Get all objects of a particular type.

Returns
all objects of the requested type.

◆ loadFile() [1/2]

bool loadFile ( const std::string &  filename,
const std::string &  externalChangedFile,
utility::ErrorState errorState 
)

◆ loadFile() [2/2]

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.

Parameters
filenamename of JSON resource file to load, for example: "default.json"
errorStatecontains the error when the load operation fails.
Returns
if the file loaded successfully.

◆ watchDirectory()

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

Parameters
directorythe directory to monitor

Member Data Documentation

◆ mPostResourcesLoadedSignal

nap::Signal mPostResourcesLoadedSignal

Signal that is emitted after a file has been successfully loaded

◆ mPreResourcesLoadedSignal

nap::Signal mPreResourcesLoadedSignal

Signal that is emitted when a file is about to be loaded

nap::RenderWindow
Definition: renderwindow.h:43