c++ - Creating a generic conversion function -
i have resourcemanager takes in classes of type resource. resource parent class of other classes such shaderprogram, texture, mesh , camera unrelated 1 another.
suffice say, resourcemanager works. there 1 thing tedious , annoying, , that's when retrieve objects resourcemanager. here problem:
in order object resourcemanager call either of these functions:
static resource* get(int id); static resource* get(const std::string &name); the first function checks 1 std::unordered_map integer id; whereas second function checks std::unordered_map name manually given client. have 2 versions of these functions flexibility sakes because there times don't care object contained within resourcemanager (like mesh) , there times care (like camera or shaderprogram) because may want retrieve said objects name rather id.
either way, both functions return pointer resource. when call function, it's easy like:
rm::get("skyboxshader"); where rm typedef of resourcemanager since class static (all members/functions static). problem though rm::get(..) function returns resource*, , not child class added resourcemanager begin with. so, in order solve problem have manual type conversion can shaderprogram* instead of resource*. this:
auto s = static_cast<shaderprogram*>(rm::get(name)); so, everytime want access resource have insert type want static_cast. problematic insofar everytime needs access resource have type convert it. so, naturally created function, , being shaderprogram subject here, thus:
shaderprogram* renderer::program(const std::string &name) { auto s = static_cast<shaderprogram*>(rm::get(name)); return s; } this function static, , resourcemanager static class 2 go hand-in-hand. nice helper function , works , program renders result fine. problem have when i'm dealing other resources; means every resource exists, there has type-conversion function accommodate it. annoying. isn't there way can write generic type-conversion function this?
auto renderer::getresource(classtypeyouwant t, const std::string &name) { auto s = static_cast<t*>(rm::get(name)); return s; } here, auto keyword causes function derive type it's supposed dealing , return result accordingly. first guess might have use templates; problem templates can't limit types inserted function, , really don't want floating-point id numbers, char ids, let alone custom-defined ids. it's either string (might change const char* tbh) or ints or else.
how can create generic conversion function 1 described above?
have looked @ using dynamic_cast? if conversion fails dynamic_cast the pointer set nullptr. either write overloads each type or write template function pass the type want convert string or id , if conversion succeeds or fails return true or false.
template<typename t> bool renderer::getresource(t*& type, const std::string &name) { type = dynamic_cast<decltype(std::remove_reference<decltype(t)>::type)>(rm::get(name)); if (type == nullptr) return false; return true; }
Comments
Post a Comment