source: XIOS/dev/XIOS_DEV_CMIP6/src/object_template_impl.hpp @ 1627

Last change on this file since 1627 was 1330, checked in by oabramkina, 6 years ago

Adding buffer evaluation in case of reading.

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