New URL for NEMO forge!   http://forge.nemo-ocean.eu

Since March 2022 along with NEMO 4.2 release, the code development moved to a self-hosted GitLab.
This present forge is now archived and remained online for history.
object_template_impl.hpp in vendors/XIOS/current/src – NEMO

source: vendors/XIOS/current/src/object_template_impl.hpp @ 3408

Last change on this file since 3408 was 3408, checked in by rblod, 12 years ago

importing initial XIOS vendor drop

  • Property svn:keywords set to Id
File size: 12.1 KB
Line 
1#ifndef __XMLIO_CObjectTemplate_impl__
2#define __XMLIO_CObjectTemplate_impl__
3
4#include "xmlioserver_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
18namespace xios
19{
20   /// ////////////////////// Définitions ////////////////////// ///
21   template <class T>
22      xios_map<StdString,
23      xios_map<StdString,
24      boost::shared_ptr<T> > > CObjectTemplate<T>::AllMapObj;
25
26   template <class T>
27      xios_map<StdString,
28      std::vector<boost::shared_ptr<T> > > CObjectTemplate<T>::AllVectObj;
29
30   template <class T>
31      xios_map<StdString,long int> CObjectTemplate<T>::GenId;
32
33   template <class T>
34      CObjectTemplate<T>::CObjectTemplate(void)
35         : CAttributeMap()
36         , CObject()
37   { /* Ne rien faire de plus */ }
38
39   template <class T>
40      CObjectTemplate<T>::CObjectTemplate(const StdString & id)
41         : CAttributeMap()
42         , CObject(id)
43   { /* Ne rien faire de plus */ }
44
45   template <class T>
46      CObjectTemplate<T>::CObjectTemplate
47         (const CObjectTemplate<T> & object, bool withAttrList, bool withId)
48         : CAttributeMap()
49         , CObject()
50   {
51      if (object.hasId() && withId)
52         this->setId(object.getId());
53      ERROR("CObjectTemplate<T> construtor 3", << "Not completly implemented yet !");
54   }
55   
56   template <class T>
57      CObjectTemplate<T>::~CObjectTemplate(void)
58   { /* Ne rien faire de plus */ }
59
60   ///--------------------------------------------------------------
61
62   template <class T>
63      std::vector<boost::shared_ptr<T> > &
64         CObjectTemplate<T>::GetAllVectobject(const StdString & contextId)
65   { 
66      return (CObjectTemplate<T>::AllVectObj[contextId]); 
67   }
68   
69   //---------------------------------------------------------------
70   
71   template <class T>
72      StdString CObjectTemplate<T>::toString(void) const
73   {
74      StdOStringStream oss;
75      oss << "<" << T::GetName();
76      if (this->hasId())
77         oss << " id=\"" << this->getId() << "\"";
78      oss << " " << SuperClassMap::toString() << "/>";
79      return (oss.str());
80   }
81
82   template <class T>
83      void CObjectTemplate<T>::fromString(const StdString & str)
84   { 
85      ERROR("CObjectTemplate<T>::fromString(str)",
86            << "[ str = " << str << "] Not implemented yet !"); 
87   }
88   
89   //---------------------------------------------------------------
90   
91   template <class T>
92      void CObjectTemplate<T>::toBinary(StdOStream & os) const
93   {
94      SuperClassMap::toBinary(os);   
95   }
96     
97   template <class T>
98      void CObjectTemplate<T>::fromBinary(StdIStream & is)
99   {
100      SuperClassMap::fromBinary(is); 
101   }
102   
103   //---------------------------------------------------------------
104
105   template <class T>
106      void CObjectTemplate<T>::parse(xml::CXMLNode & node)
107   {
108      xml::THashAttributes attributes = node.getAttributes();
109      CAttributeMap::setAttributes(attributes);
110   }
111
112   //---------------------------------------------------------------
113
114   template <class T>
115      ENodeType CObjectTemplate<T>::getType(void) const
116   {
117      return (T::GetType());
118   }
119 
120   template <class T>
121   string CObjectTemplate<T>::getName(void) const
122   {
123      return (T::GetName());
124   }
125 
126   //---------------------------------------------------------------
127
128   template <class T>
129      bool CObjectTemplate<T>::hasChild(void) const
130   { 
131      return (false); 
132   }
133
134   //---------------------------------------------------------------
135
136   template <class T>
137      void CObjectTemplate<T>::solveDescInheritance(const CAttributeMap * const parent)
138   { 
139      SuperClassMap::setAttributes(parent); 
140   }
141
142   //---------------------------------------------------------------
143
144   template <class T>
145      void CObjectTemplate<T>::ClearAllAttributes(void)
146   {
147      vector<T*> avect = CObjectTemplate<T>::getAll() ;
148      typename vector<T*>::iterator
149            it = avect.begin(), end = avect.end();
150
151      for (;it != end; it++)
152      {
153         CAttributeMap & amap = **it;
154         amap.clearAllAttributes();
155      }
156   }
157
158   template <class T>
159   void CObjectTemplate<T>::sendAttributToServer(const string& id)
160   {
161      CAttributeMap & attrMap = *this;
162      CAttribute* attr=attrMap[id] ;
163      sendAttributToServer(*attr) ;
164   }
165
166  template <class T>
167  void CObjectTemplate<T>::sendAttributToServer(CAttribute& attr)
168  {
169    CContext* context=CContext::getCurrent() ;
170   
171    if (!context->hasServer)
172    {
173       CContextClient* client=context->client ;
174
175       CEventClient event(getType(),EVENT_ID_SEND_ATTRIBUTE) ;   
176       if (client->isServerLeader())
177       {
178         CMessage msg ;
179         msg<<this->getId() ;
180         msg<<attr.getName() ;
181         msg<<attr ;
182         event.push(client->getServerLeader(),1,msg) ;
183         client->sendEvent(event) ;
184       }
185       else client->sendEvent(event) ;
186    }
187     
188  }
189   
190  template <class T>
191  void CObjectTemplate<T>::recvAttributFromClient(CEventServer& event)
192  {
193     
194    CBufferIn* buffer=event.subEvents.begin()->buffer;
195    string id,attrId;
196    *buffer>>id ;
197    CAttributeMap & attrMap = *get(id);
198    *buffer>>attrId ;
199    CAttribute* attr=attrMap[attrId] ;
200    info(50)<<"attribut recu "<<attrId<<"  " ;
201    if (attr->isEmpty()) info(50)<<"--> empty"<<endl ;
202    else info(50) /*<attr->getValue()*/<<endl ;
203    *buffer>>*attr ;
204     info(50)<<"attribut recu "<<attrId<<"  " ;
205    if (attr->isEmpty()) info(50)<<"--> empty"<<endl ;
206    else info(50) /*attr->getValue()*/<<endl ;
207  }
208
209   template <class T>
210   bool CObjectTemplate<T>::dispatchEvent(CEventServer& event)
211   {
212      switch(event.type)
213      {
214         case EVENT_ID_SEND_ATTRIBUTE :
215           recvAttributFromClient(event) ;
216           return true ;
217           break ;
218       
219         default :
220         return false ;
221//           ERROR("void CObjectTemplate<T>::recvEvent(CEventServer& event)",
222//                 <<"Unknown Event") ;
223      }
224   }
225   
226   template <typename T>
227   bool CObjectTemplate<T>::has(const string & id)
228   {
229     return CObjectFactory::HasObject<T>(id) ;
230   }
231
232   template <typename T>
233   bool CObjectTemplate<T>::has(const string& contextId, const string & id)
234   {
235     return CObjectFactory::HasObject<T>(contextId,id) ;
236   }
237
238   template <typename T>
239   T* CObjectTemplate<T>::get(const string & id)
240   {
241     return CObjectFactory::GetObject<T>(id).get() ;
242   }
243
244   template <typename T>
245   T* CObjectTemplate<T>::get(const T* ptr)
246   {
247     return CObjectFactory::GetObject<T>(ptr).get() ;
248   }
249   
250   template <typename T>
251   shared_ptr<T> CObjectTemplate<T>::getShared(const T* ptr)
252   {
253     return CObjectFactory::GetObject<T>(ptr) ;
254   }
255
256   template <typename T>
257   shared_ptr<T> CObjectTemplate<T>::getShared(void)
258   {
259     return CObjectFactory::GetObject<T>((T*)this) ;
260   }
261   
262   template <typename T>
263   const vector<T*> CObjectTemplate<T>::getAll()
264   {
265     const vector< shared_ptr<T> >& shared_vect= CObjectFactory::GetObjectVector<T>();
266     vector<T*> vect ;
267   
268     typename vector<shared_ptr<T> >::const_iterator it;
269     for(it=shared_vect.begin();it!=shared_vect.end();++it) vect.push_back(it->get()) ;
270     return vect ;
271   }
272
273   template <typename T>
274   const vector<T*> CObjectTemplate<T>::getAll(const string & id)
275   {
276     const vector< shared_ptr<T> >& shared_vect= CObjectFactory::GetObjectVector<T>(id);
277     vector<T*> vect ;
278   
279     typename vector<shared_ptr<T> >::const_iterator it;
280     for(it=shared_vect.begin();it!=shared_vect.end();++it) vect.push_back(it->get()) ;
281     return vect ;
282   }
283
284   template <typename T>
285   T* CObjectTemplate<T>::get(const string& contextId, const string & id)
286   {
287     return CObjectFactory::GetObject<T>(contextId,id).get() ;
288   }
289
290   template <typename T>
291   T* CObjectTemplate<T>::create(const string & id)
292   {
293     return CObjectFactory::CreateObject<T>(id).get() ;
294   }   ///--------------------------------------------------------------
295
296  template <typename T>
297  T* CObjectTemplate<T>::get(void)
298  {
299    return CObjectFactory::GetObject<T>((T*)this).get() ;
300  }
301 
302   template <typename T>
303   void CObjectTemplate<T>::generateCInterface(ostream& oss)
304   {
305     string className=getName() ;
306     int found=className.find_first_of("_") ;
307     if (found!=string::npos) className.replace(found,1,0,'x') ;
308     
309     oss<<"/* ************************************************************************** *"<<iendl ;
310     oss<<" *               Interface auto generated - do not modify                   *"<<iendl ;
311     oss<<" * ************************************************************************** */"<<iendl;
312     oss<<iendl ;
313     oss<<"#include <boost/multi_array.hpp>"<<iendl ;
314     oss<<"#include <boost/shared_ptr.hpp>"<<iendl ;
315     oss<<"#include \"xmlioserver.hpp\""<<iendl;
316     oss<<"#include \"attribute_template_impl.hpp\""<<iendl ;
317     oss<<"#include \"object_template_impl.hpp\""<<iendl;
318     oss<<"#include \"group_template_impl.hpp\""<<iendl ;
319     oss<<"#include \"icutil.hpp\""<<iendl ;
320     oss<<"#include \"timer.hpp\""<<iendl ;
321     oss<<iendl ;
322     oss<<"extern \"C\""<<iendl ;
323     oss<<"{"<<iendl++ ;
324     oss<<"typedef xios::"<<getStrType<T>()<<"*  "<<className<<"_Ptr;"<<iendl;
325     oss<<iendl ;
326     SuperClassMap::generateCInterface(oss,className) ;
327     oss<<iendl-- ;
328     oss<<"}"<<iendl ;
329   }
330
331   template <typename T>
332   void CObjectTemplate<T>::generateFortran2003Interface(ostream& oss)
333   {
334     string className=getName() ;
335     int found=className.find_first_of("_") ;
336     if (found!=string::npos) className.replace(found,1,0,'x') ;
337     
338     oss<<"! * ************************************************************************** *"<<iendl ;
339     oss<<"! *               Interface auto generated - do not modify                     *"<<iendl ;
340     oss<<"! * ************************************************************************** *"<<iendl;
341     oss<<iendl ;
342     oss<<"MODULE "<<className<<"_interface_attr"<<iendl++ ;
343     oss<<"USE, INTRINSIC :: ISO_C_BINDING"<<iendl ;
344     oss<<iendl ;
345     oss<<"INTERFACE ! Do not call directly / interface FORTRAN 2003 <-> C99"<<iendl++ ;
346     oss<<iendl ;   
347     oss<<iendl ;
348     SuperClassMap::generateFortran2003Interface(oss,className) ;
349     oss<<"END INTERFACE"<<iendl-- ;
350     oss<<iendl-- ;
351     oss<<"END MODULE "<<className<<"_interface_attr"<<iendl ;
352   }
353 
354   template <typename T>
355   void CObjectTemplate<T>::generateFortranInterface(ostream& oss)
356   {
357     string className=getName() ;
358     int found=className.find_first_of('_') ;
359     if (found!=string::npos) className.erase(found,1) ;
360     string superClassName=getName();
361     found=superClassName.find("_group") ;
362     if (found!=string::npos) superClassName.erase(found,6) ;
363     
364     oss<<"! * ************************************************************************** *"<<iendl ;
365     oss<<"! *               Interface auto generated - do not modify                     *"<<iendl ;
366     oss<<"! * ************************************************************************** *"<<iendl;
367     oss<<"#include \"xios_fortran_prefix.hpp\""<<iendl ;
368     oss<<iendl ;
369     oss<<"MODULE i"<<className<<"_attr"<<iendl++ ;
370     oss<<"USE, INTRINSIC :: ISO_C_BINDING"<<iendl ;
371     oss<<"USE i"<<superClassName<<iendl ;
372     oss<<"USE "<<className<<"_interface_attr"<<iendl ;
373//     oss<<"TYPE txios("<<className<<")"<<iendl ;
374//     oss<<"  INTEGER(kind = C_INTPTR_T) :: daddr"<<iendl ;
375//     oss<<"END TYPE txios("<<className<<")"<<iendl ;
376     oss<<iendl-- ;
377     oss<<"CONTAINS"<<iendl++ ;
378     oss<<iendl ;
379     SuperClassMap::generateFortranInterface_id(oss,className) ;
380     oss<<iendl ;
381     SuperClassMap::generateFortranInterface_hdl(oss,className) ;
382     oss<<iendl ;
383     SuperClassMap::generateFortranInterface_hdl_(oss,className) ;
384     oss<<iendl ;
385     SuperClassMap::generateFortranInterfaceGet_id(oss,className) ;
386     oss<<iendl ;
387     SuperClassMap::generateFortranInterfaceGet_hdl(oss,className) ;
388     oss<<iendl ;
389     SuperClassMap::generateFortranInterfaceGet_hdl_(oss,className) ;
390     oss<<iendl-- ;
391     oss<<"END MODULE i"<<className<<"_attr"<<iendl ;
392   }
393
394
395} // namespace xios
396
397#endif // __XMLIO_CObjectTemplate_impl__
Note: See TracBrowser for help on using the repository browser.