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

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

Two server levels: merging with trunk r1137.
There are bugs.

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