Game of Ur 0.3.3
This is a computer adaptation of Game of Ur, written in C++ mainly using SDL and OpenGL.
Loading...
Searching...
No Matches
ToyMaker::ResourceDatabase Class Reference

A database of all Resource types available for this project, and the various ResourceConstructors responsible for making them. More...

#include <resource_database.hpp>

Public Member Functions

template<typename TResource>
void registerFactory (const std::string &factoryName, std::unique_ptr< IResourceFactory > pFactory)
 Registers a factory responsible for tracking valid ResourceConstructors for a particular type of Resource.
 
template<typename TResource, typename TResourceConstructor>
void registerResourceConstructor (const std::string &resourceType, const std::string &methodName, std::unique_ptr< IResourceConstructor > pFactoryMethod)
 Registers a particular ResourceConstructor for a Resource.
 

Static Public Member Functions

static ResourceDatabaseGetInstance ()
 Returns the sole instance of the ResourceDatabase used by this application.
 
template<typename TResource>
static std::shared_ptr< TResource > GetRegisteredResource (const std::string &resourceName)
 Gets a reference to or constructs a Resource by name whose description has been stored in this ResourceDatabase.
 
template<typename TResource>
static std::shared_ptr< TResource > ConstructAnonymousResource (const nlohmann::json &resourceDescription)
 Constructs a fresh anonymous resource using a registered constructor according to its description in JSON.
 
static bool HasResourceDescription (const std::string &resourceName)
 Tests whether the description of a particular named resource exists in this project's ResourceDatabase.
 
template<typename TResource>
static bool HasResource (const std::string &resourceName)
 Tests whether a particular resource has already been loaded into memory.
 
static void AddResourceDescription (const nlohmann::json &resourceDescription)
 Adds the description (name, constructor, related parameters) to the resource database.
 

Private Member Functions

 ResourceDatabase ()=default
 Construct a new ResourceDatabase object.
 

Static Private Member Functions

static void AssertResourceDescriptionValidity (const nlohmann::json &resourceDescription)
 Asserts the correctness of a resource's description, per the Resource types and constructors known to the ResourceDatabase.
 

Private Attributes

std::map< std::string, std::unique_ptr< IResourceFactory > > mFactories {}
 Pointers to different factories, each responsible for the creation of different kinds of resources.
 
std::map< std::string, std::weak_ptr< IResource > > mResources {}
 References to all resources that have been loaded and are being held in memory.
 
std::map< std::string, nlohmann::json > mResourceDescriptions {}
 A list of all Resources that are known to the database, including details of how they may be constructed if no instances of them are present in memory.
 

Detailed Description

A database of all Resource types available for this project, and the various ResourceConstructors responsible for making them.

Also holds a list of resource definitions. When an attempt is made to retrieve a resource by its name, a copy of a reference to it is returned if it exists in memory, or the resource is constructed per the parameters stored in its description.

Usage:

A resource description of a known resource might look something like this:

nlohmann::json buttonPressedTextureDescription = "{\
\"name\": \"Bad_Button_Pressed_Texture\",\
\"type\": \"Texture\",\
\"method\": \"fromFile\",\
\"parameters\": {\
\"path\": \"data/textures/button_pressed.png\"\
}\
}";

This description can then be stored in the ResourceDatabase, like so:

ToyMaker::ResourceDatabase::AddResourceDescription(buttonPressedTextureDescription);
static void AddResourceDescription(const nlohmann::json &resourceDescription)
Adds the description (name, constructor, related parameters) to the resource database.
Definition resource_database.cpp:43

When the resource is required later on it can be created or retrieved, like this:

