What's the proper to do this? I don't know the name.. virtual template inheritence? or many topics on the subject.. this? http://www.java2s.com/Tutorial/Cpp/0...nheritance.htm
Instead ofCode:#include <iostream>
#include <string>
class Resource
{
};
class TileSet : public Resource
{
public:
TileSet(const std::string& name)
{
this->name = name;
}
std::string name;
};
class Provider
{
};
class TileSetProvider : public Provider
{
public:
TileSet GetAsset(const std::string& name) { return TileSet(name); }
static TileSetProvider Type;
};
TileSetProvider TileSetProvider::Type = TileSetProvider();
class ResourceManager
{
public:
template<typename B>
B operator [](B& b)
{
return TileSetProvider();
}
//Provider* provider;
};
int main()
{
ResourceManager resourceManager;
//resourceManager.AddProvider(TileSetProvider());
TileSet grass = resourceManager[TileSetProvider::Type].GetAsset("grass");
std::cout << grass.name << std::endl;
}
resourceManager[TileSetProvider::Type].GetAsset("grass")
it could be
resourceManager.GetAsset(TileSetProvider::Type, "grass")
The "TileSetProvider::Type" is a bit ugly. I'd prefer to pass just "TileSet", but "TileSet" is the class name, and "TileSet::Type" wouldn't work because the template needs to know to return a TileSetProvider object (problem lies that template functions don't have the <> access operators (like C#) instead they naturally determine to types :( ). Which would be easy if I wanted to hardcode it all into the class, but I want it generic. Provider init/list and asset init/list removed for testing.
This is a test. Not serious coding.
I could also accomplish this using virtual functions that return Resource*, access resourceManager["TileSet"] and then static_cast<TileSet>