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

Last change on this file since 515 was 511, checked in by mhnguyen, 10 years ago

Seperating database of context on "client" side and "server" side

+) Add one more context in contex client in case of attached mode
+) Do some minor changements to make sure everything fine in case of attached mode
+) Replace buffer group with the new options

Test
+) On Curie
+) Connection mode: Attached and seperated
+) File mode: one and multiple
+) All tests passed

  • 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: 12.8 KB
RevLine 
[219]1#ifndef __XMLIO_CObjectTemplate_impl__
2#define __XMLIO_CObjectTemplate_impl__
3
[327]4#include "xmlioserver_spl.hpp"
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()
[219]44         , CObject(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)
[345]50         : CAttributeMap()
[219]51         , CObject()
52   {
53      if (object.hasId() && withId)
54         this->setId(object.getId());
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   {
143      SuperClassMap::setAttributes(parent, apply);
[219]144   }
145
146   //---------------------------------------------------------------
147
148   template <class T>
149      void CObjectTemplate<T>::ClearAllAttributes(void)
150   {
[347]151      vector<T*> avect = CObjectTemplate<T>::getAll() ;
152      typename vector<T*>::iterator
[219]153            it = avect.begin(), end = avect.end();
154
155      for (;it != end; it++)
156      {
157         CAttributeMap & amap = **it;
158         amap.clearAllAttributes();
159      }
160   }
161
[509]162   template<typename T>
163   void CObjectTemplate<T>::sendAllAttributesToServer()
164   {
165     CAttributeMap& attrMap = *this;
166     CAttributeMap::const_iterator it = attrMap.begin(), itE = attrMap.end();
167     for (; it != itE; ++it)
168     {
169       if (!(it->second)->isEmpty()) sendAttributToServer(*(it->second));
170     }
171
172   }
173
[300]174   template <class T>
175   void CObjectTemplate<T>::sendAttributToServer(const string& id)
176   {
177      CAttributeMap & attrMap = *this;
178      CAttribute* attr=attrMap[id] ;
179      sendAttributToServer(*attr) ;
180   }
[219]181
[347]182  template <class T>
183  void CObjectTemplate<T>::sendAttributToServer(CAttribute& attr)
184  {
185    CContext* context=CContext::getCurrent() ;
[509]186
[300]187    if (!context->hasServer)
188    {
189       CContextClient* client=context->client ;
190
[509]191       CEventClient event(getType(),EVENT_ID_SEND_ATTRIBUTE) ;
[300]192       if (client->isServerLeader())
193       {
194         CMessage msg ;
[511]195//         msg<<this->getId() ;
196         msg<<this->getIdServer();
[300]197         msg<<attr.getName() ;
198         msg<<attr ;
199         event.push(client->getServerLeader(),1,msg) ;
200         client->sendEvent(event) ;
201       }
202       else client->sendEvent(event) ;
203    }
[509]204
[347]205  }
[509]206
[347]207  template <class T>
208  void CObjectTemplate<T>::recvAttributFromClient(CEventServer& event)
209  {
[509]210
[347]211    CBufferIn* buffer=event.subEvents.begin()->buffer;
212    string id,attrId;
213    *buffer>>id ;
214    CAttributeMap & attrMap = *get(id);
215    *buffer>>attrId ;
216    CAttribute* attr=attrMap[attrId] ;
217    info(50)<<"attribut recu "<<attrId<<"  " ;
218    if (attr->isEmpty()) info(50)<<"--> empty"<<endl ;
219    else info(50) /*<attr->getValue()*/<<endl ;
220    *buffer>>*attr ;
221     info(50)<<"attribut recu "<<attrId<<"  " ;
222    if (attr->isEmpty()) info(50)<<"--> empty"<<endl ;
223    else info(50) /*attr->getValue()*/<<endl ;
[300]224  }
225
226   template <class T>
227   bool CObjectTemplate<T>::dispatchEvent(CEventServer& event)
228   {
229      switch(event.type)
230      {
231         case EVENT_ID_SEND_ATTRIBUTE :
232           recvAttributFromClient(event) ;
233           return true ;
234           break ;
[509]235
[300]236         default :
237         return false ;
238//           ERROR("void CObjectTemplate<T>::recvEvent(CEventServer& event)",
239//                 <<"Unknown Event") ;
240      }
241   }
[509]242
[300]243   template <typename T>
244   bool CObjectTemplate<T>::has(const string & id)
245   {
246     return CObjectFactory::HasObject<T>(id) ;
247   }
248
249   template <typename T>
[346]250   bool CObjectTemplate<T>::has(const string& contextId, const string & id)
251   {
252     return CObjectFactory::HasObject<T>(contextId,id) ;
253   }
254
255   template <typename T>
[347]256   T* CObjectTemplate<T>::get(const string & id)
[300]257   {
[347]258     return CObjectFactory::GetObject<T>(id).get() ;
[300]259   }
260
261   template <typename T>
[347]262   T* CObjectTemplate<T>::get(const T* ptr)
[346]263   {
[347]264     return CObjectFactory::GetObject<T>(ptr).get() ;
265   }
[509]266
[347]267   template <typename T>
268   shared_ptr<T> CObjectTemplate<T>::getShared(const T* ptr)
269   {
[346]270     return CObjectFactory::GetObject<T>(ptr) ;
271   }
[347]272
273   template <typename T>
274   shared_ptr<T> CObjectTemplate<T>::getShared(void)
275   {
276     return CObjectFactory::GetObject<T>((T*)this) ;
277   }
[509]278
[346]279   template <typename T>
[347]280   const vector<T*> CObjectTemplate<T>::getAll()
[346]281   {
[347]282     const vector< shared_ptr<T> >& shared_vect= CObjectFactory::GetObjectVector<T>();
283     vector<T*> vect ;
[509]284
[347]285     typename vector<shared_ptr<T> >::const_iterator it;
286     for(it=shared_vect.begin();it!=shared_vect.end();++it) vect.push_back(it->get()) ;
287     return vect ;
[346]288   }
289
290   template <typename T>
[347]291   const vector<T*> CObjectTemplate<T>::getAll(const string & id)
[346]292   {
[347]293     const vector< shared_ptr<T> >& shared_vect= CObjectFactory::GetObjectVector<T>(id);
294     vector<T*> vect ;
[509]295
[347]296     typename vector<shared_ptr<T> >::const_iterator it;
297     for(it=shared_vect.begin();it!=shared_vect.end();++it) vect.push_back(it->get()) ;
298     return vect ;
[346]299   }
300
301   template <typename T>
[347]302   T* CObjectTemplate<T>::get(const string& contextId, const string & id)
[346]303   {
[347]304     return CObjectFactory::GetObject<T>(contextId,id).get() ;
[346]305   }
306
307   template <typename T>
[347]308   T* CObjectTemplate<T>::create(const string & id)
[300]309   {
[347]310     return CObjectFactory::CreateObject<T>(id).get() ;
[300]311   }   ///--------------------------------------------------------------
312
313  template <typename T>
[347]314  T* CObjectTemplate<T>::get(void)
[300]315  {
[347]316    return CObjectFactory::GetObject<T>((T*)this).get() ;
[300]317  }
[509]318
[313]319   template <typename T>
320   void CObjectTemplate<T>::generateCInterface(ostream& oss)
321   {
322     string className=getName() ;
323     int found=className.find_first_of("_") ;
324     if (found!=string::npos) className.replace(found,1,0,'x') ;
[509]325
[313]326     oss<<"/* ************************************************************************** *"<<iendl ;
327     oss<<" *               Interface auto generated - do not modify                   *"<<iendl ;
328     oss<<" * ************************************************************************** */"<<iendl;
329     oss<<iendl ;
330     oss<<"#include <boost/multi_array.hpp>"<<iendl ;
331     oss<<"#include <boost/shared_ptr.hpp>"<<iendl ;
332     oss<<"#include \"xmlioserver.hpp\""<<iendl;
[352]333     oss<<"#include \"attribute_template.hpp\""<<iendl ;
334     oss<<"#include \"object_template.hpp\""<<iendl;
335     oss<<"#include \"group_template.hpp\""<<iendl ;
[313]336     oss<<"#include \"icutil.hpp\""<<iendl ;
[347]337     oss<<"#include \"timer.hpp\""<<iendl ;
[369]338     oss<<"#include \"node_type.hpp\""<<iendl ;
[313]339     oss<<iendl ;
340     oss<<"extern \"C\""<<iendl ;
341     oss<<"{"<<iendl++ ;
[345]342     oss<<"typedef xios::"<<getStrType<T>()<<"*  "<<className<<"_Ptr;"<<iendl;
[313]343     oss<<iendl ;
344     SuperClassMap::generateCInterface(oss,className) ;
345     oss<<iendl-- ;
346     oss<<"}"<<iendl ;
347   }
[300]348
[313]349   template <typename T>
350   void CObjectTemplate<T>::generateFortran2003Interface(ostream& oss)
351   {
352     string className=getName() ;
353     int found=className.find_first_of("_") ;
354     if (found!=string::npos) className.replace(found,1,0,'x') ;
[509]355
[313]356     oss<<"! * ************************************************************************** *"<<iendl ;
357     oss<<"! *               Interface auto generated - do not modify                     *"<<iendl ;
358     oss<<"! * ************************************************************************** *"<<iendl;
359     oss<<iendl ;
360     oss<<"MODULE "<<className<<"_interface_attr"<<iendl++ ;
361     oss<<"USE, INTRINSIC :: ISO_C_BINDING"<<iendl ;
362     oss<<iendl ;
363     oss<<"INTERFACE ! Do not call directly / interface FORTRAN 2003 <-> C99"<<iendl++ ;
364     oss<<iendl ;
[509]365     oss<<iendl ;
[313]366     SuperClassMap::generateFortran2003Interface(oss,className) ;
367     oss<<"END INTERFACE"<<iendl-- ;
368     oss<<iendl-- ;
369     oss<<"END MODULE "<<className<<"_interface_attr"<<iendl ;
370   }
[509]371
[313]372   template <typename T>
373   void CObjectTemplate<T>::generateFortranInterface(ostream& oss)
374   {
375     string className=getName() ;
376     int found=className.find_first_of('_') ;
377     if (found!=string::npos) className.erase(found,1) ;
378     string superClassName=getName();
379     found=superClassName.find("_group") ;
380     if (found!=string::npos) superClassName.erase(found,6) ;
[509]381
[313]382     oss<<"! * ************************************************************************** *"<<iendl ;
383     oss<<"! *               Interface auto generated - do not modify                     *"<<iendl ;
384     oss<<"! * ************************************************************************** *"<<iendl;
385     oss<<"#include \"xios_fortran_prefix.hpp\""<<iendl ;
386     oss<<iendl ;
387     oss<<"MODULE i"<<className<<"_attr"<<iendl++ ;
388     oss<<"USE, INTRINSIC :: ISO_C_BINDING"<<iendl ;
389     oss<<"USE i"<<superClassName<<iendl ;
390     oss<<"USE "<<className<<"_interface_attr"<<iendl ;
391//     oss<<"TYPE txios("<<className<<")"<<iendl ;
392//     oss<<"  INTEGER(kind = C_INTPTR_T) :: daddr"<<iendl ;
393//     oss<<"END TYPE txios("<<className<<")"<<iendl ;
394     oss<<iendl-- ;
395     oss<<"CONTAINS"<<iendl++ ;
396     oss<<iendl ;
397     SuperClassMap::generateFortranInterface_id(oss,className) ;
398     oss<<iendl ;
399     SuperClassMap::generateFortranInterface_hdl(oss,className) ;
400     oss<<iendl ;
401     SuperClassMap::generateFortranInterface_hdl_(oss,className) ;
402     oss<<iendl ;
403     SuperClassMap::generateFortranInterfaceGet_id(oss,className) ;
404     oss<<iendl ;
405     SuperClassMap::generateFortranInterfaceGet_hdl(oss,className) ;
406     oss<<iendl ;
407     SuperClassMap::generateFortranInterfaceGet_hdl_(oss,className) ;
[432]408     oss<<iendl ;
409     SuperClassMap::generateFortranInterfaceIsDefined_id(oss,className) ;
410     oss<<iendl ;
411     SuperClassMap::generateFortranInterfaceIsDefined_hdl(oss,className) ;
412     oss<<iendl ;
[509]413     SuperClassMap::generateFortranInterfaceIsDefined_hdl_(oss,className) ;
[313]414     oss<<iendl-- ;
415     oss<<"END MODULE i"<<className<<"_attr"<<iendl ;
416   }
417
418
[335]419} // namespace xios
[219]420
421#endif // __XMLIO_CObjectTemplate_impl__
Note: See TracBrowser for help on using the repository browser.