source: XIOS/dev/common/src/manager/tree_manager.cpp @ 1620

Last change on this file since 1620 was 286, checked in by ymipsl, 13 years ago

reprise en main de la version de H. Ozdoba. Correction de différentes erreurs de conception et bug.
Version NEMO operationnel en client/server, interoperabilita avec OASIS, reconstition de fichiers via netcdf4/HDF5

YM

File size: 15.6 KB
Line 
1#include "tree_manager.hpp"
2
3#include "attribute_template_impl.hpp"
4#include "object_template_impl.hpp"
5#include "group_template_impl.hpp"
6
7namespace xmlioserver
8{
9   namespace tree
10   {
11      /// ////////////////////// Définitions ////////////////////// ///
12
13      void CTreeManager::SetCurrentContextId(const StdString & id)
14      {
15         CObjectFactory::SetCurrentContextId(id);
16         CGroupFactory::SetCurrentContextId(id);
17      }
18
19      boost::shared_ptr<CContext> CTreeManager::CreateContext(const StdString & id)
20      {
21         boost::shared_ptr<CContextGroup> group_context = CContext::GetContextGroup();
22         CTreeManager::SetCurrentContextId(id);
23         bool hasctxt = CObjectFactory::HasObject<CContext>(id);
24         
25         boost::shared_ptr<tree::CContext> context =
26               CObjectFactory::CreateObject<tree::CContext>(id);
27         if (!hasctxt) CGroupFactory::AddChild(group_context, context);
28
29#define DECLARE_NODE(Name_, name_) \
30   CObjectFactory::CreateObject<C##Name_##Definition>(C##Name_##Definition::GetDefName());
31#define DECLARE_NODE_PAR(Name_, name_)
32#include "node_type.conf"
33
34         return (context);
35   }
36
37      //--------------------------------------------------------------
38      void CTreeManager::PrintTreeToFile(const StdString & path)
39      {
40         StdOFStream ofs(path.c_str());
41         CTreeManager::PrintTreeToStream(ofs);
42         ofs.close();
43      }
44
45      void CTreeManager::PrintTreeToString(StdString & content)
46      {
47         StdOStringStream ostrstream;
48         tree::CContext::ShowTree(ostrstream);
49         content.assign(CIndentedXml::Indented(ostrstream.str()));
50      }
51
52      void CTreeManager::PrintTreeToStream(StdOStream & out)
53      {
54         StdOStringStream ostrstream;
55         tree::CContext::ShowTree(ostrstream);
56         out << CIndentedXml::Indented(ostrstream.str());
57      }
58
59      //--------------------------------------------------------------
60
61      void CTreeManager::ParseFile(const StdString & filename)
62      { 
63         xml::CXMLParser::ParseFile(filename); 
64      }
65
66      void CTreeManager::ParseString(const StdString & xmlContent)
67      { 
68         xml::CXMLParser::ParseString(xmlContent); 
69      }
70
71      void CTreeManager::ParseStream(StdIStream & stream)
72      { 
73         xml::CXMLParser::ParseStream(stream); 
74      }
75     
76      //--------------------------------------------------------------
77     
78      void CTreeManager::ToBinary(StdOStream & os)
79      {
80         StdString currentContextId =
81            CObjectFactory::GetCurrentContextId();
82         std::vector<boost::shared_ptr<CContext> > def_vector =
83            CContext::GetContextGroup()->getChildList();
84           
85         const StdSize size = def_vector.size();         
86         const ENodeType cenum = CContext::GetType();
87         const ENodeType genum = CContextGroup::GetType();
88         
89         os.write (reinterpret_cast<const char*>(&genum) , sizeof(ENodeType)); 
90         os.write (reinterpret_cast<const char*>(&size) , sizeof(StdSize));
91         
92         for (StdSize i = 0; i < size; i++)
93         {
94            boost::shared_ptr<CContext> context = def_vector[i];         
95            CTreeManager::SetCurrentContextId(context->getId());               
96            const bool hid = context->hasId(); // Toujours vrai
97           
98            os.write (reinterpret_cast<const char*>(&cenum), sizeof(ENodeType));     
99            os.write (reinterpret_cast<const char*>(&hid), sizeof(bool));
100           
101            if (hid)
102            {
103               const StdString & id = context->getId();
104               const StdSize size   = id.size();
105                 
106               os.write (reinterpret_cast<const char*>(&size), sizeof(StdSize));
107               os.write (id.data(), size * sizeof(char));         
108            } 
109           
110            context->toBinary(os);
111         }
112         CTreeManager::SetCurrentContextId(currentContextId);         
113      }
114     
115      void CTreeManager::FromBinary(StdIStream & is)
116      {
117         StdSize ctxtnb = 0;
118         ENodeType renum = Unknown;
119         
120         boost::shared_ptr<CContextGroup> group_context =
121                           CContext::GetContextGroup();
122                           
123         is.read (reinterpret_cast<char*>(&renum), sizeof(ENodeType));   
124         is.read (reinterpret_cast<char*>(&ctxtnb), sizeof(StdSize));
125
126         if (renum != CContextGroup::GetType())
127            ERROR("CTreeManager::FromBinary(StdIStream & is)",
128                  << "[ renum = " << renum << "] Bad type !");
129                 
130                 
131
132         for (StdSize i = 0; i < ctxtnb; i++)
133         {
134            bool hid = false;
135            is.read (reinterpret_cast<char*>(&renum), sizeof(tree::ENodeType));
136            is.read (reinterpret_cast<char*>(&hid), sizeof(bool));
137           
138            if (renum != CContext::GetType())
139               ERROR("CTreeManager::FromBinary(StdIStream & is)",
140                     << "[ renum = " << renum << "] Bad type !");
141                           
142            if (hid)
143            {
144               StdSize size  = 0;
145               is.read (reinterpret_cast<char*>(&size), sizeof(StdSize));
146               StdString id(size, ' ');
147               is.read (const_cast<char *>(id.data()), size * sizeof(char));
148               
149               
150               CTreeManager::SetCurrentContextId(id);               
151               bool hasctxt = CObjectFactory::HasObject<CContext>(id);
152               
153               boost::shared_ptr<CContext> context =
154                  CObjectFactory::CreateObject<CContext>(id);
155                 
156               if (!hasctxt)
157                  CGroupFactory::AddChild(group_context, context);
158               context->fromBinary(is);
159            }
160         }
161      }
162     
163      void CTreeManager::FromBinary(StdString & str)
164      {
165         StdIStringStream istrs(str);
166         CTreeManager::FromBinary(istrs);
167      }
168           
169
170      //--------------------------------------------------------------
171     
172      void CTreeManager::DomainsToBinary  (StdOStream & os)
173      {
174         StdString currentContextId =
175            CObjectFactory::GetCurrentContextId();
176         std::vector<boost::shared_ptr<CContext> > def_vector =
177            CContext::GetContextGroup()->getChildList();
178           
179         const StdSize size = def_vector.size();         
180         const ENodeType cenum = CContext::GetType();
181         const ENodeType genum = CContextGroup::GetType();
182         const ENodeType denum = CDomain::GetType();
183         const ENodeType ienum = CGrid::GetType();
184         
185         os.write (reinterpret_cast<const char*>(&genum) , sizeof(ENodeType)); 
186         os.write (reinterpret_cast<const char*>(&size) , sizeof(StdSize));
187
188         for (StdSize i = 0; i < size; i++)
189         {
190            boost::shared_ptr<CContext> context = def_vector[i];         
191            CTreeManager::SetCurrentContextId(context->getId());               
192            const bool hid = context->hasId(); // Toujours vrai
193           
194            os.write (reinterpret_cast<const char*>(&cenum), sizeof(ENodeType));     
195            os.write (reinterpret_cast<const char*>(&hid), sizeof(bool));
196           
197            if (hid)
198            {
199               const StdString & id = context->getId();
200               const StdSize size   = id.size();
201                 
202               os.write (reinterpret_cast<const char*>(&size), sizeof(StdSize));
203               os.write (id.data(), size * sizeof(char));         
204            } 
205           
206            std::vector<boost::shared_ptr<CDomain> > & alldomain = 
207               CDomain::GetAllVectobject(context->getId());
208            std::vector<boost::shared_ptr<CGrid> > & allgrid = 
209               CGrid::GetAllVectobject(context->getId());
210               
211            const StdSize alldomain_size = alldomain.size(); 
212            const StdSize allgrid_size   = allgrid.size();
213           
214            os.write (reinterpret_cast<const char*>(&alldomain_size), sizeof(StdSize));
215            os.write (reinterpret_cast<const char*>(&allgrid_size), sizeof(StdSize));
216           
217            // Écriture successive des informations binaires de domaine.
218            for (StdSize j = 0; j < alldomain_size; j++)
219            {
220               boost::shared_ptr<CDomain> domain = alldomain[j];
221               bool hid = domain->hasId();
222               
223               os.write (reinterpret_cast<const char*>(&denum), sizeof(ENodeType)); 
224               os.write (reinterpret_cast<const char*>(&hid), sizeof(bool));
225               
226               if (hid)
227               {
228                  const StdString & id = domain->getId();
229                  const StdSize idsize   = id.size();
230                     
231                  os.write (reinterpret_cast<const char*>(&idsize), sizeof(StdSize));
232                  os.write (id.data(), idsize * sizeof(char)); 
233               }         
234               domain->toBinary(os);               
235            }
236           
237            // Écriture successive des informations binaires de grille.
238            for (StdSize j = 0; j < allgrid_size; j++)
239            {
240               boost::shared_ptr<CGrid> grid = allgrid[j];
241               bool hid = grid->hasId();
242               
243               os.write (reinterpret_cast<const char*>(&ienum), sizeof(ENodeType)); 
244               os.write (reinterpret_cast<const char*>(&hid), sizeof(bool));
245               
246               if (hid)
247               {
248                  const StdString & id = grid->getId();
249                  const StdSize idsize   = id.size();
250                     
251                  os.write (reinterpret_cast<const char*>(&idsize), sizeof(StdSize));
252                  os.write (id.data(), idsize * sizeof(char));         
253               }         
254               grid->toBinary(os);               
255            }
256         }
257         CTreeManager::SetCurrentContextId(currentContextId); 
258         
259      }
260     
261      void CTreeManager::DomainsFromBinary(StdIStream & is)
262      {
263         StdSize ctxtnb = 0;
264         ENodeType renum = Unknown;
265         
266         boost::shared_ptr<CContextGroup> group_context =
267                           CContext::GetContextGroup();
268         std::vector<boost::shared_ptr<CContext> > def_vector =
269            CContext::GetContextGroup()->getChildList();
270           
271         const StdSize size = def_vector.size(); 
272                 
273         is.read (reinterpret_cast<char*>(&renum), sizeof(ENodeType));   
274         is.read (reinterpret_cast<char*>(&ctxtnb), sizeof(StdSize));
275
276         if (renum != CContextGroup::GetType())
277            ERROR("CTreeManager::DomainsFromBinary(StdIStream & is)",
278                  << "[ renum = " << renum << "] Bad type !");
279                 
280         if (size != ctxtnb)
281            ERROR("CTreeManager::DomainsFromBinary(StdIStream & is)",
282                  << "[ size = " << size << "] != "<<ctxtnb<<" : Bad context group size !");
283                 
284         for (StdSize i = 0; i < ctxtnb; i++)
285         {
286            boost::shared_ptr<CContext> context = def_vector[i];
287            bool hid = false;
288            is.read (reinterpret_cast<char*>(&renum), sizeof(ENodeType));
289            is.read (reinterpret_cast<char*>(&hid), sizeof(bool));
290           
291            if (renum != CContext::GetType())
292               ERROR("CTreeManager::DomainsFromBinary(StdIStream & is)",
293                     << "[ renum = " << renum << "] Bad type !");
294                           
295            if (hid)
296            {
297               StdSize size  = 0;
298               is.read (reinterpret_cast<char*>(&size), sizeof(StdSize));
299               StdString id(size, ' ');
300               is.read (const_cast<char *>(id.data()), size * sizeof(char));
301               
302               CTreeManager::SetCurrentContextId(id);
303            }
304           
305            std::vector<boost::shared_ptr<CDomain> > & alldomain = 
306               CDomain::GetAllVectobject(context->getId());
307            std::vector<boost::shared_ptr<CGrid> > & allgrid = 
308               CGrid::GetAllVectobject(context->getId());
309               
310            const StdSize allgrid_size = allgrid.size();
311            const StdSize alldomain_size = alldomain.size(); 
312           
313            StdSize read_alldomain_size = 0; 
314            StdSize read_allgrid_size = 0; 
315           
316            is.read (reinterpret_cast<char*>(&read_alldomain_size), sizeof(StdSize));
317            is.read (reinterpret_cast<char*>(&read_allgrid_size), sizeof(StdSize));
318           
319            if (alldomain_size != read_alldomain_size)
320               ERROR("CTreeManager::DomainsFromBinary(StdIStream & is)",
321                     << "[ read_alldomain_size = " << read_alldomain_size
322                     << "] Bad domain group size !");
323                     
324            if (alldomain_size != read_alldomain_size)
325               ERROR("CTreeManager::DomainsFromBinary(StdIStream & is)",
326                     << "[ read_allgrid_size = " << read_allgrid_size
327                     << "] Bad grid group size !");
328                     
329            // Lecture successive des informations binaires de domaine.
330             
331            for (StdSize j = 0; j < alldomain_size; j++)
332            {
333               boost::shared_ptr<CDomain> domain = alldomain[j];
334               bool hid = domain->hasId();
335               ENodeType rrenum = Unknown;
336               
337               is.read (reinterpret_cast<char*>(&rrenum), sizeof(ENodeType));
338               is.read (reinterpret_cast<char*>(&hid), sizeof(bool));
339               
340               if (rrenum != CDomain::GetType())
341                  ERROR("CTreeManager::DomainsFromBinary(StdIStream & is)",
342                        << "[ rrenum = " << rrenum << "] Bad type !");
343
344               if (hid)
345               {
346                  StdSize size  = 0;
347                  is.read (reinterpret_cast<char*>(&size), sizeof(StdSize));
348                  StdString id(size, ' ');
349                  is.read (const_cast<char *>(id.data()), size * sizeof(char));
350                 
351                  if (domain->getId().compare(id) != 0)
352                     ERROR("CTreeManager::DomainsFromBinary(StdIStream & is)",
353                           << "[ id = " <<  id <<" != "<<domain->getId()<< "] Bad id !"); 
354               }
355               domain->fromBinary(is);
356            }
357           
358            // Lecture successive des informations binaires de grille.
359            for (StdSize j = 0; j < allgrid_size; j++)
360            {
361               boost::shared_ptr<CGrid> grid = allgrid[j];
362               bool hid = grid->hasId();
363               ENodeType rrenum = Unknown;
364               
365               is.read (reinterpret_cast<char*>(&rrenum), sizeof(ENodeType));
366               is.read (reinterpret_cast<char*>(&hid), sizeof(bool));
367               
368               if (rrenum != CGrid::GetType())
369                  ERROR("CTreeManager::DomainsFromBinary(StdIStream & is)",
370                        << "[ rrenum = " << rrenum << "] Bad type !");
371
372               if (hid)
373               {
374                  StdSize size  = 0;
375                  is.read (reinterpret_cast<char*>(&size), sizeof(StdSize));
376                  StdString id(size, ' ');
377                  is.read (const_cast<char *>(id.data()), size * sizeof(char));
378                 
379                  if (grid->getId().compare(id) != 0)
380                     ERROR("CTreeManager::DomainsFromBinary(StdIStream & is)",
381                           << "[ id = " << id <<" != "<<grid->getId()<< "] Bad id !"); 
382               }
383               grid->fromBinary(is);
384            }
385           
386         }
387      }
388     
389      void CTreeManager::DomainsFromBinary(StdString & str)
390      {
391         StdIStringStream istrs(str);
392         CTreeManager::DomainsFromBinary(istrs);
393      }
394
395      ///-------------------------------------------------------------
396
397   } // namespace tree
398} // namespace xmlioserver
Note: See TracBrowser for help on using the repository browser.