23#ifndef FOOLSENGINE_RESOURCEDATABASE_H
24#define FOOLSENGINE_RESOURCEDATABASE_H
34#include <nlohmann/json.hpp>
45 template <
typename TResource>
class Resource;
81 template <
typename TResource>
106 virtual std::shared_ptr<IResource>
createResource(
const nlohmann::json& resourceDescription)=0;
138 virtual std::shared_ptr<IResource>
createResource(
const nlohmann::json& methodParameters)=0;
160 template <
typename TResource,
typename TResourceConstructor>
163 friend class ResourceDatabase;
232 template <
typename TResource>
242 template <
typename TResource>
262 template <
typename TResource>
263 static bool HasResource(
const std::string& resourceName);
272 template <
typename TResource>
273 void registerFactory (
const std::string& factoryName, std::unique_ptr<IResourceFactory> pFactory);
284 template <
typename TResource,
typename TResourceConstructor>
285 void registerResourceConstructor (
const std::string& resourceType,
const std::string& methodName, std::unique_ptr<IResourceConstructor> pFactoryMethod);
313 std::map<std::string, std::unique_ptr<IResourceFactory>>
mFactories {};
319 std::map<std::string, std::weak_ptr<IResource>>
mResources {};
371 template <
typename TDerived>
421 template<
typename TResource>
436 std::shared_ptr<IResource>
createResource(
const nlohmann::json& resourceDescription)
override;
473 template<
typename TResource,
typename TResourceFactoryMethod>
492 (void)explicitlyInitializeMe;
520 template <
typename TResource>
523 std::shared_ptr<IResource> pResource {
nullptr };
526 std::map<std::string, nlohmann::json>::iterator resourceDescPair { resourceDatabase.mResourceDescriptions.find(resourceName) };
528 resourceDescPair != resourceDatabase.mResourceDescriptions.end()
529 &&
"No resource with this name was found amongst known resources"
532 resourceDescPair->second[
"type"].get<std::string>() == TResource::getResourceTypeName()
533 &&
"The type of resource requested does not match the type of resource as declared \
539 std::map<std::string, std::weak_ptr<IResource>>::iterator resourcePair { resourceDatabase.mResources.find(resourceName) };
540 if(resourcePair != resourceDatabase.mResources.end()) {
543 if(resourcePair->second.expired()) {
544 resourceDatabase.mResources.erase(resourcePair->first);
548 pResource = resourcePair->second.lock();
556 pResource = std::static_pointer_cast<typename Resource<TResource>::IResource, TResource>(
559 resourceDatabase.mResources[resourceName] = std::weak_ptr<IResource>{ pResource };
566 return std::static_pointer_cast<TResource>(std::static_pointer_cast<
Resource<TResource>>(pResource));
569 template<
typename TResource>
572 std::shared_ptr<IResource> pResource {
nullptr };
577 pResource = resourceDatabase
578 .mFactories.at(resourceDescription.at(
"type").get<std::string>())
579 ->createResource(resourceDescription);
580 assert(pResource &&
"Resource could not be constructed");
586 return std::static_pointer_cast<TResource>(std::static_pointer_cast<
Resource<TResource>>(pResource));
589 template<
typename TResource>
593 bool typeMatched {
false };
594 bool objectLoaded {
false };
595 if(descriptionPresent){
597 TResource::getResourceTypeName()
598 == resourceDatabase.mResourceDescriptions.at(resourceName).at(
"type").get<std::string>()
601 resourceDatabase.mResources.find(resourceName)
602 != resourceDatabase.mResources.end()
606 descriptionPresent && typeMatched && objectLoaded
610 template <
typename TResource>
612 std::cout <<
"Loading resource (" << TResource::getResourceTypeName() <<
") : " << nlohmann::to_string(resourceDescription[
"parameters"]) <<
"\n";
613 return mFactoryMethods.at(resourceDescription[
"method"].get<std::string>())->createResource(
614 resourceDescription[
"parameters"].get<nlohmann::json>()
618 template <
typename TDerived>
623 template <
typename TResource,
typename TResourceFactoryMethod>
627 resourceRegistrator.emptyFunc();
632 template <
typename TResource>
637 template <
typename TResource,
typename TResourceConstructor>
642 template <
typename TResource>
644 assert((std::is_base_of<IResource, TResource>::value) &&
"Resource must be subclass of IResource");
645 mFactories[factoryName] = std::move(pFactory);
648 template <
typename TResource,
typename TResourceConstructor>
650 assert((std::is_base_of<IResource, TResource>::value) &&
"Resource must be subclass of IResource");
651 assert((std::is_base_of<IResourceConstructor, TResourceConstructor>::value) &&
"Resource must be subclass of IResourceConstructor");
652 mFactories.at(resourceType)->mFactoryMethods[methodName] = std::move(pFactoryMethod);
A single way that a resource may be constructed.
Definition resource_database.hpp:123
IResourceConstructor()=default
Construct a new IResourceConstructor object.
static void RegisterResourceConstructor()
Registers this resource constructor against its respective ResourceFactory during static initializati...
Definition resource_database.hpp:638
virtual std::shared_ptr< IResource > createResource(const nlohmann::json &methodParameters)=0
Creates a resource object using the parameters specified in methodParameters.
virtual ~IResourceConstructor()=default
Destroys this resource constructor (when the application is terminated.)
virtual std::string getResourceConstructorName_() const =0
Get the Resource Constructor type string for this constructor.
A class that holds references to all constructors for a type of Resource object.
Definition resource_database.hpp:91
std::map< std::string, std::unique_ptr< IResourceConstructor > > mFactoryMethods
A list of constructors that may be used to create a Resource of this kind.
Definition resource_database.hpp:113
virtual ~IResourceFactory()=default
Destroy the IResourceFactory object.
virtual std::shared_ptr< IResource > createResource(const nlohmann::json &resourceDescription)=0
Creates a resource object of a specific type using its JSON resource description.
Base class of all Resource types.
Definition resource_database.hpp:54
static void RegisterResource()
Registers this resource as a Resource type with the ResourceDatabase.
Definition resource_database.hpp:633
IResource()=default
Construct a new IResource object.
virtual ~IResource()=default
Destroy the IResource object.
virtual std::string getResourceTypeName_() const =0
Get the Resource Type string for this resource.
Helper class for registering a class at program startup.
Definition registrator.hpp:64
An object representing one method for creating a resource of a given kind.
Definition resource_database.hpp:474
static void registerSelf()
A helper function that informs the ResourceDatabase that this constructor type exists.
Definition resource_database.hpp:624
std::string getResourceConstructorName_() const override
Gets the resource constructor type string of the constructor.
Definition resource_database.hpp:483
ResourceConstructor(int explicitlyInitializeMe)
Construct a new ResourceConstructor object.
Definition resource_database.hpp:491
static Registrator< ResourceConstructor< TResource, TResourceFactoryMethod > > s_registrator
A helper class responsible for calling this class' registerSelf() method at during the static initial...
Definition resource_database.hpp:508
A database of all Resource types available for this project, and the various ResourceConstructors res...
Definition resource_database.hpp:216
static ResourceDatabase & GetInstance()
Returns the sole instance of the ResourceDatabase used by this application.
Definition resource_database.cpp:72
static std::shared_ptr< TResource > ConstructAnonymousResource(const nlohmann::json &resourceDescription)
Constructs a fresh anonymous resource using a registered constructor according to its description in ...
Definition resource_database.hpp:570
std::map< std::string, std::weak_ptr< IResource > > mResources
References to all resources that have been loaded and are being held in memory.
Definition resource_database.hpp:319
void registerResourceConstructor(const std::string &resourceType, const std::string &methodName, std::unique_ptr< IResourceConstructor > pFactoryMethod)
Registers a particular ResourceConstructor for a Resource.
Definition resource_database.hpp:649
static bool HasResource(const std::string &resourceName)
Tests whether a particular resource has already been loaded into memory.
Definition resource_database.hpp:590
static void AssertResourceDescriptionValidity(const nlohmann::json &resourceDescription)
Asserts the correctness of a resource's description, per the Resource types and constructors known to...
Definition resource_database.cpp:7
ResourceDatabase()=default
Construct a new ResourceDatabase object.
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 construc...
Definition resource_database.hpp:325
void registerFactory(const std::string &factoryName, std::unique_ptr< IResourceFactory > pFactory)
Registers a factory responsible for tracking valid ResourceConstructors for a particular type of Reso...
Definition resource_database.hpp:643
std::map< std::string, std::unique_ptr< IResourceFactory > > mFactories
Pointers to different factories, each responsible for the creation of different kinds of resources.
Definition resource_database.hpp:313
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
static bool HasResourceDescription(const std::string &resourceName)
Tests whether the description of a particular named resource exists in this project's ResourceDatabas...
Definition resource_database.cpp:66
static void AddResourceDescription(const nlohmann::json &resourceDescription)
Adds the description (name, constructor, related parameters) to the resource database.
Definition resource_database.cpp:43
Tracks pointers to all ResourceConstructors responsible for creating a resource of one type.
Definition resource_database.hpp:422
ResourceFactory()
Construct a new Resource Factory object.
Definition resource_database.hpp:427
std::shared_ptr< IResource > createResource(const nlohmann::json &resourceDescription) override
Create an object of type Resource, specifying its constructor and the constructor's parameters in JSO...
Definition resource_database.hpp:611
The base class for any type whose creation and storage should be managed by the ResourceDatabase.
Definition resource_database.hpp:372
std::string getResourceTypeName_() const override
Get the resource type string for this resource.
Definition resource_database.hpp:381
Resource(int explicitlyInitializeMe)
Construct a new resource object.
Definition resource_database.hpp:389
static Registrator< Resource< TDerived > > & s_registrator
A static variable to this classes Registrator.
Definition resource_database.hpp:408
static void registerSelf()
A helper function that actually performs the task of registering this class with the ResourceDatabase...
Definition resource_database.hpp:619
Namespace containing all class definitions and functions related to the ToyMaker engine.
Definition camera_system.hpp:20
Contains the definition for the Registrator<T> utility class, used anywhere that automatic registrati...