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

Last change on this file since 1350 was 1334, checked in by yushan, 7 years ago

omp_dev

  • 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: 15.2 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,
[1328]26      //boost::shared_ptr<T> > > CObjectTemplate<T>::AllMapObj;
27      boost::shared_ptr<T> > > *CObjectTemplate<T>::AllMapObj_ptr = 0;
[219]28
29   template <class T>
30      xios_map<StdString,
[1328]31      //std::vector<boost::shared_ptr<T> > > CObjectTemplate<T>::AllVectObj;
32      std::vector<boost::shared_ptr<T> > > *CObjectTemplate<T>::AllVectObj_ptr = 0;
[219]33
34   template <class T>
[1328]35      //xios_map<StdString,long int> CObjectTemplate<T>::GenId;
36      xios_map<StdString,long int> *CObjectTemplate<T>::GenId_ptr = 0;
[286]37
38   template <class T>
[219]39      CObjectTemplate<T>::CObjectTemplate(void)
[345]40         : CAttributeMap()
[219]41         , CObject()
42   { /* Ne rien faire de plus */ }
43
44   template <class T>
45      CObjectTemplate<T>::CObjectTemplate(const StdString & id)
[345]46         : CAttributeMap()
[769]47         , CObject(id, CObjectFactory::IsGenUId<T>(id))
[219]48   { /* Ne rien faire de plus */ }
49
50   template <class T>
51      CObjectTemplate<T>::CObjectTemplate
52         (const CObjectTemplate<T> & object, bool withAttrList, bool withId)
[345]53         : CAttributeMap()
[219]54         , CObject()
55   {
56      if (object.hasId() && withId)
[769]57         this->setId(object.getId(), object.hasAutoGeneratedId());
[219]58      ERROR("CObjectTemplate<T> construtor 3", << "Not completly implemented yet !");
59   }
[509]60
[219]61   template <class T>
62      CObjectTemplate<T>::~CObjectTemplate(void)
63   { /* Ne rien faire de plus */ }
64
65   ///--------------------------------------------------------------
66
67   template <class T>
68      std::vector<boost::shared_ptr<T> > &
69         CObjectTemplate<T>::GetAllVectobject(const StdString & contextId)
[509]70   {
[1328]71      //return (CObjectTemplate<T>::AllVectObj[contextId]);
72    return (CObjectTemplate<T>::AllVectObj_ptr->at(contextId));
[219]73   }
[509]74
[219]75   //---------------------------------------------------------------
[509]76
[219]77   template <class T>
78      StdString CObjectTemplate<T>::toString(void) const
79   {
80      StdOStringStream oss;
81      oss << "<" << T::GetName();
82      if (this->hasId())
83         oss << " id=\"" << this->getId() << "\"";
84      oss << " " << SuperClassMap::toString() << "/>";
85      return (oss.str());
86   }
87
88   template <class T>
89      void CObjectTemplate<T>::fromString(const StdString & str)
[509]90   {
[219]91      ERROR("CObjectTemplate<T>::fromString(str)",
[509]92            << "[ str = " << str << "] Not implemented yet !");
[219]93   }
[509]94
[219]95   //---------------------------------------------------------------
[369]96
[509]97/*
[219]98   template <class T>
99      void CObjectTemplate<T>::toBinary(StdOStream & os) const
100   {
[509]101      SuperClassMap::toBinary(os);
[219]102   }
[509]103
[219]104   template <class T>
105      void CObjectTemplate<T>::fromBinary(StdIStream & is)
106   {
[509]107      SuperClassMap::fromBinary(is);
[219]108   }
[369]109*/
110
[219]111   //---------------------------------------------------------------
112
113   template <class T>
114      void CObjectTemplate<T>::parse(xml::CXMLNode & node)
115   {
116      xml::THashAttributes attributes = node.getAttributes();
117      CAttributeMap::setAttributes(attributes);
118   }
119
120   //---------------------------------------------------------------
121
122   template <class T>
[345]123      ENodeType CObjectTemplate<T>::getType(void) const
[219]124   {
125      return (T::GetType());
126   }
[509]127
[313]128   template <class T>
129   string CObjectTemplate<T>::getName(void) const
130   {
131      return (T::GetName());
132   }
[509]133
[219]134   //---------------------------------------------------------------
135
136   template <class T>
137      bool CObjectTemplate<T>::hasChild(void) const
[509]138   {
139      return (false);
[219]140   }
141
[1105]142   /*!
143     Compare two object of same type
144   */
145   template <class T>
[1117]146   bool CObjectTemplate<T>::isEqual(const string& id, const vector<StdString>& excludedAttrs)
[1105]147   {
148      T* obj = CObjectTemplate<T>::get(id);
[1117]149      return this->isEqual(obj, excludedAttrs);
[1105]150   }
151
152   template <class T>
[1117]153   bool CObjectTemplate<T>::isEqual(T* obj, const vector<StdString>& excludedAttrs)
[1105]154   {
155
156      CAttributeMap& attrMapThis = *this;
157      CAttributeMap& attrMapObj  = *obj;
[1117]158      return (attrMapThis.isEqual(attrMapObj, excludedAttrs));
[1105]159   }
160
[219]161   //---------------------------------------------------------------
162
163   template <class T>
[445]164      void CObjectTemplate<T>::solveDescInheritance(bool apply, const CAttributeMap * const parent)
[509]165   {
[549]166      if (parent != NULL)
167         SuperClassMap::setAttributes(parent, apply);
[219]168   }
169
170   //---------------------------------------------------------------
171
172   template <class T>
173      void CObjectTemplate<T>::ClearAllAttributes(void)
174   {
[549]175      vector<T*> avect = CObjectTemplate<T>::getAll();
[347]176      typename vector<T*>::iterator
[219]177            it = avect.begin(), end = avect.end();
178
179      for (;it != end; it++)
180      {
181         CAttributeMap & amap = **it;
182         amap.clearAllAttributes();
183      }
184   }
185
[509]186   template<typename T>
[731]187   std::map<int, size_t> CObjectTemplate<T>::getMinimumBufferSizeForAttributes()
188   {
189     CContextClient* client = CContext::getCurrent()->client;
190     std::map<int, size_t> minimumSizes;
191
192     if (client->isServerLeader())
193     {
194       size_t minimumSize = 0;
195       CAttributeMap& attrMap = *this;
196       CAttributeMap::const_iterator it = attrMap.begin(), itE = attrMap.end();
197       for (; it != itE; ++it)
198       {
199         if (!it->second->isEmpty())
200         {
[735]201           size_t size = it->second->getName().size() + sizeof(size_t) + it->second->size();
[731]202           if (size > minimumSize)
203             minimumSize = size;
204         }
205       }
206
207       if (minimumSize)
208       {
209         // Account for extra header info
[735]210         minimumSize += CEventClient::headerSize + getIdServer().size() + sizeof(size_t);
[731]211
212         const std::list<int>& ranks = client->getRanksServerLeader();
213         for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank)
214           minimumSizes.insert(std::make_pair(*itRank, minimumSize));
215       }
216     }
217
218     return minimumSizes;
219   }
220
221   template<typename T>
[509]222   void CObjectTemplate<T>::sendAllAttributesToServer()
223   {
224     CAttributeMap& attrMap = *this;
225     CAttributeMap::const_iterator it = attrMap.begin(), itE = attrMap.end();
226     for (; it != itE; ++it)
227     {
228       if (!(it->second)->isEmpty()) sendAttributToServer(*(it->second));
229     }
230   }
231
[300]232   template <class T>
233   void CObjectTemplate<T>::sendAttributToServer(const string& id)
234   {
235      CAttributeMap & attrMap = *this;
[549]236      CAttribute* attr=attrMap[id];
237      sendAttributToServer(*attr);
[300]238   }
[219]239
[347]240  template <class T>
241  void CObjectTemplate<T>::sendAttributToServer(CAttribute& attr)
242  {
[549]243    CContext* context=CContext::getCurrent();
[509]244
[300]245    if (!context->hasServer)
246    {
[549]247       CContextClient* client=context->client;
[300]248
[549]249       CEventClient event(getType(),EVENT_ID_SEND_ATTRIBUTE);
[300]250       if (client->isServerLeader())
251       {
[549]252         CMessage msg;
[581]253//       msg << this->getId();
254         msg << this->getIdServer();
255         msg << attr.getName();
256         msg << attr;
[595]257         const std::list<int>& ranks = client->getRanksServerLeader();
258         for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank)
259           event.push(*itRank,1,msg);
[549]260         client->sendEvent(event);
[300]261       }
[549]262       else client->sendEvent(event);
[300]263    }
[509]264
[347]265  }
[509]266
[347]267  template <class T>
268  void CObjectTemplate<T>::recvAttributFromClient(CEventServer& event)
269  {
[509]270
[347]271    CBufferIn* buffer=event.subEvents.begin()->buffer;
272    string id,attrId;
[549]273    *buffer>>id;
[347]274    CAttributeMap & attrMap = *get(id);
[549]275    *buffer>>attrId;
276    CAttribute* attr=attrMap[attrId];
[581]277    info(50) << "attribut recu " << attrId << "  ";
278    if (attr->isEmpty()) info(50) << "--> empty" << endl;
279    else info(50) /*<attr->getValue()*/ << endl;
[549]280    *buffer>>*attr;
[581]281     info(50) << "attribut recu " << attrId << "  ";
282    if (attr->isEmpty()) info(50) << "--> empty" << endl;
283    else info(50) /*attr->getValue()*/ << endl;
[300]284  }
285
286   template <class T>
287   bool CObjectTemplate<T>::dispatchEvent(CEventServer& event)
288   {
289      switch(event.type)
290      {
291         case EVENT_ID_SEND_ATTRIBUTE :
[549]292           recvAttributFromClient(event);
293           return true;
294           break;
[509]295
[300]296         default :
[549]297         return false;
[581]298//           ERROR("void CObjectTemplate<T>::recvEvent(CEventServer& event)", << "Unknown Event");
[300]299      }
300   }
[509]301
[300]302   template <typename T>
303   bool CObjectTemplate<T>::has(const string & id)
304   {
[549]305     return CObjectFactory::HasObject<T>(id);
[300]306   }
307
308   template <typename T>
[346]309   bool CObjectTemplate<T>::has(const string& contextId, const string & id)
310   {
[549]311     return CObjectFactory::HasObject<T>(contextId,id);
[346]312   }
313
314   template <typename T>
[347]315   T* CObjectTemplate<T>::get(const string & id)
[300]316   {
[549]317     return CObjectFactory::GetObject<T>(id).get();
[300]318   }
319
320   template <typename T>
[347]321   T* CObjectTemplate<T>::get(const T* ptr)
[346]322   {
[549]323     return CObjectFactory::GetObject<T>(ptr).get();
[347]324   }
[509]325
[347]326   template <typename T>
[1334]327   boost::shared_ptr<T> CObjectTemplate<T>::getShared(const T* ptr)
[347]328   {
[549]329     return CObjectFactory::GetObject<T>(ptr);
[346]330   }
[347]331
332   template <typename T>
[1334]333   boost::shared_ptr<T> CObjectTemplate<T>::getShared(void)
[347]334   {
[549]335     return CObjectFactory::GetObject<T>((T*)this);
[347]336   }
[509]337
[346]338   template <typename T>
[347]339   const vector<T*> CObjectTemplate<T>::getAll()
[346]340   {
[1334]341     const vector< boost::shared_ptr<T> >& shared_vect= CObjectFactory::GetObjectVector<T>();
[549]342     vector<T*> vect;
[509]343
[1334]344     typename vector<boost::shared_ptr<T> >::const_iterator it;
[549]345     for(it=shared_vect.begin();it!=shared_vect.end();++it) vect.push_back(it->get());
346     return vect;
[346]347   }
348
349   template <typename T>
[347]350   const vector<T*> CObjectTemplate<T>::getAll(const string & id)
[346]351   {
[1334]352     const vector< boost::shared_ptr<T> >& shared_vect= CObjectFactory::GetObjectVector<T>(id);
[549]353     vector<T*> vect;
[509]354
[1334]355     typename vector<boost::shared_ptr<T> >::const_iterator it;
[549]356     for(it=shared_vect.begin();it!=shared_vect.end();++it) vect.push_back(it->get());
357     return vect;
[346]358   }
359
360   template <typename T>
[347]361   T* CObjectTemplate<T>::get(const string& contextId, const string & id)
[346]362   {
[549]363     return CObjectFactory::GetObject<T>(contextId,id).get();
[346]364   }
365
366   template <typename T>
[347]367   T* CObjectTemplate<T>::create(const string & id)
[300]368   {
[549]369     return CObjectFactory::CreateObject<T>(id).get();
[300]370   }   ///--------------------------------------------------------------
371
372  template <typename T>
[347]373  T* CObjectTemplate<T>::get(void)
[300]374  {
[549]375    return CObjectFactory::GetObject<T>((T*)this).get();
[300]376  }
[509]377
[313]378   template <typename T>
379   void CObjectTemplate<T>::generateCInterface(ostream& oss)
380   {
[549]381     string className=getName();
382     int found=className.rfind("_group");
383     if (found!=string::npos) className.replace(found,1,0,'x');
[509]384
[581]385     oss << "/* ************************************************************************** *" << iendl;
386     oss << " *               Interface auto generated - do not modify                     *" << iendl;
387     oss << " * ************************************************************************** */" << iendl;
388     oss << iendl;
389     oss << "#include <boost/multi_array.hpp>" << iendl;
390     oss << "#include <boost/shared_ptr.hpp>" << iendl;
[591]391     oss << "#include \"xios.hpp\"" << iendl;
[581]392     oss << "#include \"attribute_template.hpp\"" << iendl;
393     oss << "#include \"object_template.hpp\"" << iendl;
394     oss << "#include \"group_template.hpp\"" << iendl;
395     oss << "#include \"icutil.hpp\"" << iendl;
396     oss << "#include \"icdate.hpp\"" << iendl;
397     oss << "#include \"timer.hpp\"" << iendl;
398     oss << "#include \"node_type.hpp\"" << iendl;
399     oss << iendl;
400     oss << "extern \"C\"" << iendl;
401     oss << "{" << iendl++;
402     oss << "typedef xios::" << getStrType<T>() << "* " << className << "_Ptr;";
[549]403     SuperClassMap::generateCInterface(oss,className);
[581]404     oss << "}" << iendl--;
[313]405   }
[300]406
[313]407   template <typename T>
408   void CObjectTemplate<T>::generateFortran2003Interface(ostream& oss)
409   {
[549]410     string className=getName();
411     int found=className.rfind("_group");
412     if (found!=string::npos) className.replace(found,1,0,'x');
[509]413
[581]414     oss << "! * ************************************************************************** *" << iendl;
415     oss << "! *               Interface auto generated - do not modify                     *" << iendl;
416     oss << "! * ************************************************************************** *" << iendl;
417     oss << "#include \"../fortran/xios_fortran_prefix.hpp\"" << iendl;
418     oss << iendl;
419     oss << "MODULE " << className << "_interface_attr" << iendl++;
420     oss << "USE, INTRINSIC :: ISO_C_BINDING" << std::endl;
421     oss << iendl;
422     oss << "INTERFACE" << iendl++;
423     oss << "! Do not call directly / interface FORTRAN 2003 <-> C99";
[549]424     SuperClassMap::generateFortran2003Interface(oss,className);
[581]425     oss << iendl--;
426     oss << "END INTERFACE" << iendl--;
427     oss << iendl;
428     oss << "END MODULE " << className << "_interface_attr" << iendl;
[313]429   }
[509]430
[313]431   template <typename T>
432   void CObjectTemplate<T>::generateFortranInterface(ostream& oss)
433   {
[549]434     string className=getName();
435     int found=className.rfind("_group");
436     if (found!=string::npos) className.erase(found,1);
[313]437     string superClassName=getName();
[549]438     found=superClassName.find("_group");
439     if (found!=string::npos) superClassName.erase(found,6);
[509]440
[581]441     oss << "! * ************************************************************************** *" << iendl;
442     oss << "! *               Interface auto generated - do not modify                     *" << iendl;
443     oss << "! * ************************************************************************** *" << iendl;
444     oss << "#include \"xios_fortran_prefix.hpp\"" << iendl;
445     oss << iendl;
446     oss << "MODULE i" << className << "_attr" << iendl++;
447     oss << "USE, INTRINSIC :: ISO_C_BINDING" << iendl;
448     oss << "USE i" << superClassName << iendl;
449     oss << "USE " << className << "_interface_attr" << iendl--;
450//   oss << "TYPE txios(" << className << ")" << iendl;
451//   oss << "  INTEGER(kind = C_INTPTR_T) :: daddr" << iendl;
452//   oss << "END TYPE txios(" << className << ")" << iendl;
453     oss << iendl;
454     oss << "CONTAINS" << iendl;
455     oss << iendl++;
[549]456     SuperClassMap::generateFortranInterface_id(oss,className);
[581]457     oss << iendl;
[549]458     SuperClassMap::generateFortranInterface_hdl(oss,className);
[581]459     oss << iendl;
[549]460     SuperClassMap::generateFortranInterface_hdl_(oss,className);
[581]461     oss << iendl;
[549]462     SuperClassMap::generateFortranInterfaceGet_id(oss,className);
[581]463     oss << iendl;
[549]464     SuperClassMap::generateFortranInterfaceGet_hdl(oss,className);
[581]465     oss << iendl;
[549]466     SuperClassMap::generateFortranInterfaceGet_hdl_(oss,className);
[581]467     oss << iendl;
[549]468     SuperClassMap::generateFortranInterfaceIsDefined_id(oss,className);
[581]469     oss << iendl;
[549]470     SuperClassMap::generateFortranInterfaceIsDefined_hdl(oss,className);
[581]471     oss << iendl;
[549]472     SuperClassMap::generateFortranInterfaceIsDefined_hdl_(oss,className);
[581]473     oss << iendl--;
474     oss << "END MODULE i" << className << "_attr" << iendl;
[313]475   }
[335]476} // namespace xios
[219]477
[591]478#endif // __XIOS_CObjectTemplate_impl__
Note: See TracBrowser for help on using the repository browser.