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

Last change on this file since 1077 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
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  }
299
300  template <class T>
301  void CObjectTemplate<T>::sendAttributToServer(CAttribute& attr, CContextClient* client)
302  {
303    CEventClient event(getType(),EVENT_ID_SEND_ATTRIBUTE);
304    if (client->isServerLeader())
305    {
306      CMessage msg;
307      msg<<this->getIdServer();
308      msg << attr.getName();
309      msg << attr;
310      const std::list<int>& ranks = client->getRanksServerLeader();
311      for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank)
312        event.push(*itRank,1,msg);
313      client->sendEvent(event);
314    }
315    else client->sendEvent(event);
316  }
317
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    {
331      // Use correct context client to send message
332      // int nbSrvPools = (context->hasServer) ? context->clientPrimServer.size() : 1;
333      int nbSrvPools = (context->hasServer) ? (context->hasClient ? context->clientPrimServer.size() : 0) : 1;
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      }
350    }
351  }
352
353  template<class T>
354  void CObjectTemplate<T>::sendAddItem(const StdString& id, int itemType, CContextClient* client)
355  {
356    typedef typename T::EEventId ItemType;
357     CEventClient event(this->getType(),ItemType(itemType));
358     if (client->isServerLeader())
359     {
360       CMessage msg;
361       msg << this->getId();
362       msg << id;
363       const std::list<int>& ranks = client->getRanksServerLeader();
364       for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank)
365         event.push(*itRank,1,msg);
366       client->sendEvent(event);
367     }
368     else client->sendEvent(event);
369  }
370
371
372  template <class T>
373  void CObjectTemplate<T>::recvAttributFromClient(CEventServer& event)
374  {
375
376    CBufferIn* buffer=event.subEvents.begin()->buffer;
377    string id,attrId;
378    *buffer>>id;
379    CAttributeMap & attrMap = *get(id);
380    *buffer>>attrId;
381    CAttribute* attr=attrMap[attrId];
382    info(50) << "attribut recu " << attrId << "  ";
383    if (attr->isEmpty()) info(50) << "--> empty" << endl;
384    else info(50) /*<attr->getValue()*/ << endl;
385    *buffer>>*attr;
386     info(50) << "attribut recu " << attrId << "  ";
387    if (attr->isEmpty()) info(50) << "--> empty" << endl;
388    else info(50) /*attr->getValue()*/ << endl;
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 :
397           recvAttributFromClient(event);
398           return true;
399           break;
400
401         default :
402         return false;
403//           ERROR("void CObjectTemplate<T>::recvEvent(CEventServer& event)", << "Unknown Event");
404      }
405   }
406
407   template <typename T>
408   bool CObjectTemplate<T>::has(const string & id)
409   {
410     return CObjectFactory::HasObject<T>(id);
411   }
412
413   template <typename T>
414   bool CObjectTemplate<T>::has(const string& contextId, const string & id)
415   {
416     return CObjectFactory::HasObject<T>(contextId,id);
417   }
418
419   template <typename T>
420   T* CObjectTemplate<T>::get(const string & id)
421   {
422     return CObjectFactory::GetObject<T>(id).get();
423   }
424
425   template <typename T>
426   T* CObjectTemplate<T>::get(const T* ptr)
427   {
428     return CObjectFactory::GetObject<T>(ptr).get();
429   }
430
431   template <typename T>
432   shared_ptr<T> CObjectTemplate<T>::getShared(const T* ptr)
433   {
434     return CObjectFactory::GetObject<T>(ptr);
435   }
436
437   template <typename T>
438   shared_ptr<T> CObjectTemplate<T>::getShared(void)
439   {
440     return CObjectFactory::GetObject<T>((T*)this);
441   }
442
443   template <typename T>
444   const vector<T*> CObjectTemplate<T>::getAll()
445   {
446     const vector< shared_ptr<T> >& shared_vect= CObjectFactory::GetObjectVector<T>();
447     vector<T*> vect;
448
449     typename vector<shared_ptr<T> >::const_iterator it;
450     for(it=shared_vect.begin();it!=shared_vect.end();++it) vect.push_back(it->get());
451     return vect;
452   }
453
454   template <typename T>
455   const vector<T*> CObjectTemplate<T>::getAll(const string & id)
456   {
457     const vector< shared_ptr<T> >& shared_vect= CObjectFactory::GetObjectVector<T>(id);
458     vector<T*> vect;
459
460     typename vector<shared_ptr<T> >::const_iterator it;
461     for(it=shared_vect.begin();it!=shared_vect.end();++it) vect.push_back(it->get());
462     return vect;
463   }
464
465   template <typename T>
466   T* CObjectTemplate<T>::get(const string& contextId, const string & id)
467   {
468     return CObjectFactory::GetObject<T>(contextId,id).get();
469   }
470
471   template <typename T>
472   T* CObjectTemplate<T>::create(const string & id)
473   {
474     return CObjectFactory::CreateObject<T>(id).get();
475   }   ///--------------------------------------------------------------
476
477  template <typename T>
478  T* CObjectTemplate<T>::get(void)
479  {
480    return CObjectFactory::GetObject<T>((T*)this).get();
481  }
482
483   template <typename T>
484   void CObjectTemplate<T>::generateCInterface(ostream& oss)
485   {
486     string className=getName();
487     int found=className.rfind("_group");
488     if (found!=string::npos) className.replace(found,1,0,'x');
489
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;
496     oss << "#include \"xios.hpp\"" << iendl;
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;";
508     SuperClassMap::generateCInterface(oss,className);
509     oss << "}" << iendl--;
510   }
511
512   template <typename T>
513   void CObjectTemplate<T>::generateFortran2003Interface(ostream& oss)
514   {
515     string className=getName();
516     int found=className.rfind("_group");
517     if (found!=string::npos) className.replace(found,1,0,'x');
518
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";
529     SuperClassMap::generateFortran2003Interface(oss,className);
530     oss << iendl--;
531     oss << "END INTERFACE" << iendl--;
532     oss << iendl;
533     oss << "END MODULE " << className << "_interface_attr" << iendl;
534   }
535
536   template <typename T>
537   void CObjectTemplate<T>::generateFortranInterface(ostream& oss)
538   {
539     string className=getName();
540     int found=className.rfind("_group");
541     if (found!=string::npos) className.erase(found,1);
542     string superClassName=getName();
543     found=superClassName.find("_group");
544     if (found!=string::npos) superClassName.erase(found,6);
545
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++;
561     SuperClassMap::generateFortranInterface_id(oss,className);
562     oss << iendl;
563     SuperClassMap::generateFortranInterface_hdl(oss,className);
564     oss << iendl;
565     SuperClassMap::generateFortranInterface_hdl_(oss,className);
566     oss << iendl;
567     SuperClassMap::generateFortranInterfaceGet_id(oss,className);
568     oss << iendl;
569     SuperClassMap::generateFortranInterfaceGet_hdl(oss,className);
570     oss << iendl;
571     SuperClassMap::generateFortranInterfaceGet_hdl_(oss,className);
572     oss << iendl;
573     SuperClassMap::generateFortranInterfaceIsDefined_id(oss,className);
574     oss << iendl;
575     SuperClassMap::generateFortranInterfaceIsDefined_hdl(oss,className);
576     oss << iendl;
577     SuperClassMap::generateFortranInterfaceIsDefined_hdl_(oss,className);
578     oss << iendl--;
579     oss << "END MODULE i" << className << "_attr" << iendl;
580   }
581} // namespace xios
582
583#endif // __XIOS_CObjectTemplate_impl__
Note: See TracBrowser for help on using the repository browser.