source: XMLIO_V2/dev/common/src/object_template_impl.hpp @ 300

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

nouvelle version de developpement de xios

  • nouvelle interface fortran
  • recodage complet de la couche de communication
  • et bien d'autres choses...

YM

File size: 6.7 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   //---------------------------------------------------------------
116
117   template <class T>
118      bool CObjectTemplate<T>::hasChild(void) const
119   { 
120      return (false); 
121   }
122
123   //---------------------------------------------------------------
124
125   template <class T>
126      void CObjectTemplate<T>::solveDescInheritance(const CAttributeMap * const parent)
127   { 
128      SuperClassMap::setAttributes(parent); 
129   }
130
131   //---------------------------------------------------------------
132
133   template <class T>
134      void CObjectTemplate<T>::ClearAllAttributes(void)
135   {
136      std::vector<boost::shared_ptr<T> > & avect =
137         CObjectTemplate<T>::GetAllVectobject(CObjectFactory::GetCurrentContextId());
138      typename std::vector<boost::shared_ptr<T> >::iterator
139            it = avect.begin(), end = avect.end();
140
141      for (;it != end; it++)
142      {
143         CAttributeMap & amap = **it;
144         amap.clearAllAttributes();
145      }
146   }
147
148   template <class T>
149   void CObjectTemplate<T>::sendAttributToServer(const string& id)
150   {
151      CAttributeMap & attrMap = *this;
152      CAttribute* attr=attrMap[id] ;
153      sendAttributToServer(*attr) ;
154   }
155
156   template <class T>
157   void CObjectTemplate<T>::sendAttributToServer(tree::CAttribute& attr)
158   {
159     shared_ptr<CContext> context=CObjectFactory::GetObject<CContext>(CObjectFactory::GetCurrentContextId()) ;
160     
161    if (!context->hasServer)
162    {
163       CContextClient* client=context->client ;
164
165       CEventClient event(getType(),EVENT_ID_SEND_ATTRIBUTE) ;   
166       if (client->isServerLeader())
167       {
168         CMessage msg ;
169         msg<<this->getId() ;
170         msg<<attr.getName() ;
171         msg<<attr ;
172         event.push(client->getServerLeader(),1,msg) ;
173         client->sendEvent(event) ;
174       }
175       else client->sendEvent(event) ;
176    }
177     
178   }
179   
180   template <class T>
181   void CObjectTemplate<T>::recvAttributFromClient(CEventServer& event)
182   {
183     
184      CBufferIn* buffer=event.subEvents.begin()->buffer;
185      string id,attrId;
186      *buffer>>id ;
187      CAttributeMap & attrMap = *get(id);
188      *buffer>>attrId ;
189      CAttribute* attr=attrMap[attrId] ;
190      info(50)<<"attribut recu "<<attrId<<"  " ;
191      if (attr->isEmpty()) info(50)<<"--> empty"<<endl ;
192      else info(50) /*<attr->getValue()*/<<endl ;
193      *buffer>>*attr ;
194       info(50)<<"attribut recu "<<attrId<<"  " ;
195      if (attr->isEmpty()) info(50)<<"--> empty"<<endl ;
196      else info(50) /*attr->getValue()*/<<endl ;
197  }
198
199   template <class T>
200   bool CObjectTemplate<T>::dispatchEvent(CEventServer& event)
201   {
202      switch(event.type)
203      {
204         case EVENT_ID_SEND_ATTRIBUTE :
205           recvAttributFromClient(event) ;
206           return true ;
207           break ;
208       
209         default :
210         return false ;
211//           ERROR("void CObjectTemplate<T>::recvEvent(CEventServer& event)",
212//                 <<"Unknown Event") ;
213      }
214   }
215   
216   template <typename T>
217   bool CObjectTemplate<T>::has(const string & id)
218   {
219     return CObjectFactory::HasObject<T>(id) ;
220   }
221
222   template <typename T>
223   boost::shared_ptr<T> CObjectTemplate<T>::get(const string & id)
224   {
225     return CObjectFactory::GetObject<T>(id) ;
226   }
227
228   template <typename T>
229   boost::shared_ptr<T> CObjectTemplate<T>::create(const string & id)
230   {
231     return CObjectFactory::CreateObject<T>(id) ;
232   }   ///--------------------------------------------------------------
233
234  template <typename T>
235  boost::shared_ptr<T> CObjectTemplate<T>::get(void)
236  {
237    return CObjectFactory::GetObject<T>((T*)this) ;
238  }
239 
240
241 
242} // namespace xmlioserver
243
244#endif // __XMLIO_CObjectTemplate_impl__
Note: See TracBrowser for help on using the repository browser.