NAP
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)
 
bool loadFile (const std::string &filename, const std::string &externalChangedFile, utility::ErrorState &errorState)
 
const rtti::ObjectPtr< rtti::ObjectfindObject (const std::string &id)
 
template<class T >
const rtti::ObjectPtr< T > findObject (const std::string &id)
 
template<class T >
std::vector< rtti::ObjectPtr< T > > getObjects () const
 
const rtti::ObjectPtr< rtti::ObjectcreateObject (const rtti::TypeInfo &type)
 
template<typename T >
const rtti::ObjectPtr< T > createObject ()
 
void checkForFileChanges ()
 
rtti::FactorygetFactory ()
 

Detailed 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.

File load example:

// Get resource manager and load application JSON
mResourceManager = getCore().getResourceManager();
if (!mResourceManager->loadFile("helloworld.json", error))
return false;

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]

const 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.

◆ createObject() [2/2]

const rtti::ObjectPtr<T> createObject ( )

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

Returns
the newly created object.

◆ findObject() [1/2]

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

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]

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

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 ( ) const

Get all objects of a particular type.

Returns
all objects of the requested type.

◆ loadFile() [1/3]

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

Helper that calls loadFile without additional modified objects. See loadFile() comments for a full description.

Parameters
fileNameJSON resource file to load.
errorStatecontains the error when the load operation fails.
Returns
if the file loaded successfully.

◆ loadFile() [2/3]

bool loadFile ( const std::string &  filename)

Helper that calls loadFile without additional modified objects. See loadFile() comments for a full description.

Parameters
fileNameJSON resource file to load.
Returns
if the file loaded successfully.

◆ loadFile() [3/3]

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