source: XIOS/dev/branch_openmp/src/object_template_impl.hpp @ 1348

Last change on this file since 1348 was 1334, checked in by yushan, 6 years ago

omp_dev

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