Last active
December 20, 2015 03:19
-
-
Save Gerjo/6063038 to your computer and use it in GitHub Desktop.
Variadic template multiton
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template <class T, typename...PARAMS> | |
T* Load(const std::string& ident, PARAMS...args) { | |
// Create a hash from the first parameter: | |
long hash = StringHash(ident); | |
auto it = resources.find(hash); | |
if (it != resources.end()) | |
{ | |
T* resource = static_cast<T*>((*it).second); | |
refCounters.at(hash) += 1; | |
return resource; | |
} | |
// Resource was not found, create a new one: | |
T* resource = new T(args...); | |
resources.insert(make_pair(hash, resource)); | |
refCounters.insert(make_pair(hash, 1)); | |
resource->resourceID = hash; | |
resource->resourcePath = ident; | |
return resource; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment