All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
SceneModelResourceRepository.h
1 // Copyright eeGeo Ltd (2012-2014), All Rights Reserved
2 
3 #pragma once
4 
5 #include "Types.h"
6 #include "ISceneModelResourceProvider.h"
7 #include <string>
8 #include <map>
9 #include <vector>
10 
11 #define LOG_SCENEMODEL_RESOURCE_REPO_DEBUG_MESSAGES
12 
13 #if defined (LOG_SCENEMODEL_RESOURCE_REPO_DEBUG_MESSAGES)
14 #define DEBUG_LOG Eegeo_TTY
15 #else
16 #define DEBUG_LOG if (1) {} else Eegeo_TTY
17 #endif
18 
19 namespace Eegeo
20 {
21  namespace Rendering
22  {
23  namespace SceneModels
24  {
25  template <typename TResourceType, typename TResourceInternalType>
27  {
28  public:
30  {
31  ReleaseUnusedResources();
32  }
33 
34  void Insert(const std::string& name, TResourceInternalType* pMesh)
35  {
36  typename std::map<std::string, TResourceType*>::iterator value = m_resourceMap.find(name);
37  Eegeo_ASSERT(value == m_resourceMap.end(), "Already have a mesh resource: %s", name.c_str());
38  if (value == m_resourceMap.end())
39  {
40  TResourceType* sceneMeshResoure = Eegeo_NEW(TResourceType)(name, pMesh);
41  m_resourceMap[name] = sceneMeshResoure;
42  }
43  }
44 
45  TResourceType& UseResource(const std::string& name)
46  {
47  typename std::map<std::string, TResourceType*>::iterator value = m_resourceMap.find(name);
48  Eegeo_ASSERT(value != m_resourceMap.end(), "Cannot find resource: %s", name.c_str());
49  value->second->IncrementReferenceCount();
50  return *value->second;
51  }
52 
53  bool HasResource(const std::string& name) const
54  {
55  typename std::map<std::string, TResourceType*>::const_iterator value = m_resourceMap.find(name);
56  return value != m_resourceMap.end();
57  }
58 
59  void ReleaseUnusedResources()
60  {
61  std::vector<std::string> resourcesToErase;
62  for(typename std::map<std::string, TResourceType*>::iterator it = m_resourceMap.begin(); it != m_resourceMap.end(); it++)
63  {
64  if (!it->second->IsReferenced())
65  {
66  Eegeo_DELETE it->second;
67  resourcesToErase.push_back(it->first);
68  }
69  }
70 
71  for(std::vector<std::string>::iterator it = resourcesToErase.begin(); it != resourcesToErase.end(); ++it)
72  {
73  DEBUG_LOG("Deleting scene model resource: %s\n", (*it).c_str());
74  m_resourceMap.erase(*it);
75  }
76  }
77 
78 
79  protected:
80 
81  std::map<std::string, TResourceType*> m_resourceMap;
82  };
83  }
84  }
85 }