source: XIOS/dev/dev_ym/XIOS_COUPLING/src/object_template_impl.hpp @ 1780

Last change on this file since 1780 was 1761, checked in by ymipsl, 5 years ago

implementing first guess for service functionnalities.

YM

  • 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: 18.6 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      std::shared_ptr<T> > > CObjectTemplate<T>::AllMapObj;
27
28   template <class T>
29      xios_map<StdString,
30      std::vector<std::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<std::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   TRY
112   {
113      xml::THashAttributes attributes = node.getAttributes();
114      CAttributeMap::setAttributes(attributes);
115   }
116   CATCH
117
118   //---------------------------------------------------------------
119
120   template <class T>
121      ENodeType CObjectTemplate<T>::getType(void) const
122   {
123      return (T::GetType());
124   }
125
126   template <class T>
127   string CObjectTemplate<T>::getName(void) const
128   {
129      return (T::GetName());
130   }
131
132   //---------------------------------------------------------------
133
134   template <class T>
135      bool CObjectTemplate<T>::hasChild(void) const
136   {
137      return (false);
138   }
139
140   /*!
141     Compare two object of same type
142   */
143   template <class T>
144   bool CObjectTemplate<T>::isEqual(const string& id, const vector<StdString>& excludedAttrs)
145   {
146      T* obj = CObjectTemplate<T>::get(id);
147      return this->isEqual(obj, excludedAttrs);
148   }
149
150   template <class T>
151   bool CObjectTemplate<T>::isEqual(T* obj, const vector<StdString>& excludedAttrs)
152   {
153
154      CAttributeMap& attrMapThis = *this;
155      CAttributeMap& attrMapObj  = *obj;
156      return (attrMapThis.isEqual(attrMapObj, excludedAttrs));
157   }
158
159   //---------------------------------------------------------------
160
161   template <class T>
162      void CObjectTemplate<T>::solveDescInheritance(bool apply, const CAttributeMap * const parent)
163   {
164      if (parent != NULL)
165         SuperClassMap::setAttributes(parent, apply);
166   }
167
168   //---------------------------------------------------------------
169
170   template <class T>
171      void CObjectTemplate<T>::ClearAllAttributes(void)
172   {
173      vector<T*> avect = CObjectTemplate<T>::getAll();
174      typename vector<T*>::iterator
175            it = avect.begin(), end = avect.end();
176
177      for (;it != end; it++)
178      {
179         CAttributeMap & amap = **it;
180         amap.clearAllAttributes();
181      }
182   }
183
184   template<typename T>
185   std::map<int, size_t> CObjectTemplate<T>::getMinimumBufferSizeForAttributes(CContextClient* client)
186   {
187     std::map<int, size_t> minimumSizes;
188
189     if (client->isServerLeader())
190     {
191       size_t minimumSize = 0;
192       CAttributeMap& attrMap = *this;
193       CAttributeMap::const_iterator it = attrMap.begin(), itE = attrMap.end();
194       for (; it != itE; ++it)
195       {
196         if (!it->second->isEmpty())
197         {
198           size_t size = it->second->getName().size() + sizeof(size_t) + it->second->size();
199           if (size > minimumSize)
200             minimumSize = size;
201         }
202       }
203
204       if (minimumSize)
205       {
206         // Account for extra header info
207         minimumSize += CEventClient::headerSize + getIdServer().size() + sizeof(size_t);
208
209         const std::list<int>& ranks = client->getRanksServerLeader();
210         for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank)
211           minimumSizes.insert(std::make_pair(*itRank, minimumSize));
212       }
213     }
214     return minimumSizes;
215   }
216
217
218   template<typename T>
219   void CObjectTemplate<T>::sendAllAttributesToServer()
220   {
221     CAttributeMap& attrMap = *this;
222     CAttributeMap::const_iterator it = attrMap.begin(), itE = attrMap.end();
223     for (; it != itE; ++it)
224     {
225       if (it->second->doSend() && !(it->second)->isEmpty()) sendAttributToServer(*(it->second));
226     }
227   }
228
229   template<typename T>
230   void CObjectTemplate<T>::sendAllAttributesToServer(CContextClient* client)
231   {
232     CAttributeMap& attrMap = *this;
233     CAttributeMap::const_iterator it = attrMap.begin(), itE = attrMap.end();
234     for (; it != itE; ++it)
235     {
236       if (it->second->doSend() && !(it->second)->isEmpty()) sendAttributToServer(*(it->second), client);
237     }
238   }
239
240   template <class T>
241   void CObjectTemplate<T>::sendAttributToServer(const string& id)
242   {
243      CAttributeMap & attrMap = *this;
244      CAttribute* attr=attrMap[id];
245      sendAttributToServer(*attr);
246   }
247
248   template <class T>
249   void CObjectTemplate<T>::sendAttributToServer(const string& id, CContextClient* client)
250   {
251      CAttributeMap & attrMap = *this;
252      CAttribute* attr=attrMap[id];
253      sendAttributToServer(*attr, client);
254   }
255
256  template <class T>
257  void CObjectTemplate<T>::sendAttributToServer(CAttribute& attr)
258  {
259     // Use correct context client to send message
260    CContext* context=CContext::getCurrent();
261    if (context->hasClient)
262    {
263      // int nbSrvPools = (context->hasServer) ? context->clientPrimServer.size() : 1;
264      int nbSrvPools = (context->hasServer) ? (context->hasClient ? context->clientPrimServer.size() : 0) : 1;
265      for (int i = 0; i < nbSrvPools; ++i)
266      {
267        CContextClient* contextClientTmp = (context->hasServer) ? context->clientPrimServer[i] : context->client;
268        CEventClient event(getType(),EVENT_ID_SEND_ATTRIBUTE);
269        if (contextClientTmp->isServerLeader())
270        {
271          CMessage msg;
272          if (context->hasServer)  msg<<this->getIdServer(i);
273          else  msg<<this->getIdServer(); 
274
275          msg << attr.getName();
276          msg << attr;
277          const std::list<int>& ranks = contextClientTmp->getRanksServerLeader();
278          for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank)
279            event.push(*itRank,1,msg);
280          contextClientTmp->sendEvent(event);
281        }
282        else contextClientTmp->sendEvent(event);
283      }
284    }
285  }
286
287  template <class T>
288  void CObjectTemplate<T>::sendAttributToServer(CAttribute& attr, CContextClient* client)
289  {
290    CEventClient event(getType(),EVENT_ID_SEND_ATTRIBUTE);
291    if (client->isServerLeader())
292    {
293      CMessage msg;
294      msg<<this->getIdServer(); // pb with context attribute -> to check : for now seem to be never used for context...
295      msg << attr.getName();
296      msg << attr;
297      const std::list<int>& ranks = client->getRanksServerLeader();
298      for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank)
299        event.push(*itRank,1,msg);
300      client->sendEvent(event);
301    }
302    else client->sendEvent(event);
303  }
304
305  /*!
306    This generic funtion only provides instance for sending, for receving, each
307    child class must define itself.
308    \param [in] id Id of added item
309    \param [in] itemType type of added item
310  */
311  template<class T>
312  void CObjectTemplate<T>::sendAddItem(const StdString& id, int itemType)
313  {
314    CContext* context = CContext::getCurrent();
315    typedef typename T::EEventId ItemType;
316    if (context->hasClient)
317    {
318      // Use correct context client to send message
319      // int nbSrvPools = (context->hasServer) ? context->clientPrimServer.size() : 1;
320      int nbSrvPools = (context->hasServer) ? (context->hasClient ? context->clientPrimServer.size() : 0) : 1;
321      for (int i = 0; i < nbSrvPools; ++i)
322      {
323         CContextClient* contextClientTmp = (context->hasServer) ? context->clientPrimServer[i] : context->client;
324         CEventClient event(this->getType(),ItemType(itemType));
325         if (contextClientTmp->isServerLeader())
326         {
327           CMessage msg;
328           msg << this->getId();
329           msg << id;
330           const std::list<int>& ranks = contextClientTmp->getRanksServerLeader();
331           for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank)
332             event.push(*itRank,1,msg);
333           contextClientTmp->sendEvent(event);
334         }
335         else contextClientTmp->sendEvent(event);
336      }
337    }
338  }
339
340  template<class T>
341  void CObjectTemplate<T>::sendAddItem(const StdString& id, int itemType, CContextClient* client)
342  {
343    typedef typename T::EEventId ItemType;
344     CEventClient event(this->getType(),ItemType(itemType));
345     if (client->isServerLeader())
346     {
347       CMessage msg;
348       msg << this->getId();
349       msg << id;
350       const std::list<int>& ranks = client->getRanksServerLeader();
351       for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank)
352         event.push(*itRank,1,msg);
353       client->sendEvent(event);
354     }
355     else client->sendEvent(event);
356  }
357
358  template <class T>
359  void CObjectTemplate<T>::recvAttributFromClient(CEventServer& event)
360  {
361
362    CBufferIn* buffer=event.subEvents.begin()->buffer;
363    string id,attrId;
364    *buffer>>id;
365    CAttributeMap & attrMap = *get(id);
366    *buffer>>attrId;
367    CAttribute* attr=attrMap[attrId];
368    info(50) << "attribut recu " << attrId << "  ";
369    if (attr->isEmpty()) info(50) << "--> empty" << endl;
370    else info(50) /*<attr->getValue()*/ << endl;
371    *buffer>>*attr;
372     info(50) << "attribut recu " << attrId << "  ";
373    if (attr->isEmpty()) info(50) << "--> empty" << endl;
374    else info(50) /*attr->getValue()*/ << endl;
375  }
376
377   template <class T>
378   bool CObjectTemplate<T>::dispatchEvent(CEventServer& event)
379   {
380      switch(event.type)
381      {
382         case EVENT_ID_SEND_ATTRIBUTE :
383           recvAttributFromClient(event);
384           return true;
385           break;
386
387         default :
388         return false;
389//           ERROR("void CObjectTemplate<T>::recvEvent(CEventServer& event)", << "Unknown Event");
390      }
391   }
392
393   template <typename T>
394   bool CObjectTemplate<T>::has(const string & id)
395   {
396     return CObjectFactory::HasObject<T>(id);
397   }
398
399   template <typename T>
400   bool CObjectTemplate<T>::has(const string& contextId, const string & id)
401   {
402     return CObjectFactory::HasObject<T>(contextId,id);
403   }
404
405   template <typename T>
406   T* CObjectTemplate<T>::get(const string & id)
407   {
408     return CObjectFactory::GetObject<T>(id).get();
409   }
410
411   template <typename T>
412   T* CObjectTemplate<T>::get(const T* ptr)
413   {
414     return CObjectFactory::GetObject<T>(ptr).get();
415   }
416
417   template <typename T>
418   std::shared_ptr<T> CObjectTemplate<T>::getShared(const T* ptr)
419   {
420     return CObjectFactory::GetObject<T>(ptr);
421   }
422
423   template <typename T>
424   std::shared_ptr<T> CObjectTemplate<T>::getShared(void)
425   {
426     return CObjectFactory::GetObject<T>((T*)this);
427   }
428
429   template <typename T>
430   const vector<T*> CObjectTemplate<T>::getAll()
431   {
432     const vector< std::shared_ptr<T> >& shared_vect= CObjectFactory::GetObjectVector<T>();
433     vector<T*> vect;
434
435     typename vector<std::shared_ptr<T> >::const_iterator it;
436     for(it=shared_vect.begin();it!=shared_vect.end();++it) vect.push_back(it->get());
437     return vect;
438   }
439
440   template <typename T>
441   const vector<T*> CObjectTemplate<T>::getAll(const string & id)
442   {
443     const vector< std::shared_ptr<T> >& shared_vect= CObjectFactory::GetObjectVector<T>(id);
444     vector<T*> vect;
445
446     typename vector<std::shared_ptr<T> >::const_iterator it;
447     for(it=shared_vect.begin();it!=shared_vect.end();++it) vect.push_back(it->get());
448     return vect;
449   }
450
451   template <typename T>
452   T* CObjectTemplate<T>::get(const string& contextId, const string & id)
453   {
454     return CObjectFactory::GetObject<T>(contextId,id).get();
455   }
456
457   template <typename T>
458   T* CObjectTemplate<T>::create(const string & id)
459   {
460     return CObjectFactory::CreateObject<T>(id).get();
461   }   ///--------------------------------------------------------------
462
463  template <typename T>
464  T* CObjectTemplate<T>::get(void)
465  {
466    return CObjectFactory::GetObject<T>((T*)this).get();
467  }
468
469   template <typename T>
470   void CObjectTemplate<T>::generateCInterface(ostream& oss)
471   {
472     string className=getName();
473     int found=className.rfind("_group");
474     if (found!=string::npos) className.replace(found,1,0,'x');
475
476     oss << "/* ************************************************************************** *" << iendl;
477     oss << " *               Interface auto generated - do not modify                     *" << iendl;
478     oss << " * ************************************************************************** */" << iendl;
479     oss << iendl;
480     oss << "#include <boost/multi_array.hpp>" << iendl;
481     oss << "#include \"xios.hpp\"" << iendl;
482     oss << "#include \"attribute_template.hpp\"" << iendl;
483     oss << "#include \"object_template.hpp\"" << iendl;
484     oss << "#include \"group_template.hpp\"" << iendl;
485     oss << "#include \"icutil.hpp\"" << iendl;
486     oss << "#include \"icdate.hpp\"" << iendl;
487     oss << "#include \"timer.hpp\"" << iendl;
488     oss << "#include \"node_type.hpp\"" << iendl;
489     oss << iendl;
490     oss << "extern \"C\"" << iendl;
491     oss << "{" << iendl++;
492     oss << "typedef xios::" << getStrType<T>() << "* " << className << "_Ptr;";
493     SuperClassMap::generateCInterface(oss,className);
494     oss << "}" << iendl--;
495   }
496
497   template <typename T>
498   void CObjectTemplate<T>::generateFortran2003Interface(ostream& oss)
499   {
500     string className=getName();
501     int found=className.rfind("_group");
502     if (found!=string::npos) className.replace(found,1,0,'x');
503
504     oss << "! * ************************************************************************** *" << iendl;
505     oss << "! *               Interface auto generated - do not modify                     *" << iendl;
506     oss << "! * ************************************************************************** *" << iendl;
507     oss << "#include \"../fortran/xios_fortran_prefix.hpp\"" << iendl;
508     oss << iendl;
509     oss << "MODULE " << className << "_interface_attr" << iendl++;
510     oss << "USE, INTRINSIC :: ISO_C_BINDING" << std::endl;
511     oss << iendl;
512     oss << "INTERFACE" << iendl++;
513     oss << "! Do not call directly / interface FORTRAN 2003 <-> C99";
514     SuperClassMap::generateFortran2003Interface(oss, className);
515     oss << iendl--;
516     oss << "END INTERFACE" << iendl--;
517     oss << iendl;
518     oss << "END MODULE " << className << "_interface_attr" << iendl;
519   }
520
521   template <typename T>
522   void CObjectTemplate<T>::generateFortranInterface(ostream& oss)
523   {
524     string className=getName();
525     int found=className.rfind("_group");
526     if (found!=string::npos) className.erase(found,1);
527     string superClassName=getName();
528     found=superClassName.find("_group");
529     if (found!=string::npos) superClassName.erase(found,6);
530
531     oss << "! * ************************************************************************** *" << iendl;
532     oss << "! *               Interface auto generated - do not modify                     *" << iendl;
533     oss << "! * ************************************************************************** *" << iendl;
534     oss << "#include \"xios_fortran_prefix.hpp\"" << iendl;
535     oss << iendl;
536     oss << "MODULE i" << className << "_attr" << iendl++;
537     oss << "USE, INTRINSIC :: ISO_C_BINDING" << iendl;
538     oss << "USE i" << superClassName << iendl;
539     oss << "USE " << className << "_interface_attr" << iendl--;
540//   oss << "TYPE txios(" << className << ")" << iendl;
541//   oss << "  INTEGER(kind = C_INTPTR_T) :: daddr" << iendl;
542//   oss << "END TYPE txios(" << className << ")" << iendl;
543     oss << iendl;
544     oss << "CONTAINS" << iendl;
545     oss << iendl++;
546     SuperClassMap::generateFortranInterface_id(oss,className);
547     oss << iendl;
548     SuperClassMap::generateFortranInterface_hdl(oss,className);
549     oss << iendl;
550     SuperClassMap::generateFortranInterface_hdl_(oss,className);
551     oss << iendl;
552     SuperClassMap::generateFortranInterfaceGet_id(oss,className);
553     oss << iendl;
554     SuperClassMap::generateFortranInterfaceGet_hdl(oss,className);
555     oss << iendl;
556     SuperClassMap::generateFortranInterfaceGet_hdl_(oss,className);
557     oss << iendl;
558     SuperClassMap::generateFortranInterfaceIsDefined_id(oss,className);
559     oss << iendl;
560     SuperClassMap::generateFortranInterfaceIsDefined_hdl(oss,className);
561     oss << iendl;
562     SuperClassMap::generateFortranInterfaceIsDefined_hdl_(oss,className);
563     oss << iendl--;
564     oss << "END MODULE i" << className << "_attr" << iendl;
565   }
566} // namespace xios
567
568#endif // __XIOS_CObjectTemplate_impl__
Note: See TracBrowser for help on using the repository browser.