source: XMLIO_V2/dev/dev_rv/src/xmlio/manager/tree_manager.cpp @ 177

Last change on this file since 177 was 175, checked in by hozdoba, 13 years ago
File size: 12.1 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 "../config/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(StdSize));   
124         is.read (reinterpret_cast<char*>(&ctxtnb), sizeof(ENodeType));
125
126         if (renum != CContextGroup::GetType())
127            ERROR("CTreeManager::FromBinary(StdIStream & is)",
128                  << "[ renum = " << renum << "] Bad type !");
129
130         for (StdSize i = 0; i < ctxtnb; i++)
131         {
132            bool hid = false;
133            is.read (reinterpret_cast<char*>(&renum), sizeof(tree::ENodeType));
134            is.read (reinterpret_cast<char*>(&hid), sizeof(bool));
135           
136            if (renum != CContext::GetType())
137               ERROR("CTreeManager::FromBinary(StdIStream & is)",
138                     << "[ renum = " << renum << "] Bad type !");
139                           
140            if (hid)
141            {
142               StdSize size  = 0;
143               is.read (reinterpret_cast<char*>(&size), sizeof(StdSize));
144               StdString id(size, ' ');
145               is.read (const_cast<char *>(id.data()), size * sizeof(char));
146               
147               CTreeManager::SetCurrentContextId(id);               
148               bool hasctxt = CObjectFactory::HasObject<CContext>(id);
149               
150               boost::shared_ptr<CContext> context =
151                  CObjectFactory::CreateObject<CContext>(id);
152                 
153               if (!hasctxt)
154                  CGroupFactory::AddChild(group_context, context);
155               context->fromBinary(is);
156            }
157         }
158      }
159     
160      //--------------------------------------------------------------
161     
162      void CTreeManager::DomainsToBinary  (StdOStream & os)
163      {
164         StdString currentContextId =
165            CObjectFactory::GetCurrentContextId();
166         std::vector<boost::shared_ptr<CContext> > def_vector =
167            CContext::GetContextGroup()->getChildList();
168           
169         const StdSize size = def_vector.size();         
170         const ENodeType cenum = CContext::GetType();
171         const ENodeType genum = CContextGroup::GetType();
172         const ENodeType denum = CDomain::GetType();
173         
174         os.write (reinterpret_cast<const char*>(&genum) , sizeof(ENodeType)); 
175         os.write (reinterpret_cast<const char*>(&size) , sizeof(StdSize));
176         
177         for (StdSize i = 0; i < size; i++)
178         {
179            boost::shared_ptr<CContext> context = def_vector[i];         
180            CTreeManager::SetCurrentContextId(context->getId());               
181            const bool hid = context->hasId(); // Toujours vrai
182           
183            os.write (reinterpret_cast<const char*>(&cenum), sizeof(ENodeType));     
184            os.write (reinterpret_cast<const char*>(&hid), sizeof(bool));
185           
186            if (hid)
187            {
188               const StdString & id = context->getId();
189               const StdSize size   = id.size();
190                 
191               os.write (reinterpret_cast<const char*>(&size), sizeof(StdSize));
192               os.write (id.data(), size * sizeof(char));         
193            } 
194           
195            std::vector<boost::shared_ptr<CDomain> > & alldomain = 
196               CDomain::GetAllVectobject(context->getId());
197            const StdSize alldomain_size = alldomain.size(); 
198           
199            os.write (reinterpret_cast<const char*>(&alldomain_size), sizeof(StdSize));
200           
201            for (StdSize j = 0; j < alldomain_size; j++)
202            {
203               boost::shared_ptr<CDomain> domain = alldomain[j];
204               bool hid = domain->hasId();
205               
206               os.write (reinterpret_cast<const char*>(&denum), sizeof(ENodeType)); 
207               os.write (reinterpret_cast<const char*>(&hid), sizeof(bool));
208               
209               if (hid)
210               {
211                  const StdString & id = domain->getId();
212                  const StdSize idsize   = id.size();
213                     
214                  os.write (reinterpret_cast<const char*>(&idsize), sizeof(StdSize));
215                  os.write (id.data(), idsize * sizeof(char));         
216               }         
217               domain->toBinary(os);
218               
219            }
220         }
221         CTreeManager::SetCurrentContextId(currentContextId); 
222         
223      }
224     
225      void CTreeManager::DomainsFromBinary(StdIStream & is)
226      {
227         StdSize ctxtnb = 0;
228         ENodeType renum = Unknown;
229         
230         boost::shared_ptr<CContextGroup> group_context =
231                           CContext::GetContextGroup();
232         std::vector<boost::shared_ptr<CContext> > def_vector =
233            CContext::GetContextGroup()->getChildList();
234           
235         const StdSize size = def_vector.size(); 
236                 
237         is.read (reinterpret_cast<char*>(&renum), sizeof(StdSize));   
238         is.read (reinterpret_cast<char*>(&ctxtnb), sizeof(ENodeType));
239
240         if (renum != CContextGroup::GetType())
241            ERROR("CTreeManager::DomainsFromBinary(StdIStream & is)",
242                  << "[ renum = " << renum << "] Bad type !");
243                 
244         if (size != ctxtnb)
245            ERROR("CTreeManager::DomainsFromBinary(StdIStream & is)",
246                  << "[ size = " << size << "] Bad context group size !");
247                 
248         for (StdSize i = 0; i < ctxtnb; i++)
249         {
250            boost::shared_ptr<CContext> context = def_vector[i];
251            bool hid = false;
252            is.read (reinterpret_cast<char*>(&renum), sizeof(ENodeType));
253            is.read (reinterpret_cast<char*>(&hid), sizeof(bool));
254           
255            if (renum != CContext::GetType())
256               ERROR("CTreeManager::DomainsFromBinary(StdIStream & is)",
257                     << "[ renum = " << renum << "] Bad type !");
258                           
259            if (hid)
260            {
261               StdSize size  = 0;
262               is.read (reinterpret_cast<char*>(&size), sizeof(StdSize));
263               StdString id(size, ' ');
264               is.read (const_cast<char *>(id.data()), size * sizeof(char));
265               
266               CTreeManager::SetCurrentContextId(id);
267            }
268           
269            std::vector<boost::shared_ptr<CDomain> > & alldomain = 
270               CDomain::GetAllVectobject(context->getId());
271            const StdSize alldomain_size = alldomain.size(); 
272            StdSize read_alldomain_size = 0; 
273           
274            is.read (reinterpret_cast<char*>(&read_alldomain_size), sizeof(StdSize));
275           
276            if (alldomain_size != read_alldomain_size)
277               ERROR("CTreeManager::DomainsFromBinary(StdIStream & is)",
278                     << "[ read_alldomain_size = " << read_alldomain_size
279                     << "] Bad domain group size !");
280                     
281            for (StdSize j = 0; j < alldomain_size; j++)
282            {
283               boost::shared_ptr<CDomain> domain = alldomain[j];
284               bool hid = domain->hasId();
285               ENodeType rrenum = Unknown;
286               
287               is.read (reinterpret_cast<char*>(&rrenum), sizeof(ENodeType));
288               is.read (reinterpret_cast<char*>(&hid), sizeof(bool));
289               
290               if (rrenum != CDomain::GetType())
291                  ERROR("CTreeManager::DomainsFromBinary(StdIStream & is)",
292                        << "[ rrenum = " << rrenum << "] Bad type !");
293
294               if (hid)
295               {
296                  StdSize size  = 0;
297                  is.read (reinterpret_cast<char*>(&size), sizeof(StdSize));
298                  StdString id(size, ' ');
299                  is.read (const_cast<char *>(id.data()), size * sizeof(char));
300                 
301                  if (domain->getId().compare(id) != 0)
302                     ERROR("CTreeManager::DomainsFromBinary(StdIStream & is)",
303                           << "[ id = " << id << "] Bad id !"); 
304               }
305               domain->fromBinary(is);
306            }
307         }
308      }
309
310      ///-------------------------------------------------------------
311
312   } // namespace tree
313} // namespace xmlioserver
Note: See TracBrowser for help on using the repository browser.