New URL for NEMO forge!   http://forge.nemo-ocean.eu

Since March 2022 along with NEMO 4.2 release, the code development moved to a self-hosted GitLab.
This present forge is now archived and remained online for history.
group_template_impl.hpp in vendors/XIOS/current/src – NEMO

source: vendors/XIOS/current/src/group_template_impl.hpp @ 3408

Last change on this file since 3408 was 3408, checked in by rblod, 12 years ago

importing initial XIOS vendor drop

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