source: XMLIO_V2/dev/common/src/group_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: 14.2 KB
Line 
1#ifndef __XMLIO_CGroupTemplate_impl__
2#define __XMLIO_CGroupTemplate_impl__
3
4#include "xmlioserver_spl.hpp"
5#include "event_server.hpp"
6#include "object_template_impl.hpp"
7#include "group_template.hpp"
8#include "context.hpp"
9#include "event_client.hpp"
10#include "context_client.hpp"
11
12
13namespace xmlioserver
14{
15   using namespace tree;
16
17   /// ////////////////////// Définitions ////////////////////// ///
18
19   template <class U, class V, class W>
20      CGroupTemplate<U, V, W>::CGroupTemplate(void)
21         : CObjectTemplate<V>() //, V()
22         , childMap(), childList()
23         , groupMap(), groupList()
24   { /* Ne rien faire de plus */ }
25
26   template <class U, class V, class W>
27      CGroupTemplate<U, V, W>::CGroupTemplate(const StdString & id)
28         : CObjectTemplate<V>(id) //, V()
29         , childMap(), childList()
30         , groupMap(), groupList()
31   { /* Ne rien faire de plus */ }
32
33   template <class U, class V, class W>
34      CGroupTemplate<U, V, W>::~CGroupTemplate(void)
35   { /* Ne rien faire de plus */ }
36   
37   ///--------------------------------------------------------------
38   
39   template <class U, class V, class W>
40      void CGroupTemplate<U, V, W>::toBinary(StdOStream & os) const
41   {
42      SuperClass::toBinary(os);
43     
44      const StdSize grpnb = this->groupList.size();
45      const StdSize chdnb = this->childList.size();
46      tree::ENodeType cenum = U::GetType();
47      tree::ENodeType genum = V::GetType();
48     
49      os.write (reinterpret_cast<const char*>(&grpnb) , sizeof(StdSize));
50      os.write (reinterpret_cast<const char*>(&chdnb) , sizeof(StdSize));     
51     
52      typename std::vector<boost::shared_ptr<V> >::const_iterator
53         itg = this->groupList.begin(), endg = this->groupList.end();
54      typename std::vector<boost::shared_ptr<U> >::const_iterator
55         itc = this->childList.begin(), endc = this->childList.end();
56           
57      for (; itg != endg; itg++)
58      { 
59         boost::shared_ptr<V> group = *itg;
60         bool hid = group->hasId();
61         
62         os.write (reinterpret_cast<const char*>(&genum), sizeof(tree::ENodeType));     
63         os.write (reinterpret_cast<const char*>(&hid), sizeof(bool));
64         
65         if (hid)
66         {
67            const StdString & id = group->getId();
68            const StdSize size   = id.size();
69               
70            os.write (reinterpret_cast<const char*>(&size), sizeof(StdSize));
71            os.write (id.data(), size * sizeof(char));         
72         }             
73         group->toBinary(os);
74      }
75           
76      for (; itc != endc; itc++)
77      { 
78         boost::shared_ptr<U> child = *itc;
79         bool hid = child->hasId();
80         
81         os.write (reinterpret_cast<const char*>(&cenum), sizeof(tree::ENodeType));
82         os.write (reinterpret_cast<const char*>(&hid), sizeof(bool));
83         
84         if (hid)
85         {
86            const StdString & id = child->getId();
87            const StdSize size   = id.size();
88               
89            os.write (reinterpret_cast<const char*>(&size), sizeof(StdSize));
90            os.write (id.data(), size * sizeof(char));         
91         }         
92         child->toBinary(os);
93      }
94     
95   }
96   
97   template <class U, class V, class W>
98      void CGroupTemplate<U, V, W>::fromBinary(StdIStream & is)
99   {
100      SuperClass::fromBinary(is);
101     
102      boost::shared_ptr<V> group_ptr = (this->hasId())
103         ? CObjectFactory::GetObject<V>(this->getId())
104         : CObjectFactory::GetObject(boost::polymorphic_downcast<V*>(this));
105     
106      StdSize grpnb = 0;
107      StdSize chdnb = 0;
108      tree::ENodeType renum = Unknown;
109     
110      is.read (reinterpret_cast<char*>(&grpnb), sizeof(StdSize));
111      is.read (reinterpret_cast<char*>(&chdnb), sizeof(StdSize));
112     
113      for (StdSize i = 0; i < grpnb; i++)
114      {
115         bool hid = false;
116         is.read (reinterpret_cast<char*>(&renum), sizeof(tree::ENodeType));
117         is.read (reinterpret_cast<char*>(&hid), sizeof(bool));
118         
119         if (renum != V::GetType())
120            ERROR("CGroupTemplate<U, V, W>::fromBinary(StdIStream & is)",
121                  << "[ renum = " << renum << "] Bad type !");
122                       
123         if (hid)
124         {
125            StdSize size  = 0;
126            is.read (reinterpret_cast<char*>(&size), sizeof(StdSize));
127            StdString id(size, ' ');
128            is.read (const_cast<char *>(id.data()), size * sizeof(char));
129            CGroupFactory::CreateGroup(group_ptr, id)->fromBinary(is);
130         }
131         else
132         {
133            CGroupFactory::CreateGroup(group_ptr)->fromBinary(is);
134         }
135      }
136     
137      for (StdSize j = 0; j < chdnb; j++)
138      {
139         bool hid = false;
140         is.read (reinterpret_cast<char*>(&renum), sizeof(tree::ENodeType));
141         is.read (reinterpret_cast<char*>(&hid), sizeof(bool));
142         
143         if (renum != U::GetType())
144            ERROR("CGroupTemplate<U, V, W>::fromBinary(StdIStream & is)",
145                  << "[ renum = " << renum << "] Bad type !");
146                 
147         if (hid)
148         {
149            StdSize size  = 0;
150            is.read (reinterpret_cast<char*>(&size), sizeof(StdSize));
151            StdString id(size, ' ');
152            is.read (const_cast<char *>(id.data()), size * sizeof(char));
153            CGroupFactory::CreateChild(group_ptr, id)->fromBinary(is);           
154         }
155         else
156         {
157            CGroupFactory::CreateChild(group_ptr)->fromBinary(is);
158         }   
159      }
160   }
161
162   //--------------------------------------------------------------
163
164   template <class U, class V, class W>
165      StdString CGroupTemplate<U, V, W>::toString(void) const
166   {
167      StdOStringStream oss;
168      StdString name = (this->getId().compare(V::GetDefName()) != 0)
169                     ? V::GetName() : V::GetDefName();
170
171      oss << "<" << name << " ";
172      if (this->hasId() && (this->getId().compare(V::GetDefName()) != 0))
173         oss << " id=\"" << this->getId() << "\" ";
174         
175      if (this->hasChild())
176      {
177         oss << SuperClassAttribute::toString() << ">" << std::endl;
178         
179         typename std::vector<boost::shared_ptr<V> >::const_iterator
180            itg = this->groupList.begin(), endg = this->groupList.end();
181         typename std::vector<boost::shared_ptr<U> >::const_iterator
182            itc = this->childList.begin(), endc = this->childList.end();
183           
184         for (; itg != endg; itg++)
185         { 
186            boost::shared_ptr<V> group = *itg;
187            oss << *group << std::endl;
188         }
189           
190         for (; itc != endc; itc++)
191         { 
192            boost::shared_ptr<U> child = *itc;
193            oss << *child << std::endl;
194         }
195           
196         oss << "</" << name << " >";
197      }
198      else
199      {
200         oss << SuperClassAttribute::toString() << "/>";
201      }
202      return (oss.str());
203   }
204
205   template <class U, class V, class W>
206      void CGroupTemplate<U, V, W>::fromString(const StdString & str)
207   { 
208      ERROR("CGroupTemplate<U, V, W>::toString(void)",
209            << "[ str = " << str << "] Not implemented yet !");
210   }
211
212   //---------------------------------------------------------------
213
214   template <class U, class V, class W>
215      StdString CGroupTemplate<U, V, W>::GetName(void)
216   { 
217      return (U::GetName().append("_group")); 
218   }
219
220   template <class U, class V, class W>
221      StdString CGroupTemplate<U, V, W>::GetDefName(void)
222   { 
223      return (U::GetName().append("_definition")); 
224   }
225   
226   //---------------------------------------------------------------   
227
228   template <class U, class V, class W>
229      const std::vector<boost::shared_ptr<U> >&
230         CGroupTemplate<U, V, W>::getChildList(void) const
231   { 
232      return (this->childList); 
233   }
234
235   //---------------------------------------------------------------
236
237   template <class U, class V, class W>
238      const xios_map<StdString, boost::shared_ptr<V> >&
239         CGroupTemplate<U, V, W>::getGroupMap(void) const
240   { 
241      return (this->groupList);
242   }
243
244   //---------------------------------------------------------------
245
246   template <class U, class V, class W>
247      bool CGroupTemplate<U, V, W>::hasChild(void) const
248   { 
249      return ((groupList.size() + childList.size()) > 0); 
250   }
251
252   //---------------------------------------------------------------
253
254   template <class U, class V, class W>
255      void CGroupTemplate<U, V, W>::parse(xml::CXMLNode & node)
256   { 
257      this->parse(node, true); 
258   }
259   
260   //---------------------------------------------------------------
261   
262   template <class U, class V, class W>
263      void CGroupTemplate<U, V, W>::solveDescInheritance(const CAttributeMap * const parent)
264   {
265      if (parent != NULL)
266         SuperClassAttribute::setAttributes(parent);
267         
268      typename std::vector<boost::shared_ptr<U> >::const_iterator
269         itc = this->childList.begin(), endc = this->childList.end();
270      typename std::vector<boost::shared_ptr<V> >::const_iterator
271         itg = this->groupList.begin(), endg = this->groupList.end();
272             
273      for (; itc != endc; itc++)
274      { 
275         boost::shared_ptr<U> child = *itc;
276         child->solveDescInheritance(this);
277      }
278           
279      for (; itg != endg; itg++)
280      { 
281         boost::shared_ptr<V> group = *itg;
282         group->solveRefInheritance();
283         group->solveDescInheritance(this);
284      }
285   }
286
287   //---------------------------------------------------------------
288
289   template <class U, class V, class W>
290      void CGroupTemplate<U, V, W>::getAllChildren(std::vector<boost::shared_ptr<U> > & allc) const
291   {
292      allc.insert (allc.end(), childList.begin(), childList.end());
293      typename std::vector<boost::shared_ptr<V> >::const_iterator
294         itg = this->groupList.begin(), endg = this->groupList.end();
295         
296      for (; itg != endg; itg++)
297      { 
298         boost::shared_ptr<V> group = *itg;
299         group->getAllChildren(allc);
300      }
301   }
302
303   //---------------------------------------------------------------
304
305   template <class U, class V, class W>
306      std::vector<boost::shared_ptr<U> > CGroupTemplate<U, V, W>::getAllChildren(void) const
307   { 
308      std::vector<boost::shared_ptr<U> > allc;
309      this->getAllChildren(allc);
310      return (allc);
311   }
312
313   //---------------------------------------------------------------
314
315   template <class U, class V, class W>
316      void CGroupTemplate<U, V, W>::solveRefInheritance(void)
317   { /* Ne rien faire de plus */ }
318   
319//   template <class U, class V, class W>
320//   bool CGroupTemplate<U, V, W>::has(const string& id)
321//   {
322//       return CObjectFactory::HasObject<V>(id) ;
323//   }
324
325//   template <class U, class V, class W>
326//   boost::shared_ptr<V> CGroupTemplate<U, V, W>::get(const string& id)
327//   {
328//       return CObjectFactory::GetObject<V>(id) ;
329//   }
330
331//   template <class U, class V, class W>
332//   boost::shared_ptr<V> CGroupTemplate<U, V, W>::get()
333//   {
334//       return CObjectFactory::GetObject<V>(this) ;
335//   }
336   
337//   template <class U, class V, class W>
338//   boost::shared_ptr<V> CGroupTemplate<U, V, W>::create(const string& id)
339//   {
340//       return CObjectFactory::CreateObject<V>(id) ;
341//   }
342   ///--------------------------------------------------------------
343
344   template <class U, class V, class W>
345   boost::shared_ptr<U> CGroupTemplate<U, V, W>::createChild(const string& id) 
346  {
347    return CGroupFactory::CreateChild<V>(this->get(), id) ;
348  }
349 
350   template <class U, class V, class W>
351   boost::shared_ptr<V> CGroupTemplate<U, V, W>::createChildGroup(const string& id) 
352  {
353    return CGroupFactory::CreateGroup<V>(this->get(), id) ;
354  }
355
356
357   template <class U, class V, class W>
358   void CGroupTemplate<U, V, W>::sendCreateChild(const string& id)
359   {
360    shared_ptr<CContext> context=CContext::current() ;
361   
362    if (! context->hasServer )
363    {
364       CContextClient* client=context->client ;
365
366       CEventClient event(this->getType(),EVENT_ID_CREATE_CHILD) ;   
367       if (client->isServerLeader())
368       {
369         CMessage msg ;
370         msg<<this->getId() ;
371         msg<<id ;
372         event.push(client->getServerLeader(),1,msg) ;
373         client->sendEvent(event) ;
374       }
375       else client->sendEvent(event) ;
376    }
377     
378   }
379   
380   template <class U, class V, class W>
381   void CGroupTemplate<U, V, W>::sendCreateChildGroup(const string& id)
382   {
383    shared_ptr<CContext> context=CContext::current() ;
384    if (! context->hasServer )
385    {
386       CContextClient* client=context->client ;
387
388       CEventClient event(this->getType(),EVENT_ID_CREATE_CHILD_GROUP) ;   
389       if (client->isServerLeader())
390       {
391         CMessage msg ;
392         msg<<this->getId() ;
393         msg<<id ;
394         event.push(client->getServerLeader(),1,msg) ;
395         client->sendEvent(event) ;
396       }
397       else client->sendEvent(event) ;
398    }
399     
400   }
401   
402   template <class U, class V, class W>
403   void CGroupTemplate<U, V, W>::recvCreateChild(CEventServer& event)
404   {
405     
406      CBufferIn* buffer=event.subEvents.begin()->buffer;
407      string id;
408      *buffer>>id ;
409      V::get(id)->recvCreateChild(*buffer) ;
410   }
411   
412   
413   template <class U, class V, class W>
414   void CGroupTemplate<U, V, W>::recvCreateChild(CBufferIn& buffer)
415   {
416      string id ;
417      buffer>>id ;
418      createChild(id) ;
419   }
420
421   template <class U, class V, class W>
422   void CGroupTemplate<U, V, W>::recvCreateChildGroup(CEventServer& event)
423   {
424     
425      CBufferIn* buffer=event.subEvents.begin()->buffer;
426      string id;
427      *buffer>>id ;
428      V::get(id)->recvCreateChildGroup(*buffer) ;
429   }
430   
431   
432   template <class U, class V, class W>
433   void CGroupTemplate<U, V, W>::recvCreateChildGroup(CBufferIn& buffer)
434   {
435      string id ;
436      buffer>>id ;
437      createChildGroup(id) ;
438   }
439   
440
441   template <class U, class V, class W>
442   bool CGroupTemplate<U, V, W>::dispatchEvent(CEventServer& event)
443   {
444      if (CObjectTemplate<V>::dispatchEvent(event)) return true ;
445      else
446      {
447        switch(event.type)
448        {
449           case EVENT_ID_CREATE_CHILD :
450             recvCreateChild(event) ;
451             return true ;
452             break ;
453         
454           case EVENT_ID_CREATE_CHILD_GROUP :
455             recvCreateChildGroup(event) ;
456             return true ;
457             break ;       
458         
459           default :
460           return false ;
461        }
462      }
463   }
464
465} // namespace xmlioserver
466
467
468#endif // __XMLIO_CGroupTemplate_impl__
Note: See TracBrowser for help on using the repository browser.