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

Last change on this file since 1869 was 1869, checked in by ymipsl, 4 years ago

Some update...

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