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

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