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

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