Skip to content

Instantly share code, notes, and snippets.

@Gerjo
Last active December 20, 2015 03:19
Show Gist options
  • Save Gerjo/6063038 to your computer and use it in GitHub Desktop.
Save Gerjo/6063038 to your computer and use it in GitHub Desktop.
Variadic template multiton
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