1 | #ifndef __XIOS_CObjectFactory_impl__ |
---|
2 | #define __XIOS_CObjectFactory_impl__ |
---|
3 | |
---|
4 | #include "object_factory.hpp" |
---|
5 | |
---|
6 | namespace xios |
---|
7 | { |
---|
8 | /// ////////////////////// Définitions ////////////////////// /// |
---|
9 | template <typename U> |
---|
10 | int CObjectFactory::GetObjectNum(void) |
---|
11 | { |
---|
12 | if (CurrContext.size() == 0) |
---|
13 | ERROR("CObjectFactory::GetObjectNum(void)", |
---|
14 | << "please define current context id !"); |
---|
15 | return (U::AllVectObj[CObjectFactory::CurrContext].size()); |
---|
16 | } |
---|
17 | |
---|
18 | template <typename U> |
---|
19 | int CObjectFactory::GetObjectIdNum(void) |
---|
20 | { |
---|
21 | if (CurrContext.size() == 0) |
---|
22 | ERROR("CObjectFactory::GetObjectIdNum(void)", |
---|
23 | << "please define current context id !"); |
---|
24 | return (U::AllMapObj[CObjectFactory::CurrContext].size()); |
---|
25 | } |
---|
26 | |
---|
27 | template <typename U> |
---|
28 | bool CObjectFactory::HasObject(const StdString & id) |
---|
29 | { |
---|
30 | if (CurrContext.size() == 0) |
---|
31 | ERROR("CObjectFactory::HasObject(const StdString & id)", |
---|
32 | << "[ id = " << id << " ] please define current context id !"); |
---|
33 | return (U::AllMapObj[CObjectFactory::CurrContext].find(id) != |
---|
34 | U::AllMapObj[CObjectFactory::CurrContext].end()); |
---|
35 | } |
---|
36 | |
---|
37 | template <typename U> |
---|
38 | bool CObjectFactory::HasObject(const StdString & context, const StdString & id) |
---|
39 | { |
---|
40 | if (U::AllMapObj.find(context) == U::AllMapObj.end()) return false ; |
---|
41 | else return (U::AllMapObj[context].find(id) != U::AllMapObj[context].end()); |
---|
42 | } |
---|
43 | |
---|
44 | template <typename U> |
---|
45 | std::shared_ptr<U> CObjectFactory::GetObject(const U * const object) |
---|
46 | { |
---|
47 | if (CurrContext.size() == 0) |
---|
48 | ERROR("CObjectFactory::GetObject(const U * const object)", |
---|
49 | << "please define current context id !"); |
---|
50 | std::vector<std::shared_ptr<U> > & vect = |
---|
51 | U::AllVectObj[CObjectFactory::CurrContext]; |
---|
52 | |
---|
53 | typename std::vector<std::shared_ptr<U> >::const_iterator |
---|
54 | it = vect.begin(), end = vect.end(); |
---|
55 | |
---|
56 | for (; it != end; it++) |
---|
57 | { |
---|
58 | std::shared_ptr<U> ptr = *it; |
---|
59 | if (ptr.get() == object) |
---|
60 | return (ptr); |
---|
61 | } |
---|
62 | |
---|
63 | ERROR("CObjectFactory::GetObject(const U * const object)", |
---|
64 | << "[type = " << U::GetName() << ", adress = " << object << "] " |
---|
65 | << "object was not found."); |
---|
66 | return (std::shared_ptr<U>()); // jamais atteint |
---|
67 | } |
---|
68 | |
---|
69 | template <typename U> |
---|
70 | std::shared_ptr<U> CObjectFactory::GetObject(const StdString & id) |
---|
71 | { |
---|
72 | if (CurrContext.size() == 0) |
---|
73 | ERROR("CObjectFactory::GetObject(const StdString & id)", |
---|
74 | << "[ id = " << id << " ] please define current context id !"); |
---|
75 | if (!CObjectFactory::HasObject<U>(id)) |
---|
76 | ERROR("CObjectFactory::GetObject(const StdString & id)", |
---|
77 | << "[ id = " << id << ", U = " << U::GetName() << " ] " |
---|
78 | << "object was not found."); |
---|
79 | return (U::AllMapObj[CObjectFactory::CurrContext][id]); |
---|
80 | } |
---|
81 | |
---|
82 | template <typename U> |
---|
83 | std::shared_ptr<U> CObjectFactory::GetObject(const StdString & context, const StdString & id) |
---|
84 | { |
---|
85 | if (!CObjectFactory::HasObject<U>(context,id)) |
---|
86 | ERROR("CObjectFactory::GetObject(const StdString & id)", |
---|
87 | << "[ id = " << id << ", U = " << U::GetName() <<", context = "<<context<< " ] " |
---|
88 | << "object was not found."); |
---|
89 | return (U::AllMapObj[context][id]); |
---|
90 | } |
---|
91 | |
---|
92 | template <typename U> |
---|
93 | std::shared_ptr<U> CObjectFactory::CreateObject(const StdString& id) |
---|
94 | { |
---|
95 | if (CurrContext.empty()) |
---|
96 | ERROR("CObjectFactory::CreateObject(const StdString& id)", |
---|
97 | << "[ id = " << id << " ] please define current context id !"); |
---|
98 | |
---|
99 | if (CObjectFactory::HasObject<U>(id)) |
---|
100 | { |
---|
101 | return CObjectFactory::GetObject<U>(id); |
---|
102 | } |
---|
103 | else |
---|
104 | { |
---|
105 | std::shared_ptr<U> value(new U(id.empty() ? CObjectFactory::GenUId<U>() : id)); |
---|
106 | |
---|
107 | U::AllVectObj[CObjectFactory::CurrContext].insert(U::AllVectObj[CObjectFactory::CurrContext].end(), value); |
---|
108 | U::AllMapObj[CObjectFactory::CurrContext].insert(std::make_pair(value->getId(), value)); |
---|
109 | |
---|
110 | return value; |
---|
111 | } |
---|
112 | } |
---|
113 | |
---|
114 | template <typename U> |
---|
115 | std::shared_ptr<U> CObjectFactory::CreateAlias(const StdString& id, const StdString& alias) |
---|
116 | { |
---|
117 | if (CurrContext.empty()) |
---|
118 | ERROR("CObjectFactory::CreateAlias(const StdString& id, const StdString& alias)", |
---|
119 | << "[ id = " << id << " alias = "<<alias<<" ] please define current context id !"); |
---|
120 | |
---|
121 | if (CObjectFactory::HasObject<U>(alias)) |
---|
122 | { |
---|
123 | return CObjectFactory::GetObject<U>(alias); |
---|
124 | } |
---|
125 | else |
---|
126 | { |
---|
127 | if (! CObjectFactory::HasObject<U>(id)) |
---|
128 | { |
---|
129 | ERROR("CObjectFactory::CreateAlias(const StdString& id, const StdString& alias)", |
---|
130 | << "[ id = " << id << " alias = "<<alias<<" ] object id doesn't exist"); |
---|
131 | } |
---|
132 | else |
---|
133 | { |
---|
134 | std::shared_ptr<U> value = CObjectFactory::GetObject<U>(id); |
---|
135 | U::AllMapObj[CObjectFactory::CurrContext].insert(std::make_pair(alias, value)); |
---|
136 | return value; |
---|
137 | } |
---|
138 | } |
---|
139 | } |
---|
140 | |
---|
141 | template <typename U> |
---|
142 | const std::vector<std::shared_ptr<U> > & |
---|
143 | CObjectFactory::GetObjectVector(const StdString & context) |
---|
144 | { |
---|
145 | return (U::AllVectObj[context]); |
---|
146 | } |
---|
147 | |
---|
148 | template <typename U> |
---|
149 | const StdString CObjectFactory::GetUIdBase(void) |
---|
150 | { |
---|
151 | StdString base ; |
---|
152 | // base = "__"+CObjectFactory::CurrContext + "::" + U::GetName() + "_undef_id_"; |
---|
153 | base = CObjectFactory::CurrContext + "__" + U::GetName() + "_undef_id_"; |
---|
154 | return base; |
---|
155 | } |
---|
156 | |
---|
157 | template <typename U> |
---|
158 | StdString CObjectFactory::GenUId(void) |
---|
159 | { |
---|
160 | StdOStringStream oss; |
---|
161 | oss << GetUIdBase<U>() << U::GenId[CObjectFactory::CurrContext]++; |
---|
162 | return oss.str(); |
---|
163 | } |
---|
164 | |
---|
165 | template <typename U> |
---|
166 | bool CObjectFactory::IsGenUId(const StdString& id) |
---|
167 | { |
---|
168 | const StdString base = GetUIdBase<U>(); |
---|
169 | return (id.size() > base.size() && id.compare(0, base.size(), base) == 0); |
---|
170 | } |
---|
171 | |
---|
172 | template <typename U> |
---|
173 | void CObjectFactory::deleteContext(const StdString & context) |
---|
174 | { |
---|
175 | for (auto& v : U::AllVectObj[context]) v.reset() ; |
---|
176 | U::AllVectObj[context].clear() ; |
---|
177 | U::AllVectObj.erase(context) ; |
---|
178 | for (auto& m : U::AllMapObj[context]) m.second.reset() ; |
---|
179 | U::AllMapObj[context].clear() ; |
---|
180 | U::AllMapObj.erase(context) ; |
---|
181 | |
---|
182 | U::GenId.erase(context) ; |
---|
183 | } |
---|
184 | |
---|
185 | template <typename U> |
---|
186 | void CObjectFactory::deleteAllContexts(void) |
---|
187 | { |
---|
188 | list<StdString> contextList ; |
---|
189 | for(auto& context : U::AllMapObj) contextList.push_back(context.first) ; |
---|
190 | for(auto& context : contextList) deleteContext<U>(context) ; |
---|
191 | } |
---|
192 | |
---|
193 | |
---|
194 | template <typename U> |
---|
195 | void CObjectFactory::dumpObjects(void) |
---|
196 | { |
---|
197 | for (auto& context : U::AllMapObj) |
---|
198 | for(auto& m : context.second) |
---|
199 | { |
---|
200 | info(100)<<"Dump All Object"<<endl ; |
---|
201 | info(100)<<"Object from context "<<context.first<<" with id "<<m.first<<endl ; |
---|
202 | } |
---|
203 | } |
---|
204 | } // namespace xios |
---|
205 | |
---|
206 | #endif // __XIOS_CObjectFactory_impl__ |
---|