std::shared_ptr<ToyMaker::Texture> buttonPressedTexture { ToyMaker::ResourceDatabase::GetRegisteredResource(\"Bad_Button_Pressed_Texture\") };
static std::shared_ptr< TResource > GetRegisteredResource(const std::string &resourceName)
Gets a reference to or constructs a Resource by name whose description has been stored in this Resour...
Definition resource_database.hpp:521

The resource will be constructed based on its type ("type": "Texture"), constructor ("method": "fromFile"), and parameters ("parameters": {"path": "data/textures/button_pressed.png"}).

Warning
Attempting to store the two Resource descriptions with the same name is invalid, and will cause an error.
The ResourceDatabase does not check whether the construction parameters provided will actually help create a valid Resource object. It only checks that the Resource and Resource Constructor specified in the description are known to it.
Invalid parameters will likely cause an error to be thrown, depending on the ResourceConstructor's implementation.
See also
Resource
ResourceConstructor

Member Function Documentation

◆ AddResourceDescription()

void ResourceDatabase::AddResourceDescription ( const nlohmann::json & resourceDescription)
static

Adds the description (name, constructor, related parameters) to the resource database.

A resource stored in this way may be retrieved at any point later in the project using GetRegisteredResource()

Parameters
resourceDescriptionA description of the Resource.
See also
GetRegisteredResource()

◆ AssertResourceDescriptionValidity()

void ResourceDatabase::AssertResourceDescriptionValidity ( const nlohmann::json & resourceDescription)
staticprivate

Asserts the correctness of a resource's description, per the Resource types and constructors known to the ResourceDatabase.

Parameters
resourceDescriptionThe description of a resource.

◆ ConstructAnonymousResource()

template<typename TResource>
std::shared_ptr< TResource > ToyMaker::ResourceDatabase::ConstructAnonymousResource ( const nlohmann::json & resourceDescription)
static

Constructs a fresh anonymous resource using a registered constructor according to its description in JSON.

Template Parameters
TResourceThe type of Resource being constructed.
Parameters
resourceDescriptionA description of the resource, including parameters used by its factory in its construction.
Returns
std::shared_ptr<TResource> A reference to the newly constructed resource.

◆ GetInstance()

ResourceDatabase & ResourceDatabase::GetInstance ( )
static

Returns the sole instance of the ResourceDatabase used by this application.

Returns
ResourceDatabase& A reference to this project's ResourceDatabase.

◆ GetRegisteredResource()

template<typename TResource>
std::shared_ptr< TResource > ToyMaker::ResourceDatabase::GetRegisteredResource ( const std::string & resourceName)
static

Gets a reference to or constructs a Resource by name whose description has been stored in this ResourceDatabase.

Template Parameters
TResourceThe type of resource being retrieved.
Parameters
resourceNameThe name of the resource being retrieved.
Returns
std::shared_ptr<TResource> A reference to the retrieved resource.

◆ HasResource()

template<typename TResource>
bool ToyMaker::ResourceDatabase::HasResource ( const std::string & resourceName)
static

Tests whether a particular resource has already been loaded into memory.

Template Parameters
TResourceThe type of resource whose existence is being tested.
Parameters
resourceNameThe name of the resource.
Return values
trueIf the resource is in memory;
falseIf the resource isn't in memory;

◆ HasResourceDescription()

bool ResourceDatabase::HasResourceDescription ( const std::string & resourceName)
static

Tests whether the description of a particular named resource exists in this project's ResourceDatabase.

Parameters
resourceNameThe name of the resource whose registration is being tested.
Return values
trueIf a description for this Resource exists;
falseIf no description for this Resource exists;

◆ registerFactory()

template<typename TResource>
void ToyMaker::ResourceDatabase::registerFactory ( const std::string & factoryName,
std::unique_ptr< IResourceFactory > pFactory )

Registers a factory responsible for tracking valid ResourceConstructors for a particular type of Resource.

Template Parameters
TResourceThe type of Resource the factory will manage.
Parameters
factoryNameThe resource type string of the resource.
pFactoryA reference to the factory to be stored by this project's ResourceDatabase.

◆ registerResourceConstructor()

template<typename TResource, typename TResourceConstructor>
void ToyMaker::ResourceDatabase::registerResourceConstructor ( const std::string & resourceType,
const std::string & methodName,
std::unique_ptr< IResourceConstructor > pFactoryMethod )

Registers a particular ResourceConstructor for a Resource.

Template Parameters
TResourceThe type of resource constructed.
TResourceConstructorThe resource constructor's type.
Parameters
resourceTypeThe resource type string.
methodNameThe resource constructor type string.
pFactoryMethodA pointer to the constructor.

Member Data Documentation

◆ mFactories

std::map<std::string, std::unique_ptr<IResourceFactory> > ToyMaker::ResourceDatabase::mFactories {}
private

Pointers to different factories, each responsible for the creation of different kinds of resources.

Each factory tracks all constructors that can be used to create a resource.


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