source: XIOS/dev/dev_olga/src/object_template_impl.hpp @ 1027

Last change on this file since 1027 was 1025, checked in by mhnguyen, 7 years ago

Merging working version of coupler

+) Add some changes of domain and axis: Retransfer the atttributes in a generic ways for each level of client (or server)
+) Remove some spoiled files from the previous commits

Test
+) No test

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