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

Last change on this file since 1054 was 1054, checked in by oabramkina, 7 years ago

dev: intermediate commit.

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