Game of Ur 0.3.3
This is a computer adaptation of Game of Ur, written in C++ mainly using SDL and OpenGL.
|
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 ResourceDatabase & | GetInstance () |
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. | |
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.
A resource description of a known resource might look something like this:
This description can then be stored in the ResourceDatabase, like so:
When the resource is required later on it can be created or retrieved, like this:
The resource will be constructed based on its type ("type": "Texture"
), constructor ("method": "fromFile"
), and parameters ("parameters": {"path": "data/textures/button_pressed.png"}
).
|
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()
resourceDescription | A description of the Resource. |
|
staticprivate |
Asserts the correctness of a resource's description, per the Resource types and constructors known to the ResourceDatabase.
resourceDescription | The description of a resource. |
|
static |
Constructs a fresh anonymous resource using a registered constructor according to its description in JSON.
TResource | The type of Resource being constructed. |
resourceDescription | A description of the resource, including parameters used by its factory in its construction. |
|
static |
Returns the sole instance of the ResourceDatabase used by this application.
|
static |
Gets a reference to or constructs a Resource by name whose description has been stored in this ResourceDatabase.
TResource | The type of resource being retrieved. |
resourceName | The name of the resource being retrieved. |
|
static |
Tests whether a particular resource has already been loaded into memory.
TResource | The type of resource whose existence is being tested. |
resourceName | The name of the resource. |
true | If the resource is in memory; |
false | If the resource isn't in memory; |
|
static |
Tests whether the description of a particular named resource exists in this project's ResourceDatabase.
resourceName | The name of the resource whose registration is being tested. |
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.
TResource | The type of Resource the factory will manage. |
factoryName | The resource type string of the resource. |
pFactory | A reference to the factory to be stored by this project's ResourceDatabase. |
void ToyMaker::ResourceDatabase::registerResourceConstructor | ( | const std::string & | resourceType, |
const std::string & | methodName, | ||
std::unique_ptr< IResourceConstructor > | pFactoryMethod ) |
Registers a particular ResourceConstructor for a Resource.
TResource | The type of resource constructed. |
TResourceConstructor | The resource constructor's type. |
resourceType | The resource type string. |
methodName | The resource constructor type string. |
pFactoryMethod | A pointer to the constructor. |
|
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.