source: XIOS/trunk/src/object_template_impl.hpp @ 878

Last change on this file since 878 was 769, checked in by rlacroix, 8 years ago

Keep track of whether an object id was automatically generated or not.

  • Property copyright set to
    Software name : XIOS (Xml I/O Server)
    http://forge.ipsl.jussieu.fr/ioserver
    Creation date : January 2009
    Licence : CeCCIL version2
    see license file in root directory : Licence_CeCILL_V2-en.txt
    or http://www.cecill.info/licences/Licence_CeCILL_V2-en.html
    Holder : CEA/LSCE (Laboratoire des Sciences du CLimat et de l'Environnement)
    CNRS/IPSL (Institut Pierre Simon Laplace)
    Project Manager : Yann Meurdesoif
    yann.meurdesoif@cea.fr
File size: 14.3 KB
Line 
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
20namespace 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
140   template <class T>
141      void CObjectTemplate<T>::solveDescInheritance(bool apply, const CAttributeMap * const parent)
142   {
143      if (parent != NULL)
144         SuperClassMap::setAttributes(parent, apply);
145   }
146
147   //---------------------------------------------------------------
148
149   template <class T>
150      void CObjectTemplate<T>::ClearAllAttributes(void)
151   {
152      vector<T*> avect = CObjectTemplate<T>::getAll();
153      typename vector<T*>::iterator
154            it = avect.begin(), end = avect.end();
155
156      for (;it != end; it++)
157      {
158         CAttributeMap & amap = **it;
159         amap.clearAllAttributes();
160      }
161   }
162
163   template<typename T>
164   std::map<int, size_t> CObjectTemplate<T>::getMinimumBufferSizeForAttributes()
165   {
166     CContextClient* client = CContext::getCurrent()->client;
167     std::map<int, size_t> minimumSizes;
168
169     if (client->isServerLeader())
170     {
171       size_t minimumSize = 0;
172       CAttributeMap& attrMap = *this;
173       CAttributeMap::const_iterator it = attrMap.begin(), itE = attrMap.end();
174       for (; it != itE; ++it)
175       {
176         if (!it->second->isEmpty())
177         {
178           size_t size = it->second->getName().size() + sizeof(size_t) + it->second->size();
179           if (size > minimumSize)
180             minimumSize = size;
181         }
182       }
183
184       if (minimumSize)
185       {
186         // Account for extra header info
187         minimumSize += CEventClient::headerSize + getIdServer().size() + sizeof(size_t);
188
189         const std::list<int>& ranks = client->getRanksServerLeader();
190         for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank)
191           minimumSizes.insert(std::make_pair(*itRank, minimumSize));
192       }
193     }
194
195     return minimumSizes;
196   }
197
198   template<typename T>
199   void CObjectTemplate<T>::sendAllAttributesToServer()
200   {
201     CAttributeMap& attrMap = *this;
202     CAttributeMap::const_iterator it = attrMap.begin(), itE = attrMap.end();
203     for (; it != itE; ++it)
204     {
205       if (!(it->second)->isEmpty()) sendAttributToServer(*(it->second));
206     }
207   }
208
209   template <class T>
210   void CObjectTemplate<T>::sendAttributToServer(const string& id)
211   {
212      CAttributeMap & attrMap = *this;
213      CAttribute* attr=attrMap[id];
214      sendAttributToServer(*attr);
215   }
216
217  template <class T>
218  void CObjectTemplate<T>::sendAttributToServer(CAttribute& attr)
219  {
220    CContext* context=CContext::getCurrent();
221
222    if (!context->hasServer)
223    {
224       CContextClient* client=context->client;
225
226       CEventClient event(getType(),EVENT_ID_SEND_ATTRIBUTE);
227       if (client->isServerLeader())
228       {
229         CMessage msg;
230//       msg << this->getId();
231         msg << this->getIdServer();
232         msg << attr.getName();
233         msg << attr;
234         const std::list<int>& ranks = client->getRanksServerLeader();
235         for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank)
236           event.push(*itRank,1,msg);
237         client->sendEvent(event);
238       }
239       else client->sendEvent(event);
240    }
241
242  }
243
244  template <class T>
245  void CObjectTemplate<T>::recvAttributFromClient(CEventServer& event)
246  {
247
248    CBufferIn* buffer=event.subEvents.begin()->buffer;
249    string id,attrId;
250    *buffer>>id;
251    CAttributeMap & attrMap = *get(id);
252    *buffer>>attrId;
253    CAttribute* attr=attrMap[attrId];
254    info(50) << "attribut recu " << attrId << "  ";
255    if (attr->isEmpty()) info(50) << "--> empty" << endl;
256    else info(50) /*<attr->getValue()*/ << endl;
257    *buffer>>*attr;
258     info(50) << "attribut recu " << attrId << "  ";
259    if (attr->isEmpty()) info(50) << "--> empty" << endl;
260    else info(50) /*attr->getValue()*/ << endl;
261  }
262
263   template <class T>
264   bool CObjectTemplate<T>::dispatchEvent(CEventServer& event)
265   {
266      switch(event.type)
267      {
268         case EVENT_ID_SEND_ATTRIBUTE :
269           recvAttributFromClient(event);
270           return true;
271           break;
272
273         default :
274         return false;
275//           ERROR("void CObjectTemplate<T>::recvEvent(CEventServer& event)", << "Unknown Event");
276      }
277   }
278
279   template <typename T>
280   bool CObjectTemplate<T>::has(const string & id)
281   {
282     return CObjectFactory::HasObject<T>(id);
283   }
284
285   template <typename T>
286   bool CObjectTemplate<T>::has(const string& contextId, const string & id)
287   {
288     return CObjectFactory::HasObject<T>(contextId,id);
289   }
290
291   template <typename T>
292   T* CObjectTemplate<T>::get(const string & id)
293   {
294     return CObjectFactory::GetObject<T>(id).get();
295   }
296
297   template <typename T>
298   T* CObjectTemplate<T>::get(const T* ptr)
299   {
300     return CObjectFactory::GetObject<T>(ptr).get();
301   }
302
303   template <typename T>
304   shared_ptr<T> CObjectTemplate<T>::getShared(const T* ptr)
305   {
306     return CObjectFactory::GetObject<T>(ptr);
307   }
308
309   template <typename T>
310   shared_ptr<T> CObjectTemplate<T>::getShared(void)
311   {
312     return CObjectFactory::GetObject<T>((T*)this);
313   }
314
315   template <typename T>
316   const vector<T*> CObjectTemplate<T>::getAll()
317   {
318     const vector< shared_ptr<T> >& shared_vect= CObjectFactory::GetObjectVector<T>();
319     vector<T*> vect;
320
321     typename vector<shared_ptr<T> >::const_iterator it;
322     for(it=shared_vect.begin();it!=shared_vect.end();++it) vect.push_back(it->get());
323     return vect;
324   }
325
326   template <typename T>
327   const vector<T*> CObjectTemplate<T>::getAll(const string & id)
328   {
329     const vector< shared_ptr<T> >& shared_vect= CObjectFactory::GetObjectVector<T>(id);
330     vector<T*> vect;
331
332     typename vector<shared_ptr<T> >::const_iterator it;
333     for(it=shared_vect.begin();it!=shared_vect.end();++it) vect.push_back(it->get());
334     return vect;
335   }
336
337   template <typename T>
338   T* CObjectTemplate<T>::get(const string& contextId, const string & id)
339   {
340     return CObjectFactory::GetObject<T>(contextId,id).get();
341   }
342
343   template <typename T>
344   T* CObjectTemplate<T>::create(const string & id)
345   {
346     return CObjectFactory::CreateObject<T>(id).get();
347   }   ///--------------------------------------------------------------
348
349  template <typename T>
350  T* CObjectTemplate<T>::get(void)
351  {
352    return CObjectFactory::GetObject<T>((T*)this).get();
353  }
354
355   template <typename T>
356   void CObjectTemplate<T>::generateCInterface(ostream& oss)
357   {
358     string className=getName();
359     int found=className.rfind("_group");
360     if (found!=string::npos) className.replace(found,1,0,'x');
361
362     oss << "/* ************************************************************************** *" << iendl;
363     oss << " *               Interface auto generated - do not modify                     *" << iendl;
364     oss << " * ************************************************************************** */" << iendl;
365     oss << iendl;
366     oss << "#include <boost/multi_array.hpp>" << iendl;
367     oss << "#include <boost/shared_ptr.hpp>" << iendl;
368     oss << "#include \"xios.hpp\"" << iendl;
369     oss << "#include \"attribute_template.hpp\"" << iendl;
370     oss << "#include \"object_template.hpp\"" << iendl;
371     oss << "#include \"group_template.hpp\"" << iendl;
372     oss << "#include \"icutil.hpp\"" << iendl;
373     oss << "#include \"icdate.hpp\"" << iendl;
374     oss << "#include \"timer.hpp\"" << iendl;
375     oss << "#include \"node_type.hpp\"" << iendl;
376     oss << iendl;
377     oss << "extern \"C\"" << iendl;
378     oss << "{" << iendl++;
379     oss << "typedef xios::" << getStrType<T>() << "* " << className << "_Ptr;";
380     SuperClassMap::generateCInterface(oss,className);
381     oss << "}" << iendl--;
382   }
383
384   template <typename T>
385   void CObjectTemplate<T>::generateFortran2003Interface(ostream& oss)
386   {
387     string className=getName();
388     int found=className.rfind("_group");
389     if (found!=string::npos) className.replace(found,1,0,'x');
390
391     oss << "! * ************************************************************************** *" << iendl;
392     oss << "! *               Interface auto generated - do not modify                     *" << iendl;
393     oss << "! * ************************************************************************** *" << iendl;
394     oss << "#include \"../fortran/xios_fortran_prefix.hpp\"" << iendl;
395     oss << iendl;
396     oss << "MODULE " << className << "_interface_attr" << iendl++;
397     oss << "USE, INTRINSIC :: ISO_C_BINDING" << std::endl;
398     oss << iendl;
399     oss << "INTERFACE" << iendl++;
400     oss << "! Do not call directly / interface FORTRAN 2003 <-> C99";
401     SuperClassMap::generateFortran2003Interface(oss,className);
402     oss << iendl--;
403     oss << "END INTERFACE" << iendl--;
404     oss << iendl;
405     oss << "END MODULE " << className << "_interface_attr" << iendl;
406   }
407
408   template <typename T>
409   void CObjectTemplate<T>::generateFortranInterface(ostream& oss)
410   {
411     string className=getName();
412     int found=className.rfind("_group");
413     if (found!=string::npos) className.erase(found,1);
414     string superClassName=getName();
415     found=superClassName.find("_group");
416     if (found!=string::npos) superClassName.erase(found,6);
417
418     oss << "! * ************************************************************************** *" << iendl;
419     oss << "! *               Interface auto generated - do not modify                     *" << iendl;
420     oss << "! * ************************************************************************** *" << iendl;
421     oss << "#include \"xios_fortran_prefix.hpp\"" << iendl;
422     oss << iendl;
423     oss << "MODULE i" << className << "_attr" << iendl++;
424     oss << "USE, INTRINSIC :: ISO_C_BINDING" << iendl;
425     oss << "USE i" << superClassName << iendl;
426     oss << "USE " << className << "_interface_attr" << iendl--;
427//   oss << "TYPE txios(" << className << ")" << iendl;
428//   oss << "  INTEGER(kind = C_INTPTR_T) :: daddr" << iendl;
429//   oss << "END TYPE txios(" << className << ")" << iendl;
430     oss << iendl;
431     oss << "CONTAINS" << iendl;
432     oss << iendl++;
433     SuperClassMap::generateFortranInterface_id(oss,className);
434     oss << iendl;
435     SuperClassMap::generateFortranInterface_hdl(oss,className);
436     oss << iendl;
437     SuperClassMap::generateFortranInterface_hdl_(oss,className);
438     oss << iendl;
439     SuperClassMap::generateFortranInterfaceGet_id(oss,className);
440     oss << iendl;
441     SuperClassMap::generateFortranInterfaceGet_hdl(oss,className);
442     oss << iendl;
443     SuperClassMap::generateFortranInterfaceGet_hdl_(oss,className);
444     oss << iendl;
445     SuperClassMap::generateFortranInterfaceIsDefined_id(oss,className);
446     oss << iendl;
447     SuperClassMap::generateFortranInterfaceIsDefined_hdl(oss,className);
448     oss << iendl;
449     SuperClassMap::generateFortranInterfaceIsDefined_hdl_(oss,className);
450     oss << iendl--;
451     oss << "END MODULE i" << className << "_attr" << iendl;
452   }
453} // namespace xios
454
455#endif // __XIOS_CObjectTemplate_impl__
Note: See TracBrowser for help on using the repository browser.