source: XIOS/trunk/src/group_template_impl.hpp @ 591

Last change on this file since 591 was 591, checked in by rlacroix, 9 years ago

Remove leftovers from the XMLIO age.

  • Property copyright set to
    Software name : XIOS (Xml I/O Server)
    http://forge.ipsl.jussieu.fr/ioserver
    Creation date : January 2009
    Licence : CeCCIL version2
    see license file in root directory : Licence_CeCILL_V2-en.txt
    or http://www.cecill.info/licences/Licence_CeCILL_V2-en.html
    Holder : CEA/LSCE (Laboratoire des Sciences du CLimat et de l'Environnement)
    CNRS/IPSL (Institut Pierre Simon Laplace)
    Project Manager : Yann Meurdesoif
    yann.meurdesoif@cea.fr
File size: 18.4 KB
RevLine 
[591]1#ifndef __XIOS_CGroupTemplate_impl__
2#define __XIOS_CGroupTemplate_impl__
[219]3
[591]4#include "xios_spl.hpp"
[300]5#include "event_server.hpp"
[352]6#include "object_template.hpp"
[300]7#include "group_template.hpp"
8#include "context.hpp"
9#include "event_client.hpp"
10#include "context_client.hpp"
[352]11#include "message.hpp"
12#include "type.hpp"
13#include "type_util.hpp"
[300]14
15
[335]16namespace xios
[219]17{
18
19   /// ////////////////////// Définitions ////////////////////// ///
20
21   template <class U, class V, class W>
22      CGroupTemplate<U, V, W>::CGroupTemplate(void)
23         : CObjectTemplate<V>() //, V()
24         , childMap(), childList()
25         , groupMap(), groupList()
26   { /* Ne rien faire de plus */ }
27
28   template <class U, class V, class W>
29      CGroupTemplate<U, V, W>::CGroupTemplate(const StdString & id)
30         : CObjectTemplate<V>(id) //, V()
31         , childMap(), childList()
32         , groupMap(), groupList()
33   { /* Ne rien faire de plus */ }
34
35   template <class U, class V, class W>
36      CGroupTemplate<U, V, W>::~CGroupTemplate(void)
37   { /* Ne rien faire de plus */ }
38   
39   ///--------------------------------------------------------------
[369]40/*
[219]41   template <class U, class V, class W>
42      void CGroupTemplate<U, V, W>::toBinary(StdOStream & os) const
43   {
44      SuperClass::toBinary(os);
45     
46      const StdSize grpnb = this->groupList.size();
47      const StdSize chdnb = this->childList.size();
[345]48      ENodeType cenum = U::GetType();
49      ENodeType genum = V::GetType();
[219]50     
51      os.write (reinterpret_cast<const char*>(&grpnb) , sizeof(StdSize));
52      os.write (reinterpret_cast<const char*>(&chdnb) , sizeof(StdSize));     
53     
[347]54      typename std::vector<V*>::const_iterator 
[219]55         itg = this->groupList.begin(), endg = this->groupList.end();
[347]56      typename std::vector<U*>::const_iterator
[219]57         itc = this->childList.begin(), endc = this->childList.end();
58           
59      for (; itg != endg; itg++)
60      {
[347]61         V* group = *itg;
[219]62         bool hid = group->hasId();
63         
[345]64         os.write (reinterpret_cast<const char*>(&genum), sizeof(ENodeType));     
[219]65         os.write (reinterpret_cast<const char*>(&hid), sizeof(bool));
66         
67         if (hid)
68         {
69            const StdString & id = group->getId();
70            const StdSize size   = id.size();
71               
72            os.write (reinterpret_cast<const char*>(&size), sizeof(StdSize));
73            os.write (id.data(), size * sizeof(char));         
74         }             
75         group->toBinary(os);
76      }
77           
78      for (; itc != endc; itc++)
79      {
[347]80         U* child = *itc;
[219]81         bool hid = child->hasId();
82         
[345]83         os.write (reinterpret_cast<const char*>(&cenum), sizeof(ENodeType));
[219]84         os.write (reinterpret_cast<const char*>(&hid), sizeof(bool));
85         
86         if (hid)
87         {
88            const StdString & id = child->getId();
89            const StdSize size   = id.size();
90               
91            os.write (reinterpret_cast<const char*>(&size), sizeof(StdSize));
92            os.write (id.data(), size * sizeof(char));         
93         }         
94         child->toBinary(os);
95      }
96     
97   }
98   
99   template <class U, class V, class W>
100      void CGroupTemplate<U, V, W>::fromBinary(StdIStream & is)
101   {
102      SuperClass::fromBinary(is);
103     
[347]104      V* group_ptr = (this->hasId())
105         ? V::get(this->getId())
106         : V::get((V*)this);
[219]107     
108      StdSize grpnb = 0;
109      StdSize chdnb = 0;
[345]110      ENodeType renum = Unknown;
[219]111     
112      is.read (reinterpret_cast<char*>(&grpnb), sizeof(StdSize));
113      is.read (reinterpret_cast<char*>(&chdnb), sizeof(StdSize));
114     
115      for (StdSize i = 0; i < grpnb; i++)
116      {
117         bool hid = false;
[345]118         is.read (reinterpret_cast<char*>(&renum), sizeof(ENodeType));
[219]119         is.read (reinterpret_cast<char*>(&hid), sizeof(bool));
120         
121         if (renum != V::GetType())
122            ERROR("CGroupTemplate<U, V, W>::fromBinary(StdIStream & is)",
123                  << "[ renum = " << renum << "] Bad type !");
124                       
125         if (hid)
126         {
127            StdSize size  = 0;
128            is.read (reinterpret_cast<char*>(&size), sizeof(StdSize));
129            StdString id(size, ' ');
130            is.read (const_cast<char *>(id.data()), size * sizeof(char));
[347]131            CGroupFactory::CreateGroup(group_ptr->getShared(), id)->fromBinary(is);
[219]132         }
133         else
134         {
[347]135            CGroupFactory::CreateGroup(group_ptr->getShared())->fromBinary(is);
[219]136         }
137      }
138     
139      for (StdSize j = 0; j < chdnb; j++)
140      {
141         bool hid = false;
[345]142         is.read (reinterpret_cast<char*>(&renum), sizeof(ENodeType));
[219]143         is.read (reinterpret_cast<char*>(&hid), sizeof(bool));
144         
145         if (renum != U::GetType())
146            ERROR("CGroupTemplate<U, V, W>::fromBinary(StdIStream & is)",
147                  << "[ renum = " << renum << "] Bad type !");
148                 
149         if (hid)
150         {
151            StdSize size  = 0;
152            is.read (reinterpret_cast<char*>(&size), sizeof(StdSize));
153            StdString id(size, ' ');
154            is.read (const_cast<char *>(id.data()), size * sizeof(char));
[347]155            CGroupFactory::CreateChild(group_ptr->getShared(), id)->fromBinary(is);           
[219]156         }
157         else
158         {
[347]159            CGroupFactory::CreateChild(group_ptr->getShared())->fromBinary(is);
[219]160         }   
161      }
162   }
[369]163*/
[219]164   //--------------------------------------------------------------
165
166   template <class U, class V, class W>
167      StdString CGroupTemplate<U, V, W>::toString(void) const
168   {
169      StdOStringStream oss;
170      StdString name = (this->getId().compare(V::GetDefName()) != 0)
171                     ? V::GetName() : V::GetDefName();
172
173      oss << "<" << name << " ";
174      if (this->hasId() && (this->getId().compare(V::GetDefName()) != 0))
175         oss << " id=\"" << this->getId() << "\" ";
176         
177      if (this->hasChild())
178      {
179         oss << SuperClassAttribute::toString() << ">" << std::endl;
180         
[347]181         typename std::vector<V*>::const_iterator
[219]182            itg = this->groupList.begin(), endg = this->groupList.end();
[347]183         typename std::vector<U*>::const_iterator
[219]184            itc = this->childList.begin(), endc = this->childList.end();
185           
186         for (; itg != endg; itg++)
187         { 
[347]188            V* group = *itg;
[219]189            oss << *group << std::endl;
190         }
191           
192         for (; itc != endc; itc++)
193         { 
[347]194            U* child = *itc;
[219]195            oss << *child << std::endl;
196         }
197           
198         oss << "</" << name << " >";
199      }
200      else
201      {
202         oss << SuperClassAttribute::toString() << "/>";
203      }
204      return (oss.str());
205   }
206
207   template <class U, class V, class W>
208      void CGroupTemplate<U, V, W>::fromString(const StdString & str)
209   { 
210      ERROR("CGroupTemplate<U, V, W>::toString(void)",
211            << "[ str = " << str << "] Not implemented yet !");
212   }
213
214   //---------------------------------------------------------------
215
216   template <class U, class V, class W>
217      StdString CGroupTemplate<U, V, W>::GetName(void)
218   { 
219      return (U::GetName().append("_group")); 
220   }
221
222   template <class U, class V, class W>
223      StdString CGroupTemplate<U, V, W>::GetDefName(void)
224   { 
225      return (U::GetName().append("_definition")); 
226   }
227   
228   //---------------------------------------------------------------   
229
230   template <class U, class V, class W>
[347]231      const std::vector<U*>&
[219]232         CGroupTemplate<U, V, W>::getChildList(void) const
233   { 
234      return (this->childList); 
235   }
236
237   //---------------------------------------------------------------
238
239   template <class U, class V, class W>
[347]240      const xios_map<StdString, V*>&
[219]241         CGroupTemplate<U, V, W>::getGroupMap(void) const
242   { 
[352]243      return (this->groupMap);
[219]244   }
245
246   //---------------------------------------------------------------
247
248   template <class U, class V, class W>
249      bool CGroupTemplate<U, V, W>::hasChild(void) const
250   { 
251      return ((groupList.size() + childList.size()) > 0); 
252   }
253
254   //---------------------------------------------------------------
255
256   template <class U, class V, class W>
257      void CGroupTemplate<U, V, W>::parse(xml::CXMLNode & node)
258   { 
259      this->parse(node, true); 
260   }
261   
262   //---------------------------------------------------------------
263   
264   template <class U, class V, class W>
[445]265      void CGroupTemplate<U, V, W>::solveDescInheritance(bool apply, const CAttributeMap * const parent)
[219]266   {
267      if (parent != NULL)
[445]268         SuperClassAttribute::setAttributes(parent, apply);
[219]269         
[347]270      typename std::vector<U*>::const_iterator
[219]271         itc = this->childList.begin(), endc = this->childList.end();
[347]272      typename std::vector<V*>::const_iterator
[219]273         itg = this->groupList.begin(), endg = this->groupList.end();
274             
275      for (; itc != endc; itc++)
276      { 
[347]277         U* child = *itc;
[445]278         child->solveDescInheritance(apply,this);
[219]279      }
280           
281      for (; itg != endg; itg++)
282      { 
[347]283         V* group = *itg;
[446]284         if (apply) group->solveRefInheritance();
[445]285         group->solveDescInheritance(apply,this);
[219]286      }
287   }
288
289   //---------------------------------------------------------------
290
291   template <class U, class V, class W>
[347]292      void CGroupTemplate<U, V, W>::getAllChildren(std::vector<U*>& allc) const
[219]293   {
294      allc.insert (allc.end(), childList.begin(), childList.end());
[347]295      typename std::vector<V*>::const_iterator
[219]296         itg = this->groupList.begin(), endg = this->groupList.end();
297         
298      for (; itg != endg; itg++)
299      { 
[347]300         V* group = *itg;
[219]301         group->getAllChildren(allc);
302      }
303   }
304
305   //---------------------------------------------------------------
306
307   template <class U, class V, class W>
[347]308      std::vector<U*> CGroupTemplate<U, V, W>::getAllChildren(void) const
[219]309   { 
[347]310      std::vector<U*> allc;
[219]311      this->getAllChildren(allc);
312      return (allc);
313   }
314
315   //---------------------------------------------------------------
316
317   template <class U, class V, class W>
318      void CGroupTemplate<U, V, W>::solveRefInheritance(void)
319   { /* Ne rien faire de plus */ }
[300]320   
321//   template <class U, class V, class W>
322//   bool CGroupTemplate<U, V, W>::has(const string& id)
323//   {
324//       return CObjectFactory::HasObject<V>(id) ;
325//   }
[219]326
[300]327//   template <class U, class V, class W>
328//   boost::shared_ptr<V> CGroupTemplate<U, V, W>::get(const string& id)
329//   {
330//       return CObjectFactory::GetObject<V>(id) ;
331//   }
332
333//   template <class U, class V, class W>
334//   boost::shared_ptr<V> CGroupTemplate<U, V, W>::get()
335//   {
336//       return CObjectFactory::GetObject<V>(this) ;
337//   }
338   
339//   template <class U, class V, class W>
340//   boost::shared_ptr<V> CGroupTemplate<U, V, W>::create(const string& id)
341//   {
342//       return CObjectFactory::CreateObject<V>(id) ;
343//   }
[219]344   ///--------------------------------------------------------------
345
[346]346 
[300]347   template <class U, class V, class W>
[347]348   U* CGroupTemplate<U, V, W>::createChild(const string& id) 
[300]349  {
[347]350    return CGroupFactory::CreateChild<V>(this->getShared(), id).get() ;
[300]351  }
[346]352
353   template <class U, class V, class W>
[347]354   void CGroupTemplate<U, V, W>::addChild(U* child) 
[346]355  {
[352]356    return CGroupFactory::AddChild<V>(this->getShared(),child->getShared()) ;
[346]357  }
[300]358 
359   template <class U, class V, class W>
[347]360   V* CGroupTemplate<U, V, W>::createChildGroup(const string& id) 
[300]361  {
[347]362    return CGroupFactory::CreateGroup<V>(this->getShared(), id).get() ;
[300]363  }
364
[346]365   template <class U, class V, class W>
[347]366   void CGroupTemplate<U, V, W>::addChildGroup(V* childGroup) 
[346]367  {
[347]368    return CGroupFactory::AddGroup<V>(this->getShared(), childGroup->getShared()) ;
[346]369  }
[300]370
[346]371
[300]372   template <class U, class V, class W>
373   void CGroupTemplate<U, V, W>::sendCreateChild(const string& id)
374   {
[347]375    CContext* context=CContext::getCurrent() ;
[300]376   
377    if (! context->hasServer )
378    {
379       CContextClient* client=context->client ;
380
381       CEventClient event(this->getType(),EVENT_ID_CREATE_CHILD) ;   
382       if (client->isServerLeader())
383       {
384         CMessage msg ;
385         msg<<this->getId() ;
386         msg<<id ;
387         event.push(client->getServerLeader(),1,msg) ;
388         client->sendEvent(event) ;
389       }
390       else client->sendEvent(event) ;
391    }
392     
393   }
394   
395   template <class U, class V, class W>
396   void CGroupTemplate<U, V, W>::sendCreateChildGroup(const string& id)
397   {
[347]398    CContext* context=CContext::getCurrent() ;
[300]399    if (! context->hasServer )
400    {
401       CContextClient* client=context->client ;
402
403       CEventClient event(this->getType(),EVENT_ID_CREATE_CHILD_GROUP) ;   
404       if (client->isServerLeader())
405       {
406         CMessage msg ;
407         msg<<this->getId() ;
408         msg<<id ;
409         event.push(client->getServerLeader(),1,msg) ;
410         client->sendEvent(event) ;
411       }
412       else client->sendEvent(event) ;
413    }
414     
415   }
416   
417   template <class U, class V, class W>
418   void CGroupTemplate<U, V, W>::recvCreateChild(CEventServer& event)
419   {
420     
421      CBufferIn* buffer=event.subEvents.begin()->buffer;
422      string id;
423      *buffer>>id ;
424      V::get(id)->recvCreateChild(*buffer) ;
425   }
426   
427   
428   template <class U, class V, class W>
429   void CGroupTemplate<U, V, W>::recvCreateChild(CBufferIn& buffer)
430   {
431      string id ;
432      buffer>>id ;
433      createChild(id) ;
434   }
435
436   template <class U, class V, class W>
437   void CGroupTemplate<U, V, W>::recvCreateChildGroup(CEventServer& event)
438   {
439     
440      CBufferIn* buffer=event.subEvents.begin()->buffer;
441      string id;
442      *buffer>>id ;
443      V::get(id)->recvCreateChildGroup(*buffer) ;
444   }
445   
446   
447   template <class U, class V, class W>
448   void CGroupTemplate<U, V, W>::recvCreateChildGroup(CBufferIn& buffer)
449   {
450      string id ;
451      buffer>>id ;
452      createChildGroup(id) ;
453   }
454   
455
456   template <class U, class V, class W>
457   bool CGroupTemplate<U, V, W>::dispatchEvent(CEventServer& event)
458   {
459      if (CObjectTemplate<V>::dispatchEvent(event)) return true ;
460      else
461      {
462        switch(event.type)
463        {
464           case EVENT_ID_CREATE_CHILD :
465             recvCreateChild(event) ;
466             return true ;
467             break ;
468         
469           case EVENT_ID_CREATE_CHILD_GROUP :
470             recvCreateChildGroup(event) ;
471             return true ;
472             break ;       
473         
474           default :
475           return false ;
476        }
477      }
478   }
479
[352]480   template <class U, class V, class W>
481      void CGroupTemplate<U, V, W>::parse(xml::CXMLNode & node, bool withAttr)
482   {
483
484      StdString name = node.getElementName();
485      xml::THashAttributes attributes = node.getAttributes();
486      if (withAttr)
487      {
488         CGroupTemplate<U, V, W>::SuperClass::parse(node);
489         if (attributes.end() != attributes.find("src"))
490         {
491            StdIFStream ifs ( attributes["src"].c_str() , StdIFStream::in );
[462]492            if ( (ifs.rdstate() & std::ifstream::failbit ) != 0 )
493               ERROR("void CGroupTemplate<U, V, W>::parse(xml::CXMLNode & node, bool withAttr)",
494                     <<endl<< "Can not open <"<<attributes["src"].c_str()<<"> file" );
495           
[352]496            if (!ifs.good())
497               ERROR("CGroupTemplate<U, V, W>::parse(xml::CXMLNode & node, bool withAttr)",
498                     << "[ filename = " << attributes["src"] << " ] Bad xml stream !");
[462]499            xml::CXMLParser::ParseInclude(ifs, attributes["src"].c_str(), *this);
[352]500         }
501      }
502
503      // PARSING POUR GESTION DES ENFANTS
504           V* group_ptr = (this->hasId()) 
505         ? V::get(this->getId())
506         : boost::polymorphic_downcast<V*>(this);
507
508      if (!(node.goToChildElement()))
509      {
510         if (this->hasId())
511         {
512            DEBUG(<< "L'objet de type \'" << V::GetName()
513                  << "\' nommé \'" << this->getId()
514                  << "\' ne contient pas d\'enfant !");
515         }
516      }
517      else
518      {
519         do { // Parcours pour traitement.
520
521            StdString name = node.getElementName();
522            attributes.clear();
523            attributes = node.getAttributes();
524
525            if (name.compare(V::GetName()) == 0)
526            {
527               if (attributes.end() == attributes.find("id"))
528                  CGroupFactory::CreateGroup(group_ptr->getShared())->parse(node);
529               else
530                  CGroupFactory::CreateGroup(group_ptr->getShared(), attributes["id"])->parse(node);
531               continue;
532            }
533
534            if (name.compare(U::GetName()) == 0)
535            {
536               if (attributes.end() == attributes.find("id"))
537                  CGroupFactory::CreateChild(group_ptr->getShared())->parse(node);
538               else
539                  CGroupFactory::CreateChild(group_ptr->getShared(), attributes["id"])->parse(node);
540               continue;
541            }
542
543            DEBUG(<< "Dans le contexte \'" << CContext::getCurrent()->getId()
544                  << "\', un objet de type \'" << V::GetName()
545                  << "\' ne peut contenir qu'un objet de type \'" << V::GetName()
546                  << "\' ou de type \'" << U::GetName()
547                  << "\' (reçu : " << name << ") !");
548
549         } while (node.goToNextElement());
550         node.goToParentElement(); // Retour au parent
551      }
552   }
[472]553   
554   template <class U, class V, class W>
555   void CGroupTemplate<U, V, W>::parseChild(xml::CXMLNode & node)
556   {
557
558
559      // PARSING POUR GESTION DES ENFANTS
560           V* group_ptr = (this->hasId()) 
561         ? V::get(this->getId())
562         : boost::polymorphic_downcast<V*>(this);
563
564          StdString name = node.getElementName();
565          xml::THashAttributes attributes = node.getAttributes();
566
567          if (name.compare(V::GetName()) == 0)
568          {
569             if (attributes.end() == attributes.find("id"))
570                CGroupFactory::CreateGroup(group_ptr->getShared())->parse(node);
571             else
572                CGroupFactory::CreateGroup(group_ptr->getShared(), attributes["id"])->parse(node);
573             return ;
574          }
575          else if (name.compare(U::GetName()) == 0)
576          {
577             if (attributes.end() == attributes.find("id"))
578                CGroupFactory::CreateChild(group_ptr->getShared())->parse(node);
579             else
580                CGroupFactory::CreateChild(group_ptr->getShared(), attributes["id"])->parse(node);
581             return ;
582          }
583
584          DEBUG(<< "Dans le contexte \'" << CContext::getCurrent()->getId()
585                << "\', un objet de type \'" << V::GetName()
586                << "\' ne peut contenir qu'un objet de type \'" << V::GetName()
587                << "\' ou de type \'" << U::GetName()
588                << "\' (reçu : " << name << ") !");
589
590   }
[335]591} // namespace xios
[219]592
593
[591]594#endif // __XIOS_CGroupTemplate_impl__
Note: See TracBrowser for help on using the repository browser.