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

Last change on this file since 1552 was 1545, checked in by yushan, 6 years ago

branch_openmp merged with trunk r1544

  • 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.5 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,
[1545]26      std::shared_ptr<T> > > *CObjectTemplate<T>::AllMapObj_ptr = 0;
[219]27
28   template <class T>
29      xios_map<StdString,
[1545]30      std::vector<std::shared_ptr<T> > > *CObjectTemplate<T>::AllVectObj_ptr = 0;
[219]31
32   template <class T>
[1328]33      xios_map<StdString,long int> *CObjectTemplate<T>::GenId_ptr = 0;
[286]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>
[1545]65      std::vector<std::shared_ptr<T> > &
[219]66         CObjectTemplate<T>::GetAllVectobject(const StdString & contextId)
[509]67   {
[1545]68     return (CObjectTemplate<T>::AllVectObj_ptr->at(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
[1105]138   /*!
139     Compare two object of same type
140   */
141   template <class T>
[1117]142   bool CObjectTemplate<T>::isEqual(const string& id, const vector<StdString>& excludedAttrs)
[1105]143   {
144      T* obj = CObjectTemplate<T>::get(id);
[1117]145      return this->isEqual(obj, excludedAttrs);
[1105]146   }
147
148   template <class T>
[1117]149   bool CObjectTemplate<T>::isEqual(T* obj, const vector<StdString>& excludedAttrs)
[1105]150   {
151
152      CAttributeMap& attrMapThis = *this;
153      CAttributeMap& attrMapObj  = *obj;
[1117]154      return (attrMapThis.isEqual(attrMapObj, excludedAttrs));
[1105]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>
[1460]183   std::map<int, size_t> CObjectTemplate<T>::getMinimumBufferSizeForAttributes(CContextClient* client)
[731]184   {
185     std::map<int, size_t> minimumSizes;
186
187     if (client->isServerLeader())
188     {
189       size_t minimumSize = 0;
190       CAttributeMap& attrMap = *this;
191       CAttributeMap::const_iterator it = attrMap.begin(), itE = attrMap.end();
192       for (; it != itE; ++it)
193       {
194         if (!it->second->isEmpty())
195         {
[735]196           size_t size = it->second->getName().size() + sizeof(size_t) + it->second->size();
[731]197           if (size > minimumSize)
198             minimumSize = size;
199         }
200       }
201
202       if (minimumSize)
203       {
204         // Account for extra header info
[735]205         minimumSize += CEventClient::headerSize + getIdServer().size() + sizeof(size_t);
[731]206
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));
210       }
211     }
212     return minimumSizes;
213   }
214
[1460]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     {
[1460]223       if (it->second->doSend() && !(it->second)->isEmpty()) sendAttributToServer(*(it->second));
[509]224     }
225   }
226
[1460]227   template<typename T>
228   void CObjectTemplate<T>::sendAllAttributesToServer(CContextClient* client)
229   {
230     CAttributeMap& attrMap = *this;
231     CAttributeMap::const_iterator it = attrMap.begin(), itE = attrMap.end();
232     for (; it != itE; ++it)
233     {
234       if (it->second->doSend() && !(it->second)->isEmpty()) sendAttributToServer(*(it->second), client);
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];
243      sendAttributToServer(*attr);
[300]244   }
[219]245
[1460]246   template <class T>
247   void CObjectTemplate<T>::sendAttributToServer(const string& id, CContextClient* client)
248   {
249      CAttributeMap & attrMap = *this;
250      CAttribute* attr=attrMap[id];
251      sendAttributToServer(*attr, client);
252   }
253
[347]254  template <class T>
255  void CObjectTemplate<T>::sendAttributToServer(CAttribute& attr)
256  {
[1460]257     // Use correct context client to send message
[549]258    CContext* context=CContext::getCurrent();
[1460]259    if (context->hasClient)
260    {
261      // int nbSrvPools = (context->hasServer) ? context->clientPrimServer.size() : 1;
262      int nbSrvPools = (context->hasServer) ? (context->hasClient ? context->clientPrimServer.size() : 0) : 1;
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;
270          msg<<this->getIdServer();
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      }
280    }
281  }
[509]282
[1460]283  template <class T>
284  void CObjectTemplate<T>::sendAttributToServer(CAttribute& attr, CContextClient* client)
285  {
286    CEventClient event(getType(),EVENT_ID_SEND_ATTRIBUTE);
287    if (client->isServerLeader())
[300]288    {
[1460]289      CMessage msg;
290      msg<<this->getIdServer();
291      msg << attr.getName();
292      msg << attr;
293      const std::list<int>& ranks = client->getRanksServerLeader();
294      for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank)
295        event.push(*itRank,1,msg);
296      client->sendEvent(event);
297    }
298    else client->sendEvent(event);
299  }
[300]300
[1460]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    {
314      // Use correct context client to send message
315      // int nbSrvPools = (context->hasServer) ? context->clientPrimServer.size() : 1;
316      int nbSrvPools = (context->hasServer) ? (context->hasClient ? context->clientPrimServer.size() : 0) : 1;
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      }
[300]333    }
[1460]334  }
[509]335
[1460]336  template<class T>
337  void CObjectTemplate<T>::sendAddItem(const StdString& id, int itemType, CContextClient* client)
338  {
339    typedef typename T::EEventId ItemType;
340     CEventClient event(this->getType(),ItemType(itemType));
341     if (client->isServerLeader())
342     {
343       CMessage msg;
344       msg << this->getId();
345       msg << id;
346       const std::list<int>& ranks = client->getRanksServerLeader();
347       for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank)
348         event.push(*itRank,1,msg);
349       client->sendEvent(event);
350     }
351     else client->sendEvent(event);
[347]352  }
[509]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>
[1545]414   std::shared_ptr<T> CObjectTemplate<T>::getShared(const T* ptr)
[347]415   {
[549]416     return CObjectFactory::GetObject<T>(ptr);
[346]417   }
[347]418
419   template <typename T>
[1545]420   std::shared_ptr<T> CObjectTemplate<T>::getShared(void)
[347]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   {
[1545]428     const vector< std::shared_ptr<T> >& shared_vect= CObjectFactory::GetObjectVector<T>(CObjectFactory::GetCurrentContextId());
[549]429     vector<T*> vect;
[509]430
[1545]431     typename vector<std::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   {
[1545]439     const vector< std::shared_ptr<T> >& shared_vect= CObjectFactory::GetObjectVector<T>(id);
[549]440     vector<T*> vect;
[509]441
[1545]442     typename vector<std::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;
[1545]477     oss << "#include <boostXXX/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.