1 | #ifndef __XIOS_CObjectTemplate_impl__ |
---|
2 | #define __XIOS_CObjectTemplate_impl__ |
---|
3 | |
---|
4 | #include "xios_spl.hpp" |
---|
5 | #include "context_client.hpp" |
---|
6 | #include "object_factory.hpp" |
---|
7 | #include "context.hpp" |
---|
8 | #include "buffer_in.hpp" |
---|
9 | #include "attribute.hpp" |
---|
10 | #include "event_client.hpp" |
---|
11 | #include "object_template.hpp" |
---|
12 | #include "context_client.hpp" |
---|
13 | #include "indent.hpp" |
---|
14 | #include "type_util.hpp" |
---|
15 | #include "message.hpp" |
---|
16 | #include "type.hpp" |
---|
17 | #include "type_util.hpp" |
---|
18 | #include "group_template.hpp" |
---|
19 | |
---|
20 | namespace xios |
---|
21 | { |
---|
22 | /// ////////////////////// Définitions ////////////////////// /// |
---|
23 | template <class T> |
---|
24 | xios_map<StdString, |
---|
25 | xios_map<StdString, |
---|
26 | boost::shared_ptr<T> > > CObjectTemplate<T>::AllMapObj; |
---|
27 | |
---|
28 | template <class T> |
---|
29 | xios_map<StdString, |
---|
30 | std::vector<boost::shared_ptr<T> > > CObjectTemplate<T>::AllVectObj; |
---|
31 | |
---|
32 | template <class T> |
---|
33 | xios_map<StdString,long int> CObjectTemplate<T>::GenId; |
---|
34 | |
---|
35 | template <class T> |
---|
36 | CObjectTemplate<T>::CObjectTemplate(void) |
---|
37 | : CAttributeMap() |
---|
38 | , CObject() |
---|
39 | { /* Ne rien faire de plus */ } |
---|
40 | |
---|
41 | template <class T> |
---|
42 | CObjectTemplate<T>::CObjectTemplate(const StdString & id) |
---|
43 | : CAttributeMap() |
---|
44 | , CObject(id, CObjectFactory::IsGenUId<T>(id)) |
---|
45 | { /* Ne rien faire de plus */ } |
---|
46 | |
---|
47 | template <class T> |
---|
48 | CObjectTemplate<T>::CObjectTemplate |
---|
49 | (const CObjectTemplate<T> & object, bool withAttrList, bool withId) |
---|
50 | : CAttributeMap() |
---|
51 | , CObject() |
---|
52 | { |
---|
53 | if (object.hasId() && withId) |
---|
54 | this->setId(object.getId(), object.hasAutoGeneratedId()); |
---|
55 | ERROR("CObjectTemplate<T> construtor 3", << "Not completly implemented yet !"); |
---|
56 | } |
---|
57 | |
---|
58 | template <class T> |
---|
59 | CObjectTemplate<T>::~CObjectTemplate(void) |
---|
60 | { /* Ne rien faire de plus */ } |
---|
61 | |
---|
62 | ///-------------------------------------------------------------- |
---|
63 | |
---|
64 | template <class T> |
---|
65 | std::vector<boost::shared_ptr<T> > & |
---|
66 | CObjectTemplate<T>::GetAllVectobject(const StdString & contextId) |
---|
67 | { |
---|
68 | return (CObjectTemplate<T>::AllVectObj[contextId]); |
---|
69 | } |
---|
70 | |
---|
71 | //--------------------------------------------------------------- |
---|
72 | |
---|
73 | template <class T> |
---|
74 | StdString CObjectTemplate<T>::toString(void) const |
---|
75 | { |
---|
76 | StdOStringStream oss; |
---|
77 | oss << "<" << T::GetName(); |
---|
78 | if (this->hasId()) |
---|
79 | oss << " id=\"" << this->getId() << "\""; |
---|
80 | oss << " " << SuperClassMap::toString() << "/>"; |
---|
81 | return (oss.str()); |
---|
82 | } |
---|
83 | |
---|
84 | template <class T> |
---|
85 | void CObjectTemplate<T>::fromString(const StdString & str) |
---|
86 | { |
---|
87 | ERROR("CObjectTemplate<T>::fromString(str)", |
---|
88 | << "[ str = " << str << "] Not implemented yet !"); |
---|
89 | } |
---|
90 | |
---|
91 | //--------------------------------------------------------------- |
---|
92 | |
---|
93 | /* |
---|
94 | template <class T> |
---|
95 | void CObjectTemplate<T>::toBinary(StdOStream & os) const |
---|
96 | { |
---|
97 | SuperClassMap::toBinary(os); |
---|
98 | } |
---|
99 | |
---|
100 | template <class T> |
---|
101 | void CObjectTemplate<T>::fromBinary(StdIStream & is) |
---|
102 | { |
---|
103 | SuperClassMap::fromBinary(is); |
---|
104 | } |
---|
105 | */ |
---|
106 | |
---|
107 | //--------------------------------------------------------------- |
---|
108 | |
---|
109 | template <class T> |
---|
110 | void CObjectTemplate<T>::parse(xml::CXMLNode & node) |
---|
111 | { |
---|
112 | xml::THashAttributes attributes = node.getAttributes(); |
---|
113 | CAttributeMap::setAttributes(attributes); |
---|
114 | } |
---|
115 | |
---|
116 | //--------------------------------------------------------------- |
---|
117 | |
---|
118 | template <class T> |
---|
119 | ENodeType CObjectTemplate<T>::getType(void) const |
---|
120 | { |
---|
121 | return (T::GetType()); |
---|
122 | } |
---|
123 | |
---|
124 | template <class T> |
---|
125 | string CObjectTemplate<T>::getName(void) const |
---|
126 | { |
---|
127 | return (T::GetName()); |
---|
128 | } |
---|
129 | |
---|
130 | //--------------------------------------------------------------- |
---|
131 | |
---|
132 | template <class T> |
---|
133 | bool CObjectTemplate<T>::hasChild(void) const |
---|
134 | { |
---|
135 | return (false); |
---|
136 | } |
---|
137 | |
---|
138 | /*! |
---|
139 | Compare two object of same type |
---|
140 | */ |
---|
141 | template <class T> |
---|
142 | bool CObjectTemplate<T>::isEqual(const string& id) |
---|
143 | { |
---|
144 | T* obj = CObjectTemplate<T>::get(id); |
---|
145 | return this->isEqual(obj); |
---|
146 | } |
---|
147 | |
---|
148 | template <class T> |
---|
149 | bool CObjectTemplate<T>::isEqual(T* obj) |
---|
150 | { |
---|
151 | |
---|
152 | CAttributeMap& attrMapThis = *this; |
---|
153 | CAttributeMap& attrMapObj = *obj; |
---|
154 | return (attrMapThis.isEqual(attrMapObj)); |
---|
155 | } |
---|
156 | |
---|
157 | //--------------------------------------------------------------- |
---|
158 | |
---|
159 | template <class T> |
---|
160 | void CObjectTemplate<T>::solveDescInheritance(bool apply, const CAttributeMap * const parent) |
---|
161 | { |
---|
162 | if (parent != NULL) |
---|
163 | SuperClassMap::setAttributes(parent, apply); |
---|
164 | } |
---|
165 | |
---|
166 | //--------------------------------------------------------------- |
---|
167 | |
---|
168 | template <class T> |
---|
169 | void CObjectTemplate<T>::ClearAllAttributes(void) |
---|
170 | { |
---|
171 | vector<T*> avect = CObjectTemplate<T>::getAll(); |
---|
172 | typename vector<T*>::iterator |
---|
173 | it = avect.begin(), end = avect.end(); |
---|
174 | |
---|
175 | for (;it != end; it++) |
---|
176 | { |
---|
177 | CAttributeMap & amap = **it; |
---|
178 | amap.clearAllAttributes(); |
---|
179 | } |
---|
180 | } |
---|
181 | |
---|
182 | template<typename T> |
---|
183 | std::map<int, size_t> CObjectTemplate<T>::getMinimumBufferSizeForAttributes() |
---|
184 | { |
---|
185 | CContextClient* client = CContext::getCurrent()->client; |
---|
186 | std::map<int, size_t> minimumSizes; |
---|
187 | |
---|
188 | if (client->isServerLeader()) |
---|
189 | { |
---|
190 | size_t minimumSize = 0; |
---|
191 | CAttributeMap& attrMap = *this; |
---|
192 | CAttributeMap::const_iterator it = attrMap.begin(), itE = attrMap.end(); |
---|
193 | for (; it != itE; ++it) |
---|
194 | { |
---|
195 | if (!it->second->isEmpty()) |
---|
196 | { |
---|
197 | size_t size = it->second->getName().size() + sizeof(size_t) + it->second->size(); |
---|
198 | if (size > minimumSize) |
---|
199 | minimumSize = size; |
---|
200 | } |
---|
201 | } |
---|
202 | |
---|
203 | if (minimumSize) |
---|
204 | { |
---|
205 | // Account for extra header info |
---|
206 | minimumSize += CEventClient::headerSize + getIdServer().size() + sizeof(size_t); |
---|
207 | |
---|
208 | const std::list<int>& ranks = client->getRanksServerLeader(); |
---|
209 | for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank) |
---|
210 | minimumSizes.insert(std::make_pair(*itRank, minimumSize)); |
---|
211 | } |
---|
212 | } |
---|
213 | |
---|
214 | return minimumSizes; |
---|
215 | } |
---|
216 | |
---|
217 | template<typename T> |
---|
218 | void CObjectTemplate<T>::sendAllAttributesToServer() |
---|
219 | { |
---|
220 | CAttributeMap& attrMap = *this; |
---|
221 | CAttributeMap::const_iterator it = attrMap.begin(), itE = attrMap.end(); |
---|
222 | for (; it != itE; ++it) |
---|
223 | { |
---|
224 | if (!(it->second)->isEmpty()) sendAttributToServer(*(it->second)); |
---|
225 | } |
---|
226 | } |
---|
227 | |
---|
228 | template <class T> |
---|
229 | void CObjectTemplate<T>::sendAttributToServer(const string& id) |
---|
230 | { |
---|
231 | CAttributeMap & attrMap = *this; |
---|
232 | CAttribute* attr=attrMap[id]; |
---|
233 | sendAttributToServer(*attr); |
---|
234 | } |
---|
235 | |
---|
236 | template <class T> |
---|
237 | void CObjectTemplate<T>::sendAttributToServer(CAttribute& attr) |
---|
238 | { |
---|
239 | CContext* context=CContext::getCurrent(); |
---|
240 | |
---|
241 | if (!context->hasServer) |
---|
242 | { |
---|
243 | CContextClient* client=context->client; |
---|
244 | |
---|
245 | CEventClient event(getType(),EVENT_ID_SEND_ATTRIBUTE); |
---|
246 | if (client->isServerLeader()) |
---|
247 | { |
---|
248 | CMessage msg; |
---|
249 | // msg << this->getId(); |
---|
250 | msg << this->getIdServer(); |
---|
251 | msg << attr.getName(); |
---|
252 | msg << attr; |
---|
253 | const std::list<int>& ranks = client->getRanksServerLeader(); |
---|
254 | for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank) |
---|
255 | event.push(*itRank,1,msg); |
---|
256 | client->sendEvent(event); |
---|
257 | } |
---|
258 | else client->sendEvent(event); |
---|
259 | } |
---|
260 | |
---|
261 | } |
---|
262 | |
---|
263 | template <class T> |
---|
264 | void CObjectTemplate<T>::recvAttributFromClient(CEventServer& event) |
---|
265 | { |
---|
266 | |
---|
267 | CBufferIn* buffer=event.subEvents.begin()->buffer; |
---|
268 | string id,attrId; |
---|
269 | *buffer>>id; |
---|
270 | CAttributeMap & attrMap = *get(id); |
---|
271 | *buffer>>attrId; |
---|
272 | CAttribute* attr=attrMap[attrId]; |
---|
273 | info(50) << "attribut recu " << attrId << " "; |
---|
274 | if (attr->isEmpty()) info(50) << "--> empty" << endl; |
---|
275 | else info(50) /*<attr->getValue()*/ << endl; |
---|
276 | *buffer>>*attr; |
---|
277 | info(50) << "attribut recu " << attrId << " "; |
---|
278 | if (attr->isEmpty()) info(50) << "--> empty" << endl; |
---|
279 | else info(50) /*attr->getValue()*/ << endl; |
---|
280 | } |
---|
281 | |
---|
282 | template <class T> |
---|
283 | bool CObjectTemplate<T>::dispatchEvent(CEventServer& event) |
---|
284 | { |
---|
285 | switch(event.type) |
---|
286 | { |
---|
287 | case EVENT_ID_SEND_ATTRIBUTE : |
---|
288 | recvAttributFromClient(event); |
---|
289 | return true; |
---|
290 | break; |
---|
291 | |
---|
292 | default : |
---|
293 | return false; |
---|
294 | // ERROR("void CObjectTemplate<T>::recvEvent(CEventServer& event)", << "Unknown Event"); |
---|
295 | } |
---|
296 | } |
---|
297 | |
---|
298 | template <typename T> |
---|
299 | bool CObjectTemplate<T>::has(const string & id) |
---|
300 | { |
---|
301 | return CObjectFactory::HasObject<T>(id); |
---|
302 | } |
---|
303 | |
---|
304 | template <typename T> |
---|
305 | bool CObjectTemplate<T>::has(const string& contextId, const string & id) |
---|
306 | { |
---|
307 | return CObjectFactory::HasObject<T>(contextId,id); |
---|
308 | } |
---|
309 | |
---|
310 | template <typename T> |
---|
311 | T* CObjectTemplate<T>::get(const string & id) |
---|
312 | { |
---|
313 | return CObjectFactory::GetObject<T>(id).get(); |
---|
314 | } |
---|
315 | |
---|
316 | template <typename T> |
---|
317 | T* CObjectTemplate<T>::get(const T* ptr) |
---|
318 | { |
---|
319 | return CObjectFactory::GetObject<T>(ptr).get(); |
---|
320 | } |
---|
321 | |
---|
322 | template <typename T> |
---|
323 | shared_ptr<T> CObjectTemplate<T>::getShared(const T* ptr) |
---|
324 | { |
---|
325 | return CObjectFactory::GetObject<T>(ptr); |
---|
326 | } |
---|
327 | |
---|
328 | template <typename T> |
---|
329 | shared_ptr<T> CObjectTemplate<T>::getShared(void) |
---|
330 | { |
---|
331 | return CObjectFactory::GetObject<T>((T*)this); |
---|
332 | } |
---|
333 | |
---|
334 | template <typename T> |
---|
335 | const vector<T*> CObjectTemplate<T>::getAll() |
---|
336 | { |
---|
337 | const vector< shared_ptr<T> >& shared_vect= CObjectFactory::GetObjectVector<T>(); |
---|
338 | vector<T*> vect; |
---|
339 | |
---|
340 | typename vector<shared_ptr<T> >::const_iterator it; |
---|
341 | for(it=shared_vect.begin();it!=shared_vect.end();++it) vect.push_back(it->get()); |
---|
342 | return vect; |
---|
343 | } |
---|
344 | |
---|
345 | template <typename T> |
---|
346 | const vector<T*> CObjectTemplate<T>::getAll(const string & id) |
---|
347 | { |
---|
348 | const vector< shared_ptr<T> >& shared_vect= CObjectFactory::GetObjectVector<T>(id); |
---|
349 | vector<T*> vect; |
---|
350 | |
---|
351 | typename vector<shared_ptr<T> >::const_iterator it; |
---|
352 | for(it=shared_vect.begin();it!=shared_vect.end();++it) vect.push_back(it->get()); |
---|
353 | return vect; |
---|
354 | } |
---|
355 | |
---|
356 | template <typename T> |
---|
357 | T* CObjectTemplate<T>::get(const string& contextId, const string & id) |
---|
358 | { |
---|
359 | return CObjectFactory::GetObject<T>(contextId,id).get(); |
---|
360 | } |
---|
361 | |
---|
362 | template <typename T> |
---|
363 | T* CObjectTemplate<T>::create(const string & id) |
---|
364 | { |
---|
365 | return CObjectFactory::CreateObject<T>(id).get(); |
---|
366 | } ///-------------------------------------------------------------- |
---|
367 | |
---|
368 | template <typename T> |
---|
369 | T* CObjectTemplate<T>::get(void) |
---|
370 | { |
---|
371 | return CObjectFactory::GetObject<T>((T*)this).get(); |
---|
372 | } |
---|
373 | |
---|
374 | template <typename T> |
---|
375 | void CObjectTemplate<T>::generateCInterface(ostream& oss) |
---|
376 | { |
---|
377 | string className=getName(); |
---|
378 | int found=className.rfind("_group"); |
---|
379 | if (found!=string::npos) className.replace(found,1,0,'x'); |
---|
380 | |
---|
381 | oss << "/* ************************************************************************** *" << iendl; |
---|
382 | oss << " * Interface auto generated - do not modify *" << iendl; |
---|
383 | oss << " * ************************************************************************** */" << iendl; |
---|
384 | oss << iendl; |
---|
385 | oss << "#include <boost/multi_array.hpp>" << iendl; |
---|
386 | oss << "#include <boost/shared_ptr.hpp>" << iendl; |
---|
387 | oss << "#include \"xios.hpp\"" << iendl; |
---|
388 | oss << "#include \"attribute_template.hpp\"" << iendl; |
---|
389 | oss << "#include \"object_template.hpp\"" << iendl; |
---|
390 | oss << "#include \"group_template.hpp\"" << iendl; |
---|
391 | oss << "#include \"icutil.hpp\"" << iendl; |
---|
392 | oss << "#include \"icdate.hpp\"" << iendl; |
---|
393 | oss << "#include \"timer.hpp\"" << iendl; |
---|
394 | oss << "#include \"node_type.hpp\"" << iendl; |
---|
395 | oss << iendl; |
---|
396 | oss << "extern \"C\"" << iendl; |
---|
397 | oss << "{" << iendl++; |
---|
398 | oss << "typedef xios::" << getStrType<T>() << "* " << className << "_Ptr;"; |
---|
399 | SuperClassMap::generateCInterface(oss,className); |
---|
400 | oss << "}" << iendl--; |
---|
401 | } |
---|
402 | |
---|
403 | template <typename T> |
---|
404 | void CObjectTemplate<T>::generateFortran2003Interface(ostream& oss) |
---|
405 | { |
---|
406 | string className=getName(); |
---|
407 | int found=className.rfind("_group"); |
---|
408 | if (found!=string::npos) className.replace(found,1,0,'x'); |
---|
409 | |
---|
410 | oss << "! * ************************************************************************** *" << iendl; |
---|
411 | oss << "! * Interface auto generated - do not modify *" << iendl; |
---|
412 | oss << "! * ************************************************************************** *" << iendl; |
---|
413 | oss << "#include \"../fortran/xios_fortran_prefix.hpp\"" << iendl; |
---|
414 | oss << iendl; |
---|
415 | oss << "MODULE " << className << "_interface_attr" << iendl++; |
---|
416 | oss << "USE, INTRINSIC :: ISO_C_BINDING" << std::endl; |
---|
417 | oss << iendl; |
---|
418 | oss << "INTERFACE" << iendl++; |
---|
419 | oss << "! Do not call directly / interface FORTRAN 2003 <-> C99"; |
---|
420 | SuperClassMap::generateFortran2003Interface(oss,className); |
---|
421 | oss << iendl--; |
---|
422 | oss << "END INTERFACE" << iendl--; |
---|
423 | oss << iendl; |
---|
424 | oss << "END MODULE " << className << "_interface_attr" << iendl; |
---|
425 | } |
---|
426 | |
---|
427 | template <typename T> |
---|
428 | void CObjectTemplate<T>::generateFortranInterface(ostream& oss) |
---|
429 | { |
---|
430 | string className=getName(); |
---|
431 | int found=className.rfind("_group"); |
---|
432 | if (found!=string::npos) className.erase(found,1); |
---|
433 | string superClassName=getName(); |
---|
434 | found=superClassName.find("_group"); |
---|
435 | if (found!=string::npos) superClassName.erase(found,6); |
---|
436 | |
---|
437 | oss << "! * ************************************************************************** *" << iendl; |
---|
438 | oss << "! * Interface auto generated - do not modify *" << iendl; |
---|
439 | oss << "! * ************************************************************************** *" << iendl; |
---|
440 | oss << "#include \"xios_fortran_prefix.hpp\"" << iendl; |
---|
441 | oss << iendl; |
---|
442 | oss << "MODULE i" << className << "_attr" << iendl++; |
---|
443 | oss << "USE, INTRINSIC :: ISO_C_BINDING" << iendl; |
---|
444 | oss << "USE i" << superClassName << iendl; |
---|
445 | oss << "USE " << className << "_interface_attr" << iendl--; |
---|
446 | // oss << "TYPE txios(" << className << ")" << iendl; |
---|
447 | // oss << " INTEGER(kind = C_INTPTR_T) :: daddr" << iendl; |
---|
448 | // oss << "END TYPE txios(" << className << ")" << iendl; |
---|
449 | oss << iendl; |
---|
450 | oss << "CONTAINS" << iendl; |
---|
451 | oss << iendl++; |
---|
452 | SuperClassMap::generateFortranInterface_id(oss,className); |
---|
453 | oss << iendl; |
---|
454 | SuperClassMap::generateFortranInterface_hdl(oss,className); |
---|
455 | oss << iendl; |
---|
456 | SuperClassMap::generateFortranInterface_hdl_(oss,className); |
---|
457 | oss << iendl; |
---|
458 | SuperClassMap::generateFortranInterfaceGet_id(oss,className); |
---|
459 | oss << iendl; |
---|
460 | SuperClassMap::generateFortranInterfaceGet_hdl(oss,className); |
---|
461 | oss << iendl; |
---|
462 | SuperClassMap::generateFortranInterfaceGet_hdl_(oss,className); |
---|
463 | oss << iendl; |
---|
464 | SuperClassMap::generateFortranInterfaceIsDefined_id(oss,className); |
---|
465 | oss << iendl; |
---|
466 | SuperClassMap::generateFortranInterfaceIsDefined_hdl(oss,className); |
---|
467 | oss << iendl; |
---|
468 | SuperClassMap::generateFortranInterfaceIsDefined_hdl_(oss,className); |
---|
469 | oss << iendl--; |
---|
470 | oss << "END MODULE i" << className << "_attr" << iendl; |
---|
471 | } |
---|
472 | } // namespace xios |
---|
473 | |
---|
474 | #endif // __XIOS_CObjectTemplate_impl__ |
---|