OSDN Git Service

fix cmake/Macros.cmake
[moflib/moflib.git] / moflib-1.0 / src / mof / ResourceManager.hpp
1 #pragma once
2 #include "mof/tstring.hpp"
3 #include <map>
4 #include <list>
5 #include <memory>
6 #include "mof/ConsoleIO.hpp"
7
8 namespace mof{
9         
10         
11
12         /** \83\8a\83\\81[\83X\82Ì\83v\81[\83\8b\81A\92x\89\84\83\8d\81[\83h\8b@\94\\82ð\92ñ\8b\9f\82·\82é\83e\83\93\83v\83\8c\81[\83g
13          *
14          *
15         */
16         template <class T >
17         class ResourceManager
18         {
19                 typedef std::list<std::shared_ptr<ResourceManager<T > > > MANAGER_LIST;
20                 typedef std::map<mof::tstring , std::shared_ptr<T>> RESOURCE_MAP;
21                 
22
23         ResourceManager<T>* m_pParent;
24                 RESOURCE_MAP m_resources;
25                 MANAGER_LIST m_children;
26         
27                 static std::shared_ptr<T> nullPtr;
28                 //void addResouce(Resouce* pResource);
29         public:
30                 ResourceManager();
31                 virtual ~ResourceManager(void);
32
33                 std::shared_ptr<T> getResource(const tstring& name );
34                 std::shared_ptr<T> getResource(const TCHAR * const name );
35                 void addResource(const TCHAR * const name , std::shared_ptr<T>& resource);
36         std::shared_ptr< ResourceManager<T> > createChild();
37         };
38
39
40
41         template <class T>
42         std::shared_ptr<T> ResourceManager<T>::nullPtr = std::shared_ptr<T>();
43
44
45
46         /** \90e\82È\82µ\82Æ\82µ\82Ä\83I\83u\83W\83F\83N\83g\90\90¬
47          *
48          */
49         template <class T >
50         ResourceManager<T >::ResourceManager( ){
51                 m_pParent = NULL;
52         }
53
54         /** \8e©\90g\82Ì\83\8a\83\\81[\83X\82Æ\8eq\82ð\89ð\95ú\82µ\81A\90e\82Ì\8eq\83\8a\83X\83g\82©\82ç\8e©\90g\82ð\8dí\8f\9c
55          */
56         template <class T  >
57         ResourceManager<T >::~ResourceManager(){
58                 //\8e©\90g\82Ì\8aÇ\97\9d\82·\82é\83\8a\83\\81[\83X\82ð\83A\83\93\83\8d\81[\83h
59         m_resources.clear();
60
61                 //\8eq\83I\83u\83W\83F\83N\83g\82ð\8dí\8f\9c
62         m_children.clear();
63                         
64                 //\90e\82©\82ç\8e©\90g\82ð\8dí\8f\9c
65                 if(m_pParent != NULL){
66                         for(MANAGER_LIST::iterator itr = m_pParent->m_children.begin() ; itr != m_pParent->m_children.end() ; ++itr){
67                                 if(this == itr->get() ){
68                                         m_pParent->m_children.erase(itr);
69                                         return;
70                                 }
71                         }
72                 }
73         }
74
75         
76
77         /** \90V\82½\82È\8eq\82ð\92Ç\89Á\82µ\81A\95Ô\82·
78          * @return \90\90¬\82µ\82½\8eq
79          */
80         template <class T  >
81         std::shared_ptr< ResourceManager<T> > ResourceManager<T >::createChild(){
82         std::shared_ptr< ResourceManager<T > > manager(new ResourceManager<T>( ));
83                 manager->m_pParent = this;
84                 m_children.push_back(manager);
85                 return manager;
86         }
87
88         
89         /** \96Ø\8d\\91¢\91S\91Ì\82©\82ç\8ew\92è\82³\82ê\82½\96¼\91O\82Ì\83\8a\83\\81[\83X\82ð\8c\9f\8dõ\81E\8eæ\93¾<br>
90          * \8c©\82Â\82©\82ç\82È\82¯\82ê\82Î\81A\88ø\90\94\82ð\8c³\82É\90V\82½\82É\90\90¬\82·\82é\81B
91          * @param name \83\8a\83\\81[\83X\96¼
92          * @param parameter \83\8a\83\\81[\83X\8dì\90¬\8e\9e\82Ì\88ø\90\94\81i\8ew\92è\82µ\82È\82­\82Ä\82à\82æ\82¢\81
93          * @return \97v\8b\81\82³\82ê\82½\83\8a\83\\81[\83X
94          */
95         template <class T >
96         std::shared_ptr<T> ResourceManager<T >::getResource(const mof::tstring& name){
97                 //\8e©\95ª\82Ì\83\8a\83X\83g\82©\82ç\92T\82·
98                 RESOURCE_MAP::const_iterator n = m_resources.find(name);
99                 if (n != m_resources.end()){
100                         return n->second;
101                         //return (std::shared_ptr<T>&)n->second;
102                 }
103                 
104                 //\8eq\82Ì\83\8a\83X\83g\82©\82ç\92T\82·
105
106                 //\90e\82Ì\83\8a\83X\83g\82©\82ç\92T\82·
107
108                 //\82È\82¯\82ê\82Î\8dì\90¬\82·\82é
109                 std::shared_ptr<T> resource( new T( name ) );
110                 //DEBUG_PRINT(resource->getName() << _T("\82ª\90\90¬\82³\82ê\82Ü\82µ\82½") );
111                 m_resources.insert(RESOURCE_MAP::value_type(name , resource));
112                 n = m_resources.find(name);
113                 return n->second;
114                 //return (std::shared_ptr<T>&)n->second;
115         }
116
117         
118
119         template <class T >
120         std::shared_ptr<T> ResourceManager<T >::getResource(const TCHAR* const name){
121                 return getResource(mof::tstring(name));
122         }
123         
124         template <class T>
125         void ResourceManager<T>::addResource(const TCHAR* const name , std::shared_ptr<T>& resource){
126                 m_resources.insert(RESOURCE_MAP::value_type(mof::tstring(name) , resource));
127         }
128
129
130         /*template <class T , typename K >
131         std::shared_ptr<T>& ResourceManager<T , K>::findResource(mof::tstring& name , K parameter){
132
133         }*/
134
135
136 };