1
0
Fork 0
This repository has been archived on 2023-03-27. You can view files and clone it, but cannot push or open issues or pull requests.
matabstrix/src/store.hpp

35 lines
796 B
C++
Raw Permalink Normal View History

2015-11-11 17:28:11 +00:00
#ifndef _STORE_HPP_
#define _STORE_HPP_
#include <type_traits>
#include <string>
#include <map>
2015-11-16 13:05:04 +00:00
struct Adapter;
struct Resource;
2015-11-11 20:17:11 +00:00
struct Store
2015-11-11 17:28:11 +00:00
{
template <class T>
2015-11-16 13:05:04 +00:00
const T *load(const Adapter &adapter, const std::string &name);
2015-11-11 17:28:11 +00:00
private:
std::map<std::string, Resource*> _resources;
};
template <class T>
2015-11-16 13:05:04 +00:00
const T *Store::load(const Adapter &adapter, const std::string &name)
2015-11-11 17:28:11 +00:00
{
static_assert(std::is_convertible<T, Resource>::value
&& !std::is_same<T, Resource>::value,
"Store can load only Resource's children");
2015-11-11 21:12:57 +00:00
if (_resources.find(T::filename(name)) == _resources.end())
2015-11-16 13:05:04 +00:00
_resources[T::filename(name)] = new T(adapter, name);
2015-11-11 17:28:11 +00:00
_resources[T::filename(name)]->_ref_count++;
return (T*)_resources[T::filename(name)];
}
#endif // _STORE_HPP_