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

Last change on this file since 352 was 352, checked in by ymipsl, 12 years ago
File size: 12.2 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#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)
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());
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   template <class T>
94      void CObjectTemplate<T>::toBinary(StdOStream & os) const
95   {
96      SuperClassMap::toBinary(os);   
97   }
98     
99   template <class T>
100      void CObjectTemplate<T>::fromBinary(StdIStream & is)
101   {
102      SuperClassMap::fromBinary(is); 
103   }
104   
105   //---------------------------------------------------------------
106
107   template <class T>
108      void CObjectTemplate<T>::parse(xml::CXMLNode & node)
109   {
110      xml::THashAttributes attributes = node.getAttributes();
111      CAttributeMap::setAttributes(attributes);
112   }
113
114   //---------------------------------------------------------------
115
116   template <class T>
117      ENodeType CObjectTemplate<T>::getType(void) const
118   {
119      return (T::GetType());
120   }
121 
122   template <class T>
123   string CObjectTemplate<T>::getName(void) const
124   {
125      return (T::GetName());
126   }
127 
128   //---------------------------------------------------------------
129
130   template <class T>
131      bool CObjectTemplate<T>::hasChild(void) const
132   { 
133      return (false); 
134   }
135
136   //---------------------------------------------------------------
137
138   template <class T>
139      void CObjectTemplate<T>::solveDescInheritance(const CAttributeMap * const parent)
140   { 
141      SuperClassMap::setAttributes(parent); 
142   }
143
144   //---------------------------------------------------------------
145
146   template <class T>
147      void CObjectTemplate<T>::ClearAllAttributes(void)
148   {
149      vector<T*> avect = CObjectTemplate<T>::getAll() ;
150      typename vector<T*>::iterator
151            it = avect.begin(), end = avect.end();
152
153      for (;it != end; it++)
154      {
155         CAttributeMap & amap = **it;
156         amap.clearAllAttributes();
157      }
158   }
159
160   template <class T>
161   void CObjectTemplate<T>::sendAttributToServer(const string& id)
162   {
163      CAttributeMap & attrMap = *this;
164      CAttribute* attr=attrMap[id] ;
165      sendAttributToServer(*attr) ;
166   }
167
168  template <class T>
169  void CObjectTemplate<T>::sendAttributToServer(CAttribute& attr)
170  {
171    CContext* context=CContext::getCurrent() ;
172   
173    if (!context->hasServer)
174    {
175       CContextClient* client=context->client ;
176
177       CEventClient event(getType(),EVENT_ID_SEND_ATTRIBUTE) ;   
178       if (client->isServerLeader())
179       {
180         CMessage msg ;
181         msg<<this->getId() ;
182         msg<<attr.getName() ;
183         msg<<attr ;
184         event.push(client->getServerLeader(),1,msg) ;
185         client->sendEvent(event) ;
186       }
187       else client->sendEvent(event) ;
188    }
189     
190  }
191   
192  template <class T>
193  void CObjectTemplate<T>::recvAttributFromClient(CEventServer& event)
194  {
195     
196    CBufferIn* buffer=event.subEvents.begin()->buffer;
197    string id,attrId;
198    *buffer>>id ;
199    CAttributeMap & attrMap = *get(id);
200    *buffer>>attrId ;
201    CAttribute* attr=attrMap[attrId] ;
202    info(50)<<"attribut recu "<<attrId<<"  " ;
203    if (attr->isEmpty()) info(50)<<"--> empty"<<endl ;
204    else info(50) /*<attr->getValue()*/<<endl ;
205    *buffer>>*attr ;
206     info(50)<<"attribut recu "<<attrId<<"  " ;
207    if (attr->isEmpty()) info(50)<<"--> empty"<<endl ;
208    else info(50) /*attr->getValue()*/<<endl ;
209  }
210
211   template <class T>
212   bool CObjectTemplate<T>::dispatchEvent(CEventServer& event)
213   {
214      switch(event.type)
215      {
216         case EVENT_ID_SEND_ATTRIBUTE :
217           recvAttributFromClient(event) ;
218           return true ;
219           break ;
220       
221         default :
222         return false ;
223//           ERROR("void CObjectTemplate<T>::recvEvent(CEventServer& event)",
224//                 <<"Unknown Event") ;
225      }
226   }
227   
228   template <typename T>
229   bool CObjectTemplate<T>::has(const string & id)
230   {
231     return CObjectFactory::HasObject<T>(id) ;
232   }
233
234   template <typename T>
235   bool CObjectTemplate<T>::has(const string& contextId, const string & id)
236   {
237     return CObjectFactory::HasObject<T>(contextId,id) ;
238   }
239
240   template <typename T>
241   T* CObjectTemplate<T>::get(const string & id)
242   {
243     return CObjectFactory::GetObject<T>(id).get() ;
244   }
245
246   template <typename T>
247   T* CObjectTemplate<T>::get(const T* ptr)
248   {
249     return CObjectFactory::GetObject<T>(ptr).get() ;
250   }
251   
252   template <typename T>
253   shared_ptr<T> CObjectTemplate<T>::getShared(const T* ptr)
254   {
255     return CObjectFactory::GetObject<T>(ptr) ;
256   }
257
258   template <typename T>
259   shared_ptr<T> CObjectTemplate<T>::getShared(void)
260   {
261     return CObjectFactory::GetObject<T>((T*)this) ;
262   }
263   
264   template <typename T>
265   const vector<T*> CObjectTemplate<T>::getAll()
266   {
267     const vector< shared_ptr<T> >& shared_vect= CObjectFactory::GetObjectVector<T>();
268     vector<T*> vect ;
269   
270     typename vector<shared_ptr<T> >::const_iterator it;
271     for(it=shared_vect.begin();it!=shared_vect.end();++it) vect.push_back(it->get()) ;
272     return vect ;
273   }
274
275   template <typename T>
276   const vector<T*> CObjectTemplate<T>::getAll(const string & id)
277   {
278     const vector< shared_ptr<T> >& shared_vect= CObjectFactory::GetObjectVector<T>(id);
279     vector<T*> vect ;
280   
281     typename vector<shared_ptr<T> >::const_iterator it;
282     for(it=shared_vect.begin();it!=shared_vect.end();++it) vect.push_back(it->get()) ;
283     return vect ;
284   }
285
286   template <typename T>
287   T* CObjectTemplate<T>::get(const string& contextId, const string & id)
288   {
289     return CObjectFactory::GetObject<T>(contextId,id).get() ;
290   }
291
292   template <typename T>
293   T* CObjectTemplate<T>::create(const string & id)
294   {
295     return CObjectFactory::CreateObject<T>(id).get() ;
296   }   ///--------------------------------------------------------------
297
298  template <typename T>
299  T* CObjectTemplate<T>::get(void)
300  {
301    return CObjectFactory::GetObject<T>((T*)this).get() ;
302  }
303 
304   template <typename T>
305   void CObjectTemplate<T>::generateCInterface(ostream& oss)
306   {
307     string className=getName() ;
308     int found=className.find_first_of("_") ;
309     if (found!=string::npos) className.replace(found,1,0,'x') ;
310     
311     oss<<"/* ************************************************************************** *"<<iendl ;
312     oss<<" *               Interface auto generated - do not modify                   *"<<iendl ;
313     oss<<" * ************************************************************************** */"<<iendl;
314     oss<<iendl ;
315     oss<<"#include <boost/multi_array.hpp>"<<iendl ;
316     oss<<"#include <boost/shared_ptr.hpp>"<<iendl ;
317     oss<<"#include \"xmlioserver.hpp\""<<iendl;
318     oss<<"#include \"attribute_template.hpp\""<<iendl ;
319     oss<<"#include \"object_template.hpp\""<<iendl;
320     oss<<"#include \"group_template.hpp\""<<iendl ;
321     oss<<"#include \"icutil.hpp\""<<iendl ;
322     oss<<"#include \"timer.hpp\""<<iendl ;
323     oss<<iendl ;
324     oss<<"extern \"C\""<<iendl ;
325     oss<<"{"<<iendl++ ;
326     oss<<"typedef xios::"<<getStrType<T>()<<"*  "<<className<<"_Ptr;"<<iendl;
327     oss<<iendl ;
328     SuperClassMap::generateCInterface(oss,className) ;
329     oss<<iendl-- ;
330     oss<<"}"<<iendl ;
331   }
332
333   template <typename T>
334   void CObjectTemplate<T>::generateFortran2003Interface(ostream& oss)
335   {
336     string className=getName() ;
337     int found=className.find_first_of("_") ;
338     if (found!=string::npos) className.replace(found,1,0,'x') ;
339     
340     oss<<"! * ************************************************************************** *"<<iendl ;
341     oss<<"! *               Interface auto generated - do not modify                     *"<<iendl ;
342     oss<<"! * ************************************************************************** *"<<iendl;
343     oss<<iendl ;
344     oss<<"MODULE "<<className<<"_interface_attr"<<iendl++ ;
345     oss<<"USE, INTRINSIC :: ISO_C_BINDING"<<iendl ;
346     oss<<iendl ;
347     oss<<"INTERFACE ! Do not call directly / interface FORTRAN 2003 <-> C99"<<iendl++ ;
348     oss<<iendl ;   
349     oss<<iendl ;
350     SuperClassMap::generateFortran2003Interface(oss,className) ;
351     oss<<"END INTERFACE"<<iendl-- ;
352     oss<<iendl-- ;
353     oss<<"END MODULE "<<className<<"_interface_attr"<<iendl ;
354   }
355 
356   template <typename T>
357   void CObjectTemplate<T>::generateFortranInterface(ostream& oss)
358   {
359     string className=getName() ;
360     int found=className.find_first_of('_') ;
361     if (found!=string::npos) className.erase(found,1) ;
362     string superClassName=getName();
363     found=superClassName.find("_group") ;
364     if (found!=string::npos) superClassName.erase(found,6) ;
365     
366     oss<<"! * ************************************************************************** *"<<iendl ;
367     oss<<"! *               Interface auto generated - do not modify                     *"<<iendl ;
368     oss<<"! * ************************************************************************** *"<<iendl;
369     oss<<"#include \"xios_fortran_prefix.hpp\""<<iendl ;
370     oss<<iendl ;
371     oss<<"MODULE i"<<className<<"_attr"<<iendl++ ;
372     oss<<"USE, INTRINSIC :: ISO_C_BINDING"<<iendl ;
373     oss<<"USE i"<<superClassName<<iendl ;
374     oss<<"USE "<<className<<"_interface_attr"<<iendl ;
375//     oss<<"TYPE txios("<<className<<")"<<iendl ;
376//     oss<<"  INTEGER(kind = C_INTPTR_T) :: daddr"<<iendl ;
377//     oss<<"END TYPE txios("<<className<<")"<<iendl ;
378     oss<<iendl-- ;
379     oss<<"CONTAINS"<<iendl++ ;
380     oss<<iendl ;
381     SuperClassMap::generateFortranInterface_id(oss,className) ;
382     oss<<iendl ;
383     SuperClassMap::generateFortranInterface_hdl(oss,className) ;
384     oss<<iendl ;
385     SuperClassMap::generateFortranInterface_hdl_(oss,className) ;
386     oss<<iendl ;
387     SuperClassMap::generateFortranInterfaceGet_id(oss,className) ;
388     oss<<iendl ;
389     SuperClassMap::generateFortranInterfaceGet_hdl(oss,className) ;
390     oss<<iendl ;
391     SuperClassMap::generateFortranInterfaceGet_hdl_(oss,className) ;
392     oss<<iendl-- ;
393     oss<<"END MODULE i"<<className<<"_attr"<<iendl ;
394   }
395
396
397} // namespace xios
398
399#endif // __XMLIO_CObjectTemplate_impl__
Note: See TracBrowser for help on using the repository browser.