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