Inviwo  0.9.10.1
Inviwo documentation
inviwo::ResourceManager Class Reference

A resource manager to store data to avoid creating/loading the same dataset twice. More...

#include <resourcemanager.h>

+ Inheritance diagram for inviwo::ResourceManager:

Public Member Functions

template<typename T >
std::shared_ptr< T > getResource (const std::string &key)
 Finds and returns the resource with given key and type. More...
 
template<typename T >
void addResource (const std::string &key, std::shared_ptr< T > resource, bool overwrite=false)
 Adds a resource to the manager. More...
 
template<typename T >
bool hasResource (const std::string &key) const
 Checks if a resource of type T with given key exists. More...
 
template<typename T >
void removeResource (const std::string &key)
 Removes a resource from the manager. More...
 
void removeResource (const std::string &key, const std::type_index &type)
 Removes a resource from the manager. More...
 
void clear ()
 Clears the resource manager. More...
 
bool isEnabled () const
 Returns true if the resource manager is enabled.
 
void setEnabled (bool enable=true)
 Enable or disable the resource manager Notifys observers by calling ResourceManagerObservable::notifyEnableChanged. More...
 
size_t numberOfResources () const
 
- Public Member Functions inherited from inviwo::Observable< ResourceManagerObserver >
 Observable (const Observable< ResourceManagerObserver > &other)
 
 Observable (Observable< ResourceManagerObserver > &&other)
 
Observable< ResourceManagerObserver > & operator= (const Observable< ResourceManagerObserver > &other)
 
Observable< ResourceManagerObserver > & operator= (Observable< ResourceManagerObserver > &&other)
 
void addObserver (ResourceManagerObserver *observer)
 
void removeObserver (ResourceManagerObserver *observer)
 
virtual void startBlockingNotifications () override final
 
virtual void stopBlockingNotifications () override final
 

Additional Inherited Members

- Protected Member Functions inherited from inviwo::ResourceManagerObservable
void notifyResourceAdded (const std::string &key, const std::type_index &type, Resource *resource)
 
void notifyResourceRemoved (const std::string &key, const std::type_index &type, Resource *resource)
 
void notifyEnableChanged ()
 
- Protected Member Functions inherited from inviwo::Observable< ResourceManagerObserver >
void forEachObserver (C callback)
 
- Protected Member Functions inherited from inviwo::ObservableInterface
void addObservationHelper (Observer *observer)
 
void removeObservationHelper (Observer *observer)
 

Detailed Description

A resource manager to store data to avoid creating/loading the same dataset twice.

To avoid loading the same data twice, resources are stored in a std::unordered_map are hashed by type and key. Key should uniquely describe the dataset, for example, when loading a file from disk the filename could be used.

Example Usage:

std::shared_ptr<T> loadData(std::string filename){
auto rm = InviwoApplication::getPtr()->getResourceManager();
if(rm->hasResource<T>(filename)){
return rm->getResource<T>(filename);
} else {
auto data = std::make_shared<T>();
... // load/create data
rm->addResource<T>(filename,data);
}
}

Member Function Documentation

◆ addResource()

template<typename T >
void inviwo::ResourceManager::addResource ( const std::string &  key,
std::shared_ptr< T >  resource,
bool  overwrite = false 
)

Adds a resource to the manager.

Parameters
keykey of the resource to add
resourcea shared_ptr to the data to store
overwritea flag to indicate if overwriting existing resources is allowed.
Exceptions
inviwo::ResourceExceptionif resource with key and type T exists and overwrite is set to false

◆ clear()

void inviwo::ResourceManager::clear ( )

Clears the resource manager.

Does not release the memory if the data is used somewhere else.

◆ getResource()

template<typename T >
std::shared_ptr< T > inviwo::ResourceManager::getResource ( const std::string &  key)

Finds and returns the resource with given key and type.

Parameters
keykey of the resource to find
Returns
the found resource
Exceptions
inviwo::ResourceExceptionif resource with key and type T could not be found

◆ hasResource()

template<typename T >
bool inviwo::ResourceManager::hasResource ( const std::string &  key) const

Checks if a resource of type T with given key exists.

Parameters
keythe key to look for
Returns
bool true if such a resource exists, otherwise false.

◆ numberOfResources()

size_t inviwo::ResourceManager::numberOfResources ( ) const

Returns the number of added resources

◆ removeResource() [1/2]

template<typename T >
void inviwo::ResourceManager::removeResource ( const std::string &  key)

Removes a resource from the manager.

Does not release the memory if the data is used somewhere else.

Parameters
keythe key of the resource to remove

◆ removeResource() [2/2]

void inviwo::ResourceManager::removeResource ( const std::string &  key,
const std::type_index &  type 
)

Removes a resource from the manager.

Does not release the memory if the data is used somewhere else.

This method is called from the ResourceManagerDockWidget when removing a resource.

Parameters
keythe key of the resource to remove
typethe type as a string of the resource to remove

◆ setEnabled()

void inviwo::ResourceManager::setEnabled ( bool  enable = true)

Enable or disable the resource manager Notifys observers by calling ResourceManagerObservable::notifyEnableChanged.

Note
Does not clear the resource manager, calls to getResource and hasResource will continue to work resources that already has been added

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