I'm very new in C++, but I'm working on a little framework of mine.
For this framework, I need to load various types of Resources
and I would love to write it such it would be very easy to add new Resources
without having to create managers for every one of them.
So I was thinking like this:
I need base BaseResource
class from which I can then inherit other resource types
class BaseResource {
uint32_t resource_id;
std::string name;
std::string path;
public:
uint32_t get_id() {
return resource_id;
}
const std::string& get_name() {
return name;
}
const std::string& get_path() {
return path;
}
};
Then what I need is some kind of base BaseResourceManager
templated for Resource
template<class T>
class BaseResourceManager{
// path = name
std::vector<T> _resources;
std::map<std::string, uint32_t> _path_map; //
public:
void load(std::string path) {
// load from file and call T with name
_path_map.insert();
}
T& get(uint32_t idx) {
return _resources[idx];
}
T& get_by_name(std::string name) {
return _resources[_path_map[name]];
}
void delete(uint32_t idx) {
std::string name = resources[idx].get_name();
_resources[idx] = null_ptr;
_path_map[name] = nullptr; // ERASE ??
}
};
But now I'm kind of stuck while making something, what could glue multiple BaseResourceManager<T1>
, BaseResourceManager<T2>
together.
All I could do, but it is wrong, is this main ResourceManager
class ResourceManager {
// managers
std::vector<BaseManager> managers;
public:
ResourceManager() {
}
void init() {
}
void register_manager(BaseManager&& man) {
managers.push_back(man);
}
template <typename T, size_t i>
T get() {
return dynamic_cast<T>(managers[i]);
}
};
I think it would be a good idea to store in _managers
some kind of pointer to heap-allocated managers,
and also what I couldn't solve was creating get()
function only with one template, somehow automatically create a sequence of numbers for every type as a second template.