source: XIOS3/trunk/src/group_template_impl.hpp @ 2628

Last change on this file since 2628 was 2614, checked in by ymipsl, 3 months ago
  • Permit now usage of contex_group into xml file for more modularity
  • Src path is now relative to parent file, except if path is an absolute path

YM

  • 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: 13.3 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   template <class U, class V, class W>
40      StdString CGroupTemplate<U, V, W>::toString(void) const
41   {
42      StdOStringStream oss;
43      StdString name = (this->getId().compare(V::GetDefName()) != 0)
44                     ? V::GetName() : V::GetDefName();
45
46      oss << "<" << name << " ";
47      if (this->hasId() && (this->getId().compare(V::GetDefName()) != 0))
48         oss << " id=\"" << this->getId() << "\" ";
49         
50      if (this->hasChild())
51      {
52         oss << SuperClassAttribute::toString() << ">" << std::endl;
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            oss << *group << std::endl;
63         }
64           
65         for (; itc != endc; itc++)
66         { 
67            U* child = *itc;
68            oss << *child << std::endl;
69         }
70           
71         oss << "</" << name << " >";
72      }
73      else
74      {
75         oss << SuperClassAttribute::toString() << "/>";
76      }
77      return (oss.str());
78   }
79
80   template <class U, class V, class W>
81      void CGroupTemplate<U, V, W>::fromString(const StdString & str)
82   { 
83      ERROR("CGroupTemplate<U, V, W>::toString(void)",
84            << "[ str = " << str << "] Not implemented yet !");
85   }
86
87   //---------------------------------------------------------------
88
89   template <class U, class V, class W>
90      StdString CGroupTemplate<U, V, W>::GetName(void)
91   { 
92      return (U::GetName().append("_group")); 
93   }
94
95   template <class U, class V, class W>
96      StdString CGroupTemplate<U, V, W>::GetDefName(void)
97   { 
98      return (U::GetName().append("_definition")); 
99   }
100   
101   //---------------------------------------------------------------   
102
103   template <class U, class V, class W>
104      const xios_map<StdString, U*>&
105         CGroupTemplate<U, V, W>::getChildMap(void) const
106   { 
107      return (this->childMap); 
108   }
109
110   //---------------------------------------------------------------
111
112   template <class U, class V, class W>
113      const xios_map<StdString, V*>&
114         CGroupTemplate<U, V, W>::getGroupMap(void) const
115   { 
116      return (this->groupMap);
117   }
118
119   //---------------------------------------------------------------
120
121   template <class U, class V, class W>
122      bool CGroupTemplate<U, V, W>::hasChild(void) const
123   { 
124      return ((groupList.size() + childList.size()) > 0); 
125   }
126
127   //---------------------------------------------------------------
128
129   template <class U, class V, class W>
130      void CGroupTemplate<U, V, W>::solveDescInheritance(bool apply, const CAttributeMap * const parent)
131   {
132      if (parent != NULL)
133         SuperClassAttribute::setAttributes(parent, apply);
134         
135      typename std::vector<U*>::const_iterator
136         itc = this->childList.begin(), endc = this->childList.end();
137      typename std::vector<V*>::const_iterator
138         itg = this->groupList.begin(), endg = this->groupList.end();
139             
140      for (; itc != endc; itc++)
141      { 
142         U* child = *itc;
143         child->solveDescInheritance(apply,this);
144      }
145           
146      for (; itg != endg; itg++)
147      { 
148         V* group = *itg;
149         if (apply) group->solveRefInheritance();
150         group->solveDescInheritance(apply,this);
151      }
152   }
153
154   //---------------------------------------------------------------
155
156   template <class U, class V, class W>
157      void CGroupTemplate<U, V, W>::getAllChildren(std::vector<U*>& allc) const
158   {
159      allc.insert (allc.end(), childList.begin(), childList.end());
160      typename std::vector<V*>::const_iterator
161         itg = this->groupList.begin(), endg = this->groupList.end();
162         
163      for (; itg != endg; itg++)
164      { 
165         V* group = *itg;
166         group->getAllChildren(allc);
167      }
168   }
169
170   //---------------------------------------------------------------
171
172   template <class U, class V, class W>
173      std::vector<U*> CGroupTemplate<U, V, W>::getAllChildren(void) const
174   { 
175      std::vector<U*> allc;
176      this->getAllChildren(allc);
177      return (allc);
178   }
179
180   //---------------------------------------------------------------
181
182   template <class U, class V, class W>
183      void CGroupTemplate<U, V, W>::solveRefInheritance(void)
184   { /* Ne rien faire de plus */ }
185   
186
187   ///--------------------------------------------------------------
188
189 
190   template <class U, class V, class W>
191   U* CGroupTemplate<U, V, W>::createChild(const string& id) 
192  {
193    return CGroupFactory::CreateChild<V>(this->getShared(), id).get() ;
194  }
195
196   template <class U, class V, class W>
197   void CGroupTemplate<U, V, W>::addChild(U* child) 
198  {
199    return CGroupFactory::AddChild<V>(this->getShared(),child->getShared()) ;
200  }
201 
202   template <class U, class V, class W>
203   V* CGroupTemplate<U, V, W>::createChildGroup(const string& id) 
204  {
205    return CGroupFactory::CreateGroup<V>(this->getShared(), id).get() ;
206  }
207
208   template <class U, class V, class W>
209   void CGroupTemplate<U, V, W>::addChildGroup(V* childGroup) 
210  {
211    return CGroupFactory::AddGroup<V>(this->getShared(), childGroup->getShared()) ;
212  }
213
214   template <class U, class V, class W>
215   void CGroupTemplate<U, V, W>::sendCreateChild(const string& id, CContextClient* client, const string& objectId)
216   {
217
218    CEventClient event(this->getType(),EVENT_ID_CREATE_CHILD) ;
219    if (client->isServerLeader())
220    {
221      CMessage msg ;
222      if (objectId.empty()) msg << this->getId();
223      else msg << objectId;
224      msg<<id ;
225      const std::list<int>& ranks = client->getRanksServerLeader();
226      for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank)
227       event.push(*itRank,1,msg) ;
228      client->sendEvent(event) ;
229    }
230    else client->sendEvent(event) ;
231   }
232
233   template <class U, class V, class W>
234   void CGroupTemplate<U, V, W>::sendCreateChildGroup(const string& id, CContextClient* client, const string& objectId)
235   {
236     CEventClient event(this->getType(),EVENT_ID_CREATE_CHILD_GROUP) ;
237     if (client->isServerLeader())
238     {
239       CMessage msg ;
240       if (objectId.empty()) msg << this->getId();
241       else msg << objectId;
242       msg<<id ;
243       const std::list<int>& ranks = client->getRanksServerLeader();
244       for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank)
245         event.push(*itRank,1,msg) ;
246       client->sendEvent(event) ;
247     }
248     else client->sendEvent(event) ;
249   }   
250
251
252   template <class U, class V, class W>
253   void CGroupTemplate<U, V, W>::recvCreateChild(CEventServer& event)
254   {
255      CBufferIn* buffer=event.subEvents.begin()->buffer;
256      string id;
257      *buffer>>id ;
258      V::get(id)->recvCreateChild(*buffer) ;
259   }
260   
261   
262   template <class U, class V, class W>
263   void CGroupTemplate<U, V, W>::recvCreateChild(CBufferIn& buffer)
264   {
265      string id ;
266      buffer>>id ;
267      createChild(id) ;
268   }
269
270   template <class U, class V, class W>
271   void CGroupTemplate<U, V, W>::recvCreateChildGroup(CEventServer& event)
272   {
273     
274      CBufferIn* buffer=event.subEvents.begin()->buffer;
275      string id;
276      *buffer>>id ;
277      V::get(id)->recvCreateChildGroup(*buffer) ;
278   }
279   
280   
281   template <class U, class V, class W>
282   void CGroupTemplate<U, V, W>::recvCreateChildGroup(CBufferIn& buffer)
283   {
284      string id ;
285      buffer>>id ;
286      createChildGroup(id) ;
287   }
288   
289
290   template <class U, class V, class W>
291   bool CGroupTemplate<U, V, W>::dispatchEvent(CEventServer& event)
292   {
293      if (CObjectTemplate<V>::dispatchEvent(event)) return true ;
294      else
295      {
296        switch(event.type)
297        {
298           case EVENT_ID_CREATE_CHILD :
299             recvCreateChild(event) ;
300             return true ;
301             break ;
302         
303           case EVENT_ID_CREATE_CHILD_GROUP :
304             recvCreateChildGroup(event) ;
305             return true ;
306             break ;       
307         
308           default :
309           return false ;
310        }
311      }
312   }
313
314   template <class U, class V, class W>
315   void CGroupTemplate<U, V, W>::parse(xml::CXMLNode & node)
316   { 
317      this->parse(node, true); 
318   }
319   
320   template <class U, class V, class W>
321   void CGroupTemplate<U, V, W>::parse(xml::CXMLNode & node, bool withAttr, const std::set<StdString>& parseContextList)
322   {
323     ERROR("void CGroupTemplate<U, V, W>::parse(xml::CXMLNode & node, bool withAttr, const std::set<StdString>& parseContextList)",
324                     <<"must not be called by this kind of object : "<<GetName() ) ;
325   }
326
327   template <class U, class V, class W>
328      void CGroupTemplate<U, V, W>::parse(xml::CXMLNode & node, bool withAttr)
329   {
330
331      StdString name = node.getElementName();
332      xml::THashAttributes attributes = node.getAttributes();
333      if (withAttr)
334      {
335         CGroupTemplate<U, V, W>::SuperClass::parse(node);
336         if (attributes.end() != attributes.find("src")) xml::CXMLParser::ParseInclude(attributes["src"].c_str(), *this);
337      }
338
339      // PARSING POUR GESTION DES ENFANTS
340           V* group_ptr = (this->hasId()) 
341         ? V::get(this->getId())
342         : boost::polymorphic_downcast<V*>(this);
343
344      if (!(node.goToChildElement()))
345      {
346         if (this->hasId())
347         {
348            DEBUG(<< "L'objet de type \'" << V::GetName()
349                  << "\' nommé \'" << this->getId()
350                  << "\' ne contient pas d\'enfant !");
351         }
352      }
353      else
354      {
355         do { // Parcours pour traitement.
356
357            StdString name = node.getElementName();
358            attributes.clear();
359            attributes = node.getAttributes();
360
361            if (name.compare(V::GetName()) == 0)
362            {
363               if (attributes.end() == attributes.find("id"))
364                  CGroupFactory::CreateGroup(group_ptr->getShared())->parse(node);
365               else
366                  CGroupFactory::CreateGroup(group_ptr->getShared(), attributes["id"])->parse(node);
367               continue;
368            }
369
370            if (name.compare(U::GetName()) == 0)
371            {
372               if (attributes.end() == attributes.find("id"))
373                  CGroupFactory::CreateChild(group_ptr->getShared())->parse(node);
374               else
375                  CGroupFactory::CreateChild(group_ptr->getShared(), attributes["id"])->parse(node);
376               continue;
377            }
378
379            DEBUG(<< "Dans le contexte \'" << CContext::getCurrent()->getId()
380                  << "\', un objet de type \'" << V::GetName()
381                  << "\' ne peut contenir qu'un objet de type \'" << V::GetName()
382                  << "\' ou de type \'" << U::GetName()
383                  << "\' (reçu : " << name << ") !");
384
385         } while (node.goToNextElement());
386         node.goToParentElement(); // Retour au parent
387      }
388   }
389   
390   template <class U, class V, class W>
391   void CGroupTemplate<U, V, W>::parseChild(xml::CXMLNode & node)
392   {
393
394
395      // PARSING POUR GESTION DES ENFANTS
396           V* group_ptr = (this->hasId()) 
397         ? V::get(this->getId())
398         : boost::polymorphic_downcast<V*>(this);
399
400          StdString name = node.getElementName();
401          xml::THashAttributes attributes = node.getAttributes();
402
403          if (name.compare(V::GetName()) == 0)
404          {
405             if (attributes.end() == attributes.find("id"))
406                CGroupFactory::CreateGroup(group_ptr->getShared())->parse(node);
407             else
408                CGroupFactory::CreateGroup(group_ptr->getShared(), attributes["id"])->parse(node);
409             return ;
410          }
411          else if (name.compare(U::GetName()) == 0)
412          {
413             if (attributes.end() == attributes.find("id"))
414                CGroupFactory::CreateChild(group_ptr->getShared())->parse(node);
415             else
416                CGroupFactory::CreateChild(group_ptr->getShared(), attributes["id"])->parse(node);
417             return ;
418          }
419
420          DEBUG(<< "Dans le contexte \'" << CContext::getCurrent()->getId()
421                << "\', un objet de type \'" << V::GetName()
422                << "\' ne peut contenir qu'un objet de type \'" << V::GetName()
423                << "\' ou de type \'" << U::GetName()
424                << "\' (reçu : " << name << ") !");
425
426   }
427} // namespace xios
428
429
430#endif // __XIOS_CGroupTemplate_impl__
Note: See TracBrowser for help on using the repository browser.