source: XIOS/trunk/src/object_template_impl.hpp @ 930

Last change on this file since 930 was 769, checked in by rlacroix, 9 years ago

Keep track of whether an object id was automatically generated or not.

  • 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: 14.3 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   {
166     CContextClient* client = CContext::getCurrent()->client;
167     std::map<int, size_t> minimumSizes;
168
169     if (client->isServerLeader())
170     {
171       size_t minimumSize = 0;
172       CAttributeMap& attrMap = *this;
173       CAttributeMap::const_iterator it = attrMap.begin(), itE = attrMap.end();
174       for (; it != itE; ++it)
175       {
176         if (!it->second->isEmpty())
177         {
[735]178           size_t size = it->second->getName().size() + sizeof(size_t) + it->second->size();
[731]179           if (size > minimumSize)
180             minimumSize = size;
181         }
182       }
183
184       if (minimumSize)
185       {
186         // Account for extra header info
[735]187         minimumSize += CEventClient::headerSize + getIdServer().size() + sizeof(size_t);
[731]188
189         const std::list<int>& ranks = client->getRanksServerLeader();
190         for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank)
191           minimumSizes.insert(std::make_pair(*itRank, minimumSize));
192       }
193     }
194
195     return minimumSizes;
196   }
197
198   template<typename T>
[509]199   void CObjectTemplate<T>::sendAllAttributesToServer()
200   {
201     CAttributeMap& attrMap = *this;
202     CAttributeMap::const_iterator it = attrMap.begin(), itE = attrMap.end();
203     for (; it != itE; ++it)
204     {
205       if (!(it->second)->isEmpty()) sendAttributToServer(*(it->second));
206     }
207   }
208
[300]209   template <class T>
210   void CObjectTemplate<T>::sendAttributToServer(const string& id)
211   {
212      CAttributeMap & attrMap = *this;
[549]213      CAttribute* attr=attrMap[id];
214      sendAttributToServer(*attr);
[300]215   }
[219]216
[347]217  template <class T>
218  void CObjectTemplate<T>::sendAttributToServer(CAttribute& attr)
219  {
[549]220    CContext* context=CContext::getCurrent();
[509]221
[300]222    if (!context->hasServer)
223    {
[549]224       CContextClient* client=context->client;
[300]225
[549]226       CEventClient event(getType(),EVENT_ID_SEND_ATTRIBUTE);
[300]227       if (client->isServerLeader())
228       {
[549]229         CMessage msg;
[581]230//       msg << this->getId();
231         msg << this->getIdServer();
232         msg << attr.getName();
233         msg << attr;
[595]234         const std::list<int>& ranks = client->getRanksServerLeader();
235         for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank)
236           event.push(*itRank,1,msg);
[549]237         client->sendEvent(event);
[300]238       }
[549]239       else client->sendEvent(event);
[300]240    }
[509]241
[347]242  }
[509]243
[347]244  template <class T>
245  void CObjectTemplate<T>::recvAttributFromClient(CEventServer& event)
246  {
[509]247
[347]248    CBufferIn* buffer=event.subEvents.begin()->buffer;
249    string id,attrId;
[549]250    *buffer>>id;
[347]251    CAttributeMap & attrMap = *get(id);
[549]252    *buffer>>attrId;
253    CAttribute* attr=attrMap[attrId];
[581]254    info(50) << "attribut recu " << attrId << "  ";
255    if (attr->isEmpty()) info(50) << "--> empty" << endl;
256    else info(50) /*<attr->getValue()*/ << endl;
[549]257    *buffer>>*attr;
[581]258     info(50) << "attribut recu " << attrId << "  ";
259    if (attr->isEmpty()) info(50) << "--> empty" << endl;
260    else info(50) /*attr->getValue()*/ << endl;
[300]261  }
262
263   template <class T>
264   bool CObjectTemplate<T>::dispatchEvent(CEventServer& event)
265   {
266      switch(event.type)
267      {
268         case EVENT_ID_SEND_ATTRIBUTE :
[549]269           recvAttributFromClient(event);
270           return true;
271           break;
[509]272
[300]273         default :
[549]274         return false;
[581]275//           ERROR("void CObjectTemplate<T>::recvEvent(CEventServer& event)", << "Unknown Event");
[300]276      }
277   }
[509]278
[300]279   template <typename T>
280   bool CObjectTemplate<T>::has(const string & id)
281   {
[549]282     return CObjectFactory::HasObject<T>(id);
[300]283   }
284
285   template <typename T>
[346]286   bool CObjectTemplate<T>::has(const string& contextId, const string & id)
287   {
[549]288     return CObjectFactory::HasObject<T>(contextId,id);
[346]289   }
290
291   template <typename T>
[347]292   T* CObjectTemplate<T>::get(const string & id)
[300]293   {
[549]294     return CObjectFactory::GetObject<T>(id).get();
[300]295   }
296
297   template <typename T>
[347]298   T* CObjectTemplate<T>::get(const T* ptr)
[346]299   {
[549]300     return CObjectFactory::GetObject<T>(ptr).get();
[347]301   }
[509]302
[347]303   template <typename T>
304   shared_ptr<T> CObjectTemplate<T>::getShared(const T* ptr)
305   {
[549]306     return CObjectFactory::GetObject<T>(ptr);
[346]307   }
[347]308
309   template <typename T>
310   shared_ptr<T> CObjectTemplate<T>::getShared(void)
311   {
[549]312     return CObjectFactory::GetObject<T>((T*)this);
[347]313   }
[509]314
[346]315   template <typename T>
[347]316   const vector<T*> CObjectTemplate<T>::getAll()
[346]317   {
[347]318     const vector< shared_ptr<T> >& shared_vect= CObjectFactory::GetObjectVector<T>();
[549]319     vector<T*> vect;
[509]320
[347]321     typename vector<shared_ptr<T> >::const_iterator it;
[549]322     for(it=shared_vect.begin();it!=shared_vect.end();++it) vect.push_back(it->get());
323     return vect;
[346]324   }
325
326   template <typename T>
[347]327   const vector<T*> CObjectTemplate<T>::getAll(const string & id)
[346]328   {
[347]329     const vector< shared_ptr<T> >& shared_vect= CObjectFactory::GetObjectVector<T>(id);
[549]330     vector<T*> vect;
[509]331
[347]332     typename vector<shared_ptr<T> >::const_iterator it;
[549]333     for(it=shared_vect.begin();it!=shared_vect.end();++it) vect.push_back(it->get());
334     return vect;
[346]335   }
336
337   template <typename T>
[347]338   T* CObjectTemplate<T>::get(const string& contextId, const string & id)
[346]339   {
[549]340     return CObjectFactory::GetObject<T>(contextId,id).get();
[346]341   }
342
343   template <typename T>
[347]344   T* CObjectTemplate<T>::create(const string & id)
[300]345   {
[549]346     return CObjectFactory::CreateObject<T>(id).get();
[300]347   }   ///--------------------------------------------------------------
348
349  template <typename T>
[347]350  T* CObjectTemplate<T>::get(void)
[300]351  {
[549]352    return CObjectFactory::GetObject<T>((T*)this).get();
[300]353  }
[509]354
[313]355   template <typename T>
356   void CObjectTemplate<T>::generateCInterface(ostream& oss)
357   {
[549]358     string className=getName();
359     int found=className.rfind("_group");
360     if (found!=string::npos) className.replace(found,1,0,'x');
[509]361
[581]362     oss << "/* ************************************************************************** *" << iendl;
363     oss << " *               Interface auto generated - do not modify                     *" << iendl;
364     oss << " * ************************************************************************** */" << iendl;
365     oss << iendl;
366     oss << "#include <boost/multi_array.hpp>" << iendl;
367     oss << "#include <boost/shared_ptr.hpp>" << iendl;
[591]368     oss << "#include \"xios.hpp\"" << iendl;
[581]369     oss << "#include \"attribute_template.hpp\"" << iendl;
370     oss << "#include \"object_template.hpp\"" << iendl;
371     oss << "#include \"group_template.hpp\"" << iendl;
372     oss << "#include \"icutil.hpp\"" << iendl;
373     oss << "#include \"icdate.hpp\"" << iendl;
374     oss << "#include \"timer.hpp\"" << iendl;
375     oss << "#include \"node_type.hpp\"" << iendl;
376     oss << iendl;
377     oss << "extern \"C\"" << iendl;
378     oss << "{" << iendl++;
379     oss << "typedef xios::" << getStrType<T>() << "* " << className << "_Ptr;";
[549]380     SuperClassMap::generateCInterface(oss,className);
[581]381     oss << "}" << iendl--;
[313]382   }
[300]383
[313]384   template <typename T>
385   void CObjectTemplate<T>::generateFortran2003Interface(ostream& oss)
386   {
[549]387     string className=getName();
388     int found=className.rfind("_group");
389     if (found!=string::npos) className.replace(found,1,0,'x');
[509]390
[581]391     oss << "! * ************************************************************************** *" << iendl;
392     oss << "! *               Interface auto generated - do not modify                     *" << iendl;
393     oss << "! * ************************************************************************** *" << iendl;
394     oss << "#include \"../fortran/xios_fortran_prefix.hpp\"" << iendl;
395     oss << iendl;
396     oss << "MODULE " << className << "_interface_attr" << iendl++;
397     oss << "USE, INTRINSIC :: ISO_C_BINDING" << std::endl;
398     oss << iendl;
399     oss << "INTERFACE" << iendl++;
400     oss << "! Do not call directly / interface FORTRAN 2003 <-> C99";
[549]401     SuperClassMap::generateFortran2003Interface(oss,className);
[581]402     oss << iendl--;
403     oss << "END INTERFACE" << iendl--;
404     oss << iendl;
405     oss << "END MODULE " << className << "_interface_attr" << iendl;
[313]406   }
[509]407
[313]408   template <typename T>
409   void CObjectTemplate<T>::generateFortranInterface(ostream& oss)
410   {
[549]411     string className=getName();
412     int found=className.rfind("_group");
413     if (found!=string::npos) className.erase(found,1);
[313]414     string superClassName=getName();
[549]415     found=superClassName.find("_group");
416     if (found!=string::npos) superClassName.erase(found,6);
[509]417
[581]418     oss << "! * ************************************************************************** *" << iendl;
419     oss << "! *               Interface auto generated - do not modify                     *" << iendl;
420     oss << "! * ************************************************************************** *" << iendl;
421     oss << "#include \"xios_fortran_prefix.hpp\"" << iendl;
422     oss << iendl;
423     oss << "MODULE i" << className << "_attr" << iendl++;
424     oss << "USE, INTRINSIC :: ISO_C_BINDING" << iendl;
425     oss << "USE i" << superClassName << iendl;
426     oss << "USE " << className << "_interface_attr" << iendl--;
427//   oss << "TYPE txios(" << className << ")" << iendl;
428//   oss << "  INTEGER(kind = C_INTPTR_T) :: daddr" << iendl;
429//   oss << "END TYPE txios(" << className << ")" << iendl;
430     oss << iendl;
431     oss << "CONTAINS" << iendl;
432     oss << iendl++;
[549]433     SuperClassMap::generateFortranInterface_id(oss,className);
[581]434     oss << iendl;
[549]435     SuperClassMap::generateFortranInterface_hdl(oss,className);
[581]436     oss << iendl;
[549]437     SuperClassMap::generateFortranInterface_hdl_(oss,className);
[581]438     oss << iendl;
[549]439     SuperClassMap::generateFortranInterfaceGet_id(oss,className);
[581]440     oss << iendl;
[549]441     SuperClassMap::generateFortranInterfaceGet_hdl(oss,className);
[581]442     oss << iendl;
[549]443     SuperClassMap::generateFortranInterfaceGet_hdl_(oss,className);
[581]444     oss << iendl;
[549]445     SuperClassMap::generateFortranInterfaceIsDefined_id(oss,className);
[581]446     oss << iendl;
[549]447     SuperClassMap::generateFortranInterfaceIsDefined_hdl(oss,className);
[581]448     oss << iendl;
[549]449     SuperClassMap::generateFortranInterfaceIsDefined_hdl_(oss,className);
[581]450     oss << iendl--;
451     oss << "END MODULE i" << className << "_attr" << iendl;
[313]452   }
[335]453} // namespace xios
[219]454
[591]455#endif // __XIOS_CObjectTemplate_impl__
Note: See TracBrowser for help on using the repository browser.