VAPOR3 3.9.4
IResourceManager.h
Go to the documentation of this file.
1#pragma once
2
3#include <string>
4#include <map>
5#include "vapor/VAssert.h"
6#include "vapor/MyBase.h"
7
8#ifdef WIN32
9 #ifdef RENDER_EXPORTS
10 #define IRESOURCEMANAGER_IMPLEMENT
11 #endif
12#else
13 #define IRESOURCEMANAGER_IMPLEMENT
14#endif
15
16namespace VAPoR {
17
18template<typename K, typename T> class RENDER_API IResourceManager : public Wasp::MyBase {
19protected:
20 std::map<K, T *> _map;
21
22 T *GetResource(const K &key);
23
24public:
26 bool HasResource(const K &key) const;
27 bool HasResource(const T *resource) const;
28 virtual int LoadResourceByKey(const K &key) = 0;
29 bool AddResource(const K &key, T *resource);
30 void DeleteResource(const K &key);
31};
32
33#ifdef IRESOURCEMANAGER_IMPLEMENT
34template<typename K, typename T> IResourceManager<K, T>::~IResourceManager()
35{
36 for (auto it = _map.begin(); it != _map.end(); ++it) delete it->second;
37}
38
39template<typename K, typename T> T *IResourceManager<K, T>::GetResource(const K &key)
40{
41 auto it = _map.find(key);
42 if (it == _map.end()) {
43 if (LoadResourceByKey(key) < 0) {
44 SetErrMsg("Resource does not exist and unable to load by name");
45 return nullptr;
46 }
47 it = _map.find(key);
48 }
49 return it->second;
50}
51
52template<typename K, typename T> bool IResourceManager<K, T>::HasResource(const K &key) const { return _map.find(key) != _map.end(); }
53
54template<typename K, typename T> bool IResourceManager<K, T>::HasResource(const T *resource) const
55{
56 for (auto it = _map.begin(); it != _map.end(); ++it)
57 if (it->second == resource) return true;
58 return false;
59}
60
61template<typename K, typename T> bool IResourceManager<K, T>::AddResource(const K &key, T *resource)
62{
63 if (HasResource(key) || HasResource(resource)) {
64 VAssert(!"Resource already exists");
65 return false;
66 }
67 _map.insert(std::pair<K, T *>(key, resource));
68 return true;
69}
70
71template<typename K, typename T> void IResourceManager<K, T>::DeleteResource(const K &key)
72{
73 VAssert(HasResource(key));
74 delete _map[key];
75 _map.erase(key);
76}
77#endif
78
79} // namespace VAPoR
#define VAssert(expr)
Definition: VAssert.h:9
std::map< K, T * > _map
bool HasResource(const T *resource) const
bool HasResource(const K &key) const
bool AddResource(const K &key, T *resource)
void DeleteResource(const K &key)
T * GetResource(const K &key)
virtual int LoadResourceByKey(const K &key)=0
Wasp base class.
Definition: MyBase.h:67
#define RENDER_API
Definition: common.h:78