source: XIOS/dev/branch_yushan/src/group_template_impl.hpp @ 1101

Last change on this file since 1101 was 1101, checked in by yushan, 7 years ago

From ADA, test_omp (test_client + openmp) OK. Todo: add field_C

  • 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.8 KB
Line 
1#ifndef __XIOS_CGroupTemplate_impl__
2#define __XIOS_CGroupTemplate_impl__
3
4#include "xios_spl.hpp"
5#include "event_server.hpp"
6#include "object_template.hpp"
7#include "group_template.hpp"
8#include "context.hpp"
9#include "event_client.hpp"
10#include "context_client.hpp"
11#include "message.hpp"
12#include "type.hpp"
13#include "type_util.hpp"
14
15
16namespace xios
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   ///--------------------------------------------------------------
40/*
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();
48      ENodeType cenum = U::GetType();
49      ENodeType genum = V::GetType();
50     
51      os.write (reinterpret_cast<const char*>(&grpnb) , sizeof(StdSize));
52      os.write (reinterpret_cast<const char*>(&chdnb) , sizeof(StdSize));     
53     
54      typename std::vector<V*>::const_iterator 
55         itg = this->groupList.begin(), endg = this->groupList.end();
56      typename std::vector<U*>::const_iterator
57         itc = this->childList.begin(), endc = this->childList.end();
58           
59      for (; itg != endg; itg++)
60      {
61         V* group = *itg;
62         bool hid = group->hasId();
63         
64         os.write (reinterpret_cast<const char*>(&genum), sizeof(ENodeType));     
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      {
80         U* child = *itc;
81         bool hid = child->hasId();
82         
83         os.write (reinterpret_cast<const char*>(&cenum), sizeof(ENodeType));
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     
104      V* group_ptr = (this->hasId())
105         ? V::get(this->getId())
106         : V::get((V*)this);
107     
108      StdSize grpnb = 0;
109      StdSize chdnb = 0;
110      ENodeType renum = Unknown;
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;
118         is.read (reinterpret_cast<char*>(&renum), sizeof(ENodeType));
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));
131            CGroupFactory::CreateGroup(group_ptr->getShared(), id)->fromBinary(is);
132         }
133         else
134         {
135            CGroupFactory::CreateGroup(group_ptr->getShared())->fromBinary(is);
136         }
137      }
138     
139      for (StdSize j = 0; j < chdnb; j++)
140      {
141         bool hid = false;
142         is.read (reinterpret_cast<char*>(&renum), sizeof(ENodeType));
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));
155            CGroupFactory::CreateChild(group_ptr->getShared(), id)->fromBinary(is);           
156         }
157         else
158         {
159            CGroupFactory::CreateChild(group_ptr->getShared())->fromBinary(is);
160         }   
161      }
162   }
163*/
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         
181         typename std::vector<V*>::const_iterator
182            itg = this->groupList.begin(), endg = this->groupList.end();
183         typename std::vector<U*>::const_iterator
184            itc = this->childList.begin(), endc = this->childList.end();
185           
186         for (; itg != endg; itg++)
187         { 
188            V* group = *itg;
189            oss << *group << std::endl;
190         }
191           
192         for (; itc != endc; itc++)
193         { 
194            U* child = *itc;
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>
231      const std::vector<U*>&
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>
240      const xios_map<StdString, V*>&
241         CGroupTemplate<U, V, W>::getGroupMap(void) const
242   { 
243      return (this->groupMap);
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>
265      void CGroupTemplate<U, V, W>::solveDescInheritance(bool apply, const CAttributeMap * const parent)
266   {
267      if (parent != NULL)
268         SuperClassAttribute::setAttributes(parent, apply);
269         
270      typename std::vector<U*>::const_iterator
271         itc = this->childList.begin(), endc = this->childList.end();
272      typename std::vector<V*>::const_iterator
273         itg = this->groupList.begin(), endg = this->groupList.end();
274             
275      for (; itc != endc; itc++)
276      { 
277         U* child = *itc;
278         child->solveDescInheritance(apply,this);
279      }
280           
281      for (; itg != endg; itg++)
282      { 
283         V* group = *itg;
284         if (apply) group->solveRefInheritance();
285         group->solveDescInheritance(apply,this);
286      }
287   }
288
289   //---------------------------------------------------------------
290
291   template <class U, class V, class W>
292      void CGroupTemplate<U, V, W>::getAllChildren(std::vector<U*>& allc) const
293   {
294      allc.insert (allc.end(), childList.begin(), childList.end());
295      typename std::vector<V*>::const_iterator
296         itg = this->groupList.begin(), endg = this->groupList.end();
297         
298      for (; itg != endg; itg++)
299      { 
300         V* group = *itg;
301         group->getAllChildren(allc);
302      }
303   }
304
305   //---------------------------------------------------------------
306
307   template <class U, class V, class W>
308      std::vector<U*> CGroupTemplate<U, V, W>::getAllChildren(void) const
309   { 
310      std::vector<U*> allc;
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 */ }
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//   }
326
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//   }
344   ///--------------------------------------------------------------
345
346 
347   template <class U, class V, class W>
348   U* CGroupTemplate<U, V, W>::createChild(const string& id) 
349  {
350    printf("U* CGroupTemplate<U, V, W>::createChild(const string& id) %p\n", this->getShared());
351    return CGroupFactory::CreateChild<V>(this->getShared(), id).get() ;
352  }
353
354   template <class U, class V, class W>
355   void CGroupTemplate<U, V, W>::addChild(U* child) 
356  {
357    return CGroupFactory::AddChild<V>(this->getShared(),child->getShared()) ;
358  }
359 
360   template <class U, class V, class W>
361   V* CGroupTemplate<U, V, W>::createChildGroup(const string& id) 
362  {
363    return CGroupFactory::CreateGroup<V>(this->getShared(), id).get() ;
364  }
365
366   template <class U, class V, class W>
367   void CGroupTemplate<U, V, W>::addChildGroup(V* childGroup) 
368  {
369    return CGroupFactory::AddGroup<V>(this->getShared(), childGroup->getShared()) ;
370  }
371
372
373   template <class U, class V, class W>
374   void CGroupTemplate<U, V, W>::sendCreateChild(const string& id)
375   {
376    CContext* context=CContext::getCurrent() ;
377   
378    if (! context->hasServer )
379    {
380       CContextClient* client=context->client ;
381
382       CEventClient event(this->getType(),EVENT_ID_CREATE_CHILD) ;   
383       if (client->isServerLeader())
384       {
385         CMessage msg ;
386         msg<<this->getId() ;
387         msg<<id ;
388         const std::list<int>& ranks = client->getRanksServerLeader();
389         for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank)
390           event.push(*itRank,1,msg) ;
391         client->sendEvent(event) ;
392       }
393       else client->sendEvent(event) ;
394    }
395     
396   }
397   
398   template <class U, class V, class W>
399   void CGroupTemplate<U, V, W>::sendCreateChildGroup(const string& id)
400   {
401    CContext* context=CContext::getCurrent() ;
402    if (! context->hasServer )
403    {
404       CContextClient* client=context->client ;
405
406       CEventClient event(this->getType(),EVENT_ID_CREATE_CHILD_GROUP) ;   
407       if (client->isServerLeader())
408       {
409         CMessage msg ;
410         msg<<this->getId() ;
411         msg<<id ;
412         const std::list<int>& ranks = client->getRanksServerLeader();
413         for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank)
414           event.push(*itRank,1,msg) ;
415         client->sendEvent(event) ;
416       }
417       else client->sendEvent(event) ;
418    }
419     
420   }
421   
422   template <class U, class V, class W>
423   void CGroupTemplate<U, V, W>::recvCreateChild(CEventServer& event)
424   {
425     
426      CBufferIn* buffer=event.subEvents.begin()->buffer;
427      string id;
428      *buffer>>id ;
429      V::get(id)->recvCreateChild(*buffer) ;
430   }
431   
432   
433   template <class U, class V, class W>
434   void CGroupTemplate<U, V, W>::recvCreateChild(CBufferIn& buffer)
435   {
436      string id ;
437      buffer>>id ;
438      createChild(id) ;
439   }
440
441   template <class U, class V, class W>
442   void CGroupTemplate<U, V, W>::recvCreateChildGroup(CEventServer& event)
443   {
444     
445      CBufferIn* buffer=event.subEvents.begin()->buffer;
446      string id;
447      *buffer>>id ;
448      V::get(id)->recvCreateChildGroup(*buffer) ;
449   }
450   
451   
452   template <class U, class V, class W>
453   void CGroupTemplate<U, V, W>::recvCreateChildGroup(CBufferIn& buffer)
454   {
455      string id ;
456      buffer>>id ;
457      createChildGroup(id) ;
458   }
459   
460
461   template <class U, class V, class W>
462   bool CGroupTemplate<U, V, W>::dispatchEvent(CEventServer& event)
463   {
464      if (CObjectTemplate<V>::dispatchEvent(event)) return true ;
465      else
466      {
467        switch(event.type)
468        {
469           case EVENT_ID_CREATE_CHILD :
470             recvCreateChild(event) ;
471             return true ;
472             break ;
473         
474           case EVENT_ID_CREATE_CHILD_GROUP :
475             recvCreateChildGroup(event) ;
476             return true ;
477             break ;       
478         
479           default :
480           return false ;
481        }
482      }
483   }
484
485   template <class U, class V, class W>
486      void CGroupTemplate<U, V, W>::parse(xml::CXMLNode & node, bool withAttr)
487   {
488
489      StdString name = node.getElementName();
490      xml::THashAttributes attributes = node.getAttributes();
491      if (withAttr)
492      {
493         CGroupTemplate<U, V, W>::SuperClass::parse(node);
494         if (attributes.end() != attributes.find("src"))
495         {
496            StdIFStream ifs ( attributes["src"].c_str() , StdIFStream::in );
497            if ( (ifs.rdstate() & std::ifstream::failbit ) != 0 )
498               ERROR("void CGroupTemplate<U, V, W>::parse(xml::CXMLNode & node, bool withAttr)",
499                     <<endl<< "Can not open <"<<attributes["src"].c_str()<<"> file" );
500           
501            if (!ifs.good())
502               ERROR("CGroupTemplate<U, V, W>::parse(xml::CXMLNode & node, bool withAttr)",
503                     << "[ filename = " << attributes["src"] << " ] Bad xml stream !");
504            xml::CXMLParser::ParseInclude(ifs, attributes["src"].c_str(), *this);
505         }
506      }
507
508      // PARSING POUR GESTION DES ENFANTS
509           V* group_ptr = (this->hasId()) 
510         ? V::get(this->getId())
511         : boost::polymorphic_downcast<V*>(this);
512
513      if (!(node.goToChildElement()))
514      {
515         if (this->hasId())
516         {
517            DEBUG(<< "L'objet de type \'" << V::GetName()
518                  << "\' nommé \'" << this->getId()
519                  << "\' ne contient pas d\'enfant !");
520         }
521      }
522      else
523      {
524         do { // Parcours pour traitement.
525
526            StdString name = node.getElementName();
527            attributes.clear();
528            attributes = node.getAttributes();
529
530            if (name.compare(V::GetName()) == 0)
531            {
532               if (attributes.end() == attributes.find("id"))
533                  CGroupFactory::CreateGroup(group_ptr->getShared())->parse(node);
534               else
535                  CGroupFactory::CreateGroup(group_ptr->getShared(), attributes["id"])->parse(node);
536               continue;
537            }
538
539            if (name.compare(U::GetName()) == 0)
540            {
541               if (attributes.end() == attributes.find("id"))
542                  CGroupFactory::CreateChild(group_ptr->getShared())->parse(node);
543               else
544                  CGroupFactory::CreateChild(group_ptr->getShared(), attributes["id"])->parse(node);
545               continue;
546            }
547
548            DEBUG(<< "Dans le contexte \'" << CContext::getCurrent()->getId()
549                  << "\', un objet de type \'" << V::GetName()
550                  << "\' ne peut contenir qu'un objet de type \'" << V::GetName()
551                  << "\' ou de type \'" << U::GetName()
552                  << "\' (reçu : " << name << ") !");
553
554         } while (node.goToNextElement());
555         node.goToParentElement(); // Retour au parent
556      }
557   }
558   
559   template <class U, class V, class W>
560   void CGroupTemplate<U, V, W>::parseChild(xml::CXMLNode & node)
561   {
562
563
564      // PARSING POUR GESTION DES ENFANTS
565           V* group_ptr = (this->hasId()) 
566         ? V::get(this->getId())
567         : boost::polymorphic_downcast<V*>(this);
568
569          StdString name = node.getElementName();
570          xml::THashAttributes attributes = node.getAttributes();
571
572          if (name.compare(V::GetName()) == 0)
573          {
574             if (attributes.end() == attributes.find("id"))
575                CGroupFactory::CreateGroup(group_ptr->getShared())->parse(node);
576             else
577                CGroupFactory::CreateGroup(group_ptr->getShared(), attributes["id"])->parse(node);
578             return ;
579          }
580          else if (name.compare(U::GetName()) == 0)
581          {
582             if (attributes.end() == attributes.find("id"))
583                CGroupFactory::CreateChild(group_ptr->getShared())->parse(node);
584             else
585                CGroupFactory::CreateChild(group_ptr->getShared(), attributes["id"])->parse(node);
586             return ;
587          }
588
589          DEBUG(<< "Dans le contexte \'" << CContext::getCurrent()->getId()
590                << "\', un objet de type \'" << V::GetName()
591                << "\' ne peut contenir qu'un objet de type \'" << V::GetName()
592                << "\' ou de type \'" << U::GetName()
593                << "\' (reçu : " << name << ") !");
594
595   }
596} // namespace xios
597
598
599#endif // __XIOS_CGroupTemplate_impl__
Note: See TracBrowser for help on using the repository browser.