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

Last change on this file since 445 was 445, checked in by ymipsl, 11 years ago

Add possibility to make inheritance of attributes and reference before closing the context definition.
New fortran fonction : xios_solve inheritance()
After this call, the value of attribute have the inherited value of their parent.

YM

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