0

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.

Patrik Bašo
  • 145
  • 8
  • Have you learned about inheritance and virtual functions yet? – Sam Varshavchik May 03 '21 at 21:13
  • @Sam Varshavchik yes , I did, I just want to provide easy way, how I could easily create new ResourceManagers without having to create many files you know. Because every manager does the same thing. – Patrik Bašo May 03 '21 at 21:13
  • 1
    And what exactly is `BaseManager`? It is unclear from your question. Perhaps what you are looking for is to simply define a `BaseManager` class that contains nothing but abstract virtual functions, and have the template inherit from `BaseManager` and implement all virtual functions. Then, `ResourceManager` would need to store a vector of `std::shared_ptr` or `std::unique_ptr`s to `BaseManager`s, and the rest would pretty much work, as is. – Sam Varshavchik May 03 '21 at 21:18
  • @SamVarshavchik `BaseManager` is manager for concrete Class inherited from `BaseResource` . And then ResourceManager should 'unity' everything , and also add some way how to add new `BaseManager` by simply calling function – Patrik Bašo May 03 '21 at 21:21
  • @SamVarshavchik if I define those functions as `pure virtual` where do i define them then ? – Patrik Bašo May 03 '21 at 21:22
  • Sounds like the template should inherit from `BaseManager` as I suspected. Implementing abstract classes and virtual functions is something that's covered [in every C++ textbook](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). It's unrealistic for me or anyone else to copy/paste entire chapters here, to explain how to implement pure virtual functions in C++, Stackoverflow isn't really a replacement for a C++ textbook or tutorial. C++ is the most complicated general purpose programming language in use today; the only way to learn it is from a textbook. – Sam Varshavchik May 03 '21 at 21:33
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/231901/discussion-between-patrik-baso-and-sam-varshavchik). – Patrik Bašo May 03 '21 at 21:39
  • And where does BaseResourceManager fit into this? It is not used by any of the other code you give. Is it meant to be be templatised Resource classes? – Ian4264 May 03 '21 at 21:40
  • @Ian4264 It should be templatised Manager for Specific Resource given by template – Patrik Bašo May 03 '21 at 21:44

0 Answers0