source: XMLIO_V2/dev/common/src/xmlio/manager/tree_manager.cpp @ 278

Last change on this file since 278 was 274, checked in by hozdoba, 13 years ago

nouvelle interface fortran et corrections

File size: 15.7 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//         std::cout << "Nombre de contexte :" << ctxtnb << std::endl;
132
133         for (StdSize i = 0; i < ctxtnb; i++)
134         {
135            bool hid = false;
136            is.read (reinterpret_cast<char*>(&renum), sizeof(tree::ENodeType));
137            is.read (reinterpret_cast<char*>(&hid), sizeof(bool));
138           
139            if (renum != CContext::GetType())
140               ERROR("CTreeManager::FromBinary(StdIStream & is)",
141                     << "[ renum = " << renum << "] Bad type !");
142                           
143            if (hid)
144            {
145               StdSize size  = 0;
146               is.read (reinterpret_cast<char*>(&size), sizeof(StdSize));
147               StdString id(size, ' ');
148               is.read (const_cast<char *>(id.data()), size * sizeof(char));
149               
150//               std::cout << "context ::::" << id << std::endl;
151               
152               CTreeManager::SetCurrentContextId(id);               
153               bool hasctxt = CObjectFactory::HasObject<CContext>(id);
154               
155               boost::shared_ptr<CContext> context =
156                  CObjectFactory::CreateObject<CContext>(id);
157                 
158               if (!hasctxt)
159                  CGroupFactory::AddChild(group_context, context);
160               context->fromBinary(is);
161            }
162         }
163      }
164     
165      void CTreeManager::FromBinary(StdString & str)
166      {
167         StdIStringStream istrs(str);
168         CTreeManager::FromBinary(istrs);
169      }
170           
171
172      //--------------------------------------------------------------
173     
174      void CTreeManager::DomainsToBinary  (StdOStream & os)
175      {
176         StdString currentContextId =
177            CObjectFactory::GetCurrentContextId();
178         std::vector<boost::shared_ptr<CContext> > def_vector =
179            CContext::GetContextGroup()->getChildList();
180           
181         const StdSize size = def_vector.size();         
182         const ENodeType cenum = CContext::GetType();
183         const ENodeType genum = CContextGroup::GetType();
184         const ENodeType denum = CDomain::GetType();
185         const ENodeType ienum = CGrid::GetType();
186         
187         os.write (reinterpret_cast<const char*>(&genum) , sizeof(ENodeType)); 
188         os.write (reinterpret_cast<const char*>(&size) , sizeof(StdSize));
189         
190         for (StdSize i = 0; i < size; i++)
191         {
192            boost::shared_ptr<CContext> context = def_vector[i];         
193            CTreeManager::SetCurrentContextId(context->getId());               
194            const bool hid = context->hasId(); // Toujours vrai
195           
196            os.write (reinterpret_cast<const char*>(&cenum), sizeof(ENodeType));     
197            os.write (reinterpret_cast<const char*>(&hid), sizeof(bool));
198           
199            if (hid)
200            {
201               const StdString & id = context->getId();
202               const StdSize size   = id.size();
203                 
204               os.write (reinterpret_cast<const char*>(&size), sizeof(StdSize));
205               os.write (id.data(), size * sizeof(char));         
206            } 
207           
208            std::vector<boost::shared_ptr<CDomain> > & alldomain = 
209               CDomain::GetAllVectobject(context->getId());
210            std::vector<boost::shared_ptr<CGrid> > & allgrid = 
211               CGrid::GetAllVectobject(context->getId());
212               
213            const StdSize alldomain_size = alldomain.size(); 
214            const StdSize allgrid_size   = allgrid.size();
215           
216            os.write (reinterpret_cast<const char*>(&alldomain_size), sizeof(StdSize));
217            os.write (reinterpret_cast<const char*>(&allgrid_size), sizeof(StdSize));
218           
219            // Écriture successive des informations binaires de domaine.
220            for (StdSize j = 0; j < alldomain_size; j++)
221            {
222               boost::shared_ptr<CDomain> domain = alldomain[j];
223               bool hid = domain->hasId();
224               
225               os.write (reinterpret_cast<const char*>(&denum), sizeof(ENodeType)); 
226               os.write (reinterpret_cast<const char*>(&hid), sizeof(bool));
227               
228               if (hid)
229               {
230                  const StdString & id = domain->getId();
231                  const StdSize idsize   = id.size();
232                     
233                  os.write (reinterpret_cast<const char*>(&idsize), sizeof(StdSize));
234                  os.write (id.data(), idsize * sizeof(char));         
235               }         
236               domain->toBinary(os);               
237            }
238           
239            // Écriture successive des informations binaires de grille.
240            for (StdSize j = 0; j < allgrid_size; j++)
241            {
242               boost::shared_ptr<CGrid> grid = allgrid[j];
243               bool hid = grid->hasId();
244               
245               os.write (reinterpret_cast<const char*>(&ienum), sizeof(ENodeType)); 
246               os.write (reinterpret_cast<const char*>(&hid), sizeof(bool));
247               
248               if (hid)
249               {
250                  const StdString & id = grid->getId();
251                  const StdSize idsize   = id.size();
252                     
253                  os.write (reinterpret_cast<const char*>(&idsize), sizeof(StdSize));
254                  os.write (id.data(), idsize * sizeof(char));         
255               }         
256               grid->toBinary(os);               
257            }
258         }
259         CTreeManager::SetCurrentContextId(currentContextId); 
260         
261      }
262     
263      void CTreeManager::DomainsFromBinary(StdIStream & is)
264      {
265         StdSize ctxtnb = 0;
266         ENodeType renum = Unknown;
267         
268         boost::shared_ptr<CContextGroup> group_context =
269                           CContext::GetContextGroup();
270         std::vector<boost::shared_ptr<CContext> > def_vector =
271            CContext::GetContextGroup()->getChildList();
272           
273         const StdSize size = def_vector.size(); 
274                 
275         is.read (reinterpret_cast<char*>(&renum), sizeof(StdSize));   
276         is.read (reinterpret_cast<char*>(&ctxtnb), sizeof(ENodeType));
277
278         if (renum != CContextGroup::GetType())
279            ERROR("CTreeManager::DomainsFromBinary(StdIStream & is)",
280                  << "[ renum = " << renum << "] Bad type !");
281                 
282         if (size != ctxtnb)
283            ERROR("CTreeManager::DomainsFromBinary(StdIStream & is)",
284                  << "[ size = " << size << "] Bad context group size !");
285                 
286         for (StdSize i = 0; i < ctxtnb; i++)
287         {
288            boost::shared_ptr<CContext> context = def_vector[i];
289            bool hid = false;
290            is.read (reinterpret_cast<char*>(&renum), sizeof(ENodeType));
291            is.read (reinterpret_cast<char*>(&hid), sizeof(bool));
292           
293            if (renum != CContext::GetType())
294               ERROR("CTreeManager::DomainsFromBinary(StdIStream & is)",
295                     << "[ renum = " << renum << "] Bad type !");
296                           
297            if (hid)
298            {
299               StdSize size  = 0;
300               is.read (reinterpret_cast<char*>(&size), sizeof(StdSize));
301               StdString id(size, ' ');
302               is.read (const_cast<char *>(id.data()), size * sizeof(char));
303               
304               CTreeManager::SetCurrentContextId(id);
305            }
306           
307            std::vector<boost::shared_ptr<CDomain> > & alldomain = 
308               CDomain::GetAllVectobject(context->getId());
309            std::vector<boost::shared_ptr<CGrid> > & allgrid = 
310               CGrid::GetAllVectobject(context->getId());
311               
312            const StdSize allgrid_size = allgrid.size();
313            const StdSize alldomain_size = alldomain.size(); 
314           
315            StdSize read_alldomain_size = 0; 
316            StdSize read_allgrid_size = 0; 
317           
318            is.read (reinterpret_cast<char*>(&read_alldomain_size), sizeof(StdSize));
319            is.read (reinterpret_cast<char*>(&read_allgrid_size), sizeof(StdSize));
320           
321            if (alldomain_size != read_alldomain_size)
322               ERROR("CTreeManager::DomainsFromBinary(StdIStream & is)",
323                     << "[ read_alldomain_size = " << read_alldomain_size
324                     << "] Bad domain group size !");
325                     
326            if (alldomain_size != read_alldomain_size)
327               ERROR("CTreeManager::DomainsFromBinary(StdIStream & is)",
328                     << "[ read_allgrid_size = " << read_allgrid_size
329                     << "] Bad grid group size !");
330                     
331            // Lecture successive des informations binaires de domaine.
332            for (StdSize j = 0; j < alldomain_size; j++)
333            {
334               boost::shared_ptr<CDomain> domain = alldomain[j];
335               bool hid = domain->hasId();
336               ENodeType rrenum = Unknown;
337               
338               is.read (reinterpret_cast<char*>(&rrenum), sizeof(ENodeType));
339               is.read (reinterpret_cast<char*>(&hid), sizeof(bool));
340               
341               if (rrenum != CDomain::GetType())
342                  ERROR("CTreeManager::DomainsFromBinary(StdIStream & is)",
343                        << "[ rrenum = " << rrenum << "] Bad type !");
344
345               if (hid)
346               {
347                  StdSize size  = 0;
348                  is.read (reinterpret_cast<char*>(&size), sizeof(StdSize));
349                  StdString id(size, ' ');
350                  is.read (const_cast<char *>(id.data()), size * sizeof(char));
351                 
352                  if (domain->getId().compare(id) != 0)
353                     ERROR("CTreeManager::DomainsFromBinary(StdIStream & is)",
354                           << "[ id = " << id << "] Bad id !"); 
355               }
356               domain->fromBinary(is);
357            }
358           
359            // Lecture successive des informations binaires de grille.
360            for (StdSize j = 0; j < allgrid_size; j++)
361            {
362               boost::shared_ptr<CGrid> grid = allgrid[j];
363               bool hid = grid->hasId();
364               ENodeType rrenum = Unknown;
365               
366               is.read (reinterpret_cast<char*>(&rrenum), sizeof(ENodeType));
367               is.read (reinterpret_cast<char*>(&hid), sizeof(bool));
368               
369               if (rrenum != CGrid::GetType())
370                  ERROR("CTreeManager::DomainsFromBinary(StdIStream & is)",
371                        << "[ rrenum = " << rrenum << "] Bad type !");
372
373               if (hid)
374               {
375                  StdSize size  = 0;
376                  is.read (reinterpret_cast<char*>(&size), sizeof(StdSize));
377                  StdString id(size, ' ');
378                  is.read (const_cast<char *>(id.data()), size * sizeof(char));
379                 
380                  if (grid->getId().compare(id) != 0)
381                     ERROR("CTreeManager::DomainsFromBinary(StdIStream & is)",
382                           << "[ id = " << id << "] Bad id !"); 
383               }
384               grid->fromBinary(is);
385            }
386           
387         }
388      }
389     
390      void CTreeManager::DomainsFromBinary(StdString & str)
391      {
392         StdIStringStream istrs(str);
393         CTreeManager::DomainsFromBinary(istrs);
394      }
395
396      ///-------------------------------------------------------------
397
398   } // namespace tree
399} // namespace xmlioserver
Note: See TracBrowser for help on using the repository browser.