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

Last change on this file since 313 was 313, checked in by ymipsl, 12 years ago

Fortran attribut interface are now automaticaly generated.
Add get attribut fonctionnality from fortran.

YM

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