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

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

Suppress access to CObjectFactory class and CTreeManager.

YM

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