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 | |
---|
7 | namespace 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 | } // namespace tree |
---|
163 | } // namespace xmlioserver |
---|