[591] | 1 | #ifndef __XIOS_CGroupTemplate_impl__ |
---|
| 2 | #define __XIOS_CGroupTemplate_impl__ |
---|
[219] | 3 | |
---|
[591] | 4 | #include "xios_spl.hpp" |
---|
[300] | 5 | #include "event_server.hpp" |
---|
[352] | 6 | #include "object_template.hpp" |
---|
[300] | 7 | #include "group_template.hpp" |
---|
| 8 | #include "context.hpp" |
---|
| 9 | #include "event_client.hpp" |
---|
[1158] | 10 | #include "context_client.hpp" |
---|
[352] | 11 | #include "message.hpp" |
---|
| 12 | #include "type.hpp" |
---|
| 13 | #include "type_util.hpp" |
---|
[2629] | 14 | #include <boost_extract.hpp> |
---|
[300] | 15 | |
---|
[335] | 16 | namespace xios |
---|
[219] | 17 | { |
---|
| 18 | |
---|
| 19 | /// ////////////////////// Définitions ////////////////////// /// |
---|
| 20 | |
---|
| 21 | template <class U, class V, class W> |
---|
| 22 | CGroupTemplate<U, V, W>::CGroupTemplate(void) |
---|
| 23 | : CObjectTemplate<V>() //, V() |
---|
| 24 | , childMap(), childList() |
---|
| 25 | , groupMap(), groupList() |
---|
| 26 | { /* Ne rien faire de plus */ } |
---|
| 27 | |
---|
| 28 | template <class U, class V, class W> |
---|
| 29 | CGroupTemplate<U, V, W>::CGroupTemplate(const StdString & id) |
---|
| 30 | : CObjectTemplate<V>(id) //, V() |
---|
| 31 | , childMap(), childList() |
---|
| 32 | , groupMap(), groupList() |
---|
| 33 | { /* Ne rien faire de plus */ } |
---|
| 34 | |
---|
| 35 | template <class U, class V, class W> |
---|
| 36 | CGroupTemplate<U, V, W>::~CGroupTemplate(void) |
---|
| 37 | { /* Ne rien faire de plus */ } |
---|
| 38 | |
---|
| 39 | template <class U, class V, class W> |
---|
| 40 | StdString CGroupTemplate<U, V, W>::toString(void) const |
---|
| 41 | { |
---|
| 42 | StdOStringStream oss; |
---|
| 43 | StdString name = (this->getId().compare(V::GetDefName()) != 0) |
---|
| 44 | ? V::GetName() : V::GetDefName(); |
---|
| 45 | |
---|
| 46 | oss << "<" << name << " "; |
---|
| 47 | if (this->hasId() && (this->getId().compare(V::GetDefName()) != 0)) |
---|
| 48 | oss << " id=\"" << this->getId() << "\" "; |
---|
| 49 | |
---|
| 50 | if (this->hasChild()) |
---|
| 51 | { |
---|
| 52 | oss << SuperClassAttribute::toString() << ">" << std::endl; |
---|
| 53 | |
---|
[347] | 54 | typename std::vector<V*>::const_iterator |
---|
[219] | 55 | itg = this->groupList.begin(), endg = this->groupList.end(); |
---|
[347] | 56 | typename std::vector<U*>::const_iterator |
---|
[219] | 57 | itc = this->childList.begin(), endc = this->childList.end(); |
---|
| 58 | |
---|
| 59 | for (; itg != endg; itg++) |
---|
| 60 | { |
---|
[347] | 61 | V* group = *itg; |
---|
[219] | 62 | oss << *group << std::endl; |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | for (; itc != endc; itc++) |
---|
| 66 | { |
---|
[347] | 67 | U* child = *itc; |
---|
[219] | 68 | oss << *child << std::endl; |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | oss << "</" << name << " >"; |
---|
| 72 | } |
---|
| 73 | else |
---|
| 74 | { |
---|
| 75 | oss << SuperClassAttribute::toString() << "/>"; |
---|
| 76 | } |
---|
| 77 | return (oss.str()); |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | template <class U, class V, class W> |
---|
| 81 | void CGroupTemplate<U, V, W>::fromString(const StdString & str) |
---|
| 82 | { |
---|
| 83 | ERROR("CGroupTemplate<U, V, W>::toString(void)", |
---|
| 84 | << "[ str = " << str << "] Not implemented yet !"); |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | //--------------------------------------------------------------- |
---|
| 88 | |
---|
| 89 | template <class U, class V, class W> |
---|
| 90 | StdString CGroupTemplate<U, V, W>::GetName(void) |
---|
| 91 | { |
---|
| 92 | return (U::GetName().append("_group")); |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | template <class U, class V, class W> |
---|
| 96 | StdString CGroupTemplate<U, V, W>::GetDefName(void) |
---|
| 97 | { |
---|
| 98 | return (U::GetName().append("_definition")); |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | //--------------------------------------------------------------- |
---|
| 102 | |
---|
| 103 | template <class U, class V, class W> |
---|
[2603] | 104 | const xios_map<StdString, U*>& |
---|
| 105 | CGroupTemplate<U, V, W>::getChildMap(void) const |
---|
[219] | 106 | { |
---|
[2603] | 107 | return (this->childMap); |
---|
[219] | 108 | } |
---|
| 109 | |
---|
| 110 | //--------------------------------------------------------------- |
---|
| 111 | |
---|
| 112 | template <class U, class V, class W> |
---|
[347] | 113 | const xios_map<StdString, V*>& |
---|
[219] | 114 | CGroupTemplate<U, V, W>::getGroupMap(void) const |
---|
| 115 | { |
---|
[352] | 116 | return (this->groupMap); |
---|
[219] | 117 | } |
---|
| 118 | |
---|
| 119 | //--------------------------------------------------------------- |
---|
| 120 | |
---|
| 121 | template <class U, class V, class W> |
---|
| 122 | bool CGroupTemplate<U, V, W>::hasChild(void) const |
---|
| 123 | { |
---|
| 124 | return ((groupList.size() + childList.size()) > 0); |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | //--------------------------------------------------------------- |
---|
| 128 | |
---|
| 129 | template <class U, class V, class W> |
---|
[445] | 130 | void CGroupTemplate<U, V, W>::solveDescInheritance(bool apply, const CAttributeMap * const parent) |
---|
[219] | 131 | { |
---|
| 132 | if (parent != NULL) |
---|
[445] | 133 | SuperClassAttribute::setAttributes(parent, apply); |
---|
[219] | 134 | |
---|
[347] | 135 | typename std::vector<U*>::const_iterator |
---|
[219] | 136 | itc = this->childList.begin(), endc = this->childList.end(); |
---|
[347] | 137 | typename std::vector<V*>::const_iterator |
---|
[219] | 138 | itg = this->groupList.begin(), endg = this->groupList.end(); |
---|
| 139 | |
---|
| 140 | for (; itc != endc; itc++) |
---|
| 141 | { |
---|
[347] | 142 | U* child = *itc; |
---|
[445] | 143 | child->solveDescInheritance(apply,this); |
---|
[219] | 144 | } |
---|
| 145 | |
---|
| 146 | for (; itg != endg; itg++) |
---|
| 147 | { |
---|
[347] | 148 | V* group = *itg; |
---|
[446] | 149 | if (apply) group->solveRefInheritance(); |
---|
[445] | 150 | group->solveDescInheritance(apply,this); |
---|
[219] | 151 | } |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | //--------------------------------------------------------------- |
---|
| 155 | |
---|
| 156 | template <class U, class V, class W> |
---|
[347] | 157 | void CGroupTemplate<U, V, W>::getAllChildren(std::vector<U*>& allc) const |
---|
[219] | 158 | { |
---|
| 159 | allc.insert (allc.end(), childList.begin(), childList.end()); |
---|
[347] | 160 | typename std::vector<V*>::const_iterator |
---|
[219] | 161 | itg = this->groupList.begin(), endg = this->groupList.end(); |
---|
| 162 | |
---|
| 163 | for (; itg != endg; itg++) |
---|
| 164 | { |
---|
[347] | 165 | V* group = *itg; |
---|
[219] | 166 | group->getAllChildren(allc); |
---|
| 167 | } |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | //--------------------------------------------------------------- |
---|
| 171 | |
---|
| 172 | template <class U, class V, class W> |
---|
[347] | 173 | std::vector<U*> CGroupTemplate<U, V, W>::getAllChildren(void) const |
---|
[219] | 174 | { |
---|
[347] | 175 | std::vector<U*> allc; |
---|
[219] | 176 | this->getAllChildren(allc); |
---|
| 177 | return (allc); |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | //--------------------------------------------------------------- |
---|
| 181 | |
---|
| 182 | template <class U, class V, class W> |
---|
| 183 | void CGroupTemplate<U, V, W>::solveRefInheritance(void) |
---|
| 184 | { /* Ne rien faire de plus */ } |
---|
[300] | 185 | |
---|
[219] | 186 | |
---|
| 187 | ///-------------------------------------------------------------- |
---|
| 188 | |
---|
[346] | 189 | |
---|
[300] | 190 | template <class U, class V, class W> |
---|
[347] | 191 | U* CGroupTemplate<U, V, W>::createChild(const string& id) |
---|
[300] | 192 | { |
---|
[347] | 193 | return CGroupFactory::CreateChild<V>(this->getShared(), id).get() ; |
---|
[300] | 194 | } |
---|
[346] | 195 | |
---|
| 196 | template <class U, class V, class W> |
---|
[347] | 197 | void CGroupTemplate<U, V, W>::addChild(U* child) |
---|
[346] | 198 | { |
---|
[352] | 199 | return CGroupFactory::AddChild<V>(this->getShared(),child->getShared()) ; |
---|
[346] | 200 | } |
---|
[300] | 201 | |
---|
| 202 | template <class U, class V, class W> |
---|
[347] | 203 | V* CGroupTemplate<U, V, W>::createChildGroup(const string& id) |
---|
[300] | 204 | { |
---|
[347] | 205 | return CGroupFactory::CreateGroup<V>(this->getShared(), id).get() ; |
---|
[300] | 206 | } |
---|
| 207 | |
---|
[346] | 208 | template <class U, class V, class W> |
---|
[347] | 209 | void CGroupTemplate<U, V, W>::addChildGroup(V* childGroup) |
---|
[346] | 210 | { |
---|
[347] | 211 | return CGroupFactory::AddGroup<V>(this->getShared(), childGroup->getShared()) ; |
---|
[346] | 212 | } |
---|
[300] | 213 | |
---|
| 214 | template <class U, class V, class W> |
---|
[1875] | 215 | void CGroupTemplate<U, V, W>::sendCreateChild(const string& id, CContextClient* client, const string& objectId) |
---|
[1009] | 216 | { |
---|
| 217 | |
---|
| 218 | CEventClient event(this->getType(),EVENT_ID_CREATE_CHILD) ; |
---|
[1021] | 219 | if (client->isServerLeader()) |
---|
[1009] | 220 | { |
---|
| 221 | CMessage msg ; |
---|
[1875] | 222 | if (objectId.empty()) msg << this->getId(); |
---|
| 223 | else msg << objectId; |
---|
[1009] | 224 | msg<<id ; |
---|
[1021] | 225 | const std::list<int>& ranks = client->getRanksServerLeader(); |
---|
[1009] | 226 | for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank) |
---|
| 227 | event.push(*itRank,1,msg) ; |
---|
[1021] | 228 | client->sendEvent(event) ; |
---|
[1009] | 229 | } |
---|
[1021] | 230 | else client->sendEvent(event) ; |
---|
[1009] | 231 | } |
---|
| 232 | |
---|
| 233 | template <class U, class V, class W> |
---|
[1875] | 234 | void CGroupTemplate<U, V, W>::sendCreateChildGroup(const string& id, CContextClient* client, const string& objectId) |
---|
[300] | 235 | { |
---|
[1784] | 236 | CEventClient event(this->getType(),EVENT_ID_CREATE_CHILD_GROUP) ; |
---|
| 237 | if (client->isServerLeader()) |
---|
| 238 | { |
---|
| 239 | CMessage msg ; |
---|
[1875] | 240 | if (objectId.empty()) msg << this->getId(); |
---|
| 241 | else msg << objectId; |
---|
[1784] | 242 | msg<<id ; |
---|
| 243 | const std::list<int>& ranks = client->getRanksServerLeader(); |
---|
| 244 | for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank) |
---|
| 245 | event.push(*itRank,1,msg) ; |
---|
| 246 | client->sendEvent(event) ; |
---|
| 247 | } |
---|
| 248 | else client->sendEvent(event) ; |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | |
---|
[300] | 252 | template <class U, class V, class W> |
---|
| 253 | void CGroupTemplate<U, V, W>::recvCreateChild(CEventServer& event) |
---|
| 254 | { |
---|
| 255 | CBufferIn* buffer=event.subEvents.begin()->buffer; |
---|
| 256 | string id; |
---|
| 257 | *buffer>>id ; |
---|
| 258 | V::get(id)->recvCreateChild(*buffer) ; |
---|
| 259 | } |
---|
| 260 | |
---|
| 261 | |
---|
| 262 | template <class U, class V, class W> |
---|
| 263 | void CGroupTemplate<U, V, W>::recvCreateChild(CBufferIn& buffer) |
---|
| 264 | { |
---|
| 265 | string id ; |
---|
| 266 | buffer>>id ; |
---|
| 267 | createChild(id) ; |
---|
| 268 | } |
---|
| 269 | |
---|
| 270 | template <class U, class V, class W> |
---|
| 271 | void CGroupTemplate<U, V, W>::recvCreateChildGroup(CEventServer& event) |
---|
| 272 | { |
---|
| 273 | |
---|
| 274 | CBufferIn* buffer=event.subEvents.begin()->buffer; |
---|
| 275 | string id; |
---|
| 276 | *buffer>>id ; |
---|
| 277 | V::get(id)->recvCreateChildGroup(*buffer) ; |
---|
| 278 | } |
---|
| 279 | |
---|
| 280 | |
---|
| 281 | template <class U, class V, class W> |
---|
| 282 | void CGroupTemplate<U, V, W>::recvCreateChildGroup(CBufferIn& buffer) |
---|
| 283 | { |
---|
| 284 | string id ; |
---|
| 285 | buffer>>id ; |
---|
| 286 | createChildGroup(id) ; |
---|
| 287 | } |
---|
| 288 | |
---|
| 289 | |
---|
| 290 | template <class U, class V, class W> |
---|
| 291 | bool CGroupTemplate<U, V, W>::dispatchEvent(CEventServer& event) |
---|
| 292 | { |
---|
| 293 | if (CObjectTemplate<V>::dispatchEvent(event)) return true ; |
---|
| 294 | else |
---|
| 295 | { |
---|
| 296 | switch(event.type) |
---|
| 297 | { |
---|
| 298 | case EVENT_ID_CREATE_CHILD : |
---|
| 299 | recvCreateChild(event) ; |
---|
| 300 | return true ; |
---|
| 301 | break ; |
---|
| 302 | |
---|
| 303 | case EVENT_ID_CREATE_CHILD_GROUP : |
---|
| 304 | recvCreateChildGroup(event) ; |
---|
| 305 | return true ; |
---|
| 306 | break ; |
---|
| 307 | |
---|
| 308 | default : |
---|
| 309 | return false ; |
---|
| 310 | } |
---|
| 311 | } |
---|
| 312 | } |
---|
| 313 | |
---|
[352] | 314 | template <class U, class V, class W> |
---|
[2614] | 315 | void CGroupTemplate<U, V, W>::parse(xml::CXMLNode & node) |
---|
| 316 | { |
---|
| 317 | this->parse(node, true); |
---|
| 318 | } |
---|
| 319 | |
---|
| 320 | template <class U, class V, class W> |
---|
| 321 | void CGroupTemplate<U, V, W>::parse(xml::CXMLNode & node, bool withAttr, const std::set<StdString>& parseContextList) |
---|
| 322 | { |
---|
| 323 | ERROR("void CGroupTemplate<U, V, W>::parse(xml::CXMLNode & node, bool withAttr, const std::set<StdString>& parseContextList)", |
---|
| 324 | <<"must not be called by this kind of object : "<<GetName() ) ; |
---|
| 325 | } |
---|
| 326 | |
---|
| 327 | template <class U, class V, class W> |
---|
[352] | 328 | void CGroupTemplate<U, V, W>::parse(xml::CXMLNode & node, bool withAttr) |
---|
| 329 | { |
---|
| 330 | |
---|
| 331 | StdString name = node.getElementName(); |
---|
| 332 | xml::THashAttributes attributes = node.getAttributes(); |
---|
| 333 | if (withAttr) |
---|
| 334 | { |
---|
| 335 | CGroupTemplate<U, V, W>::SuperClass::parse(node); |
---|
[2614] | 336 | if (attributes.end() != attributes.find("src")) xml::CXMLParser::ParseInclude(attributes["src"].c_str(), *this); |
---|
[352] | 337 | } |
---|
| 338 | |
---|
| 339 | // PARSING POUR GESTION DES ENFANTS |
---|
| 340 | V* group_ptr = (this->hasId()) |
---|
| 341 | ? V::get(this->getId()) |
---|
[2629] | 342 | : xios_polymorphic_downcast<V*>(this); |
---|
[352] | 343 | |
---|
| 344 | if (!(node.goToChildElement())) |
---|
| 345 | { |
---|
| 346 | if (this->hasId()) |
---|
| 347 | { |
---|
| 348 | DEBUG(<< "L'objet de type \'" << V::GetName() |
---|
| 349 | << "\' nommé \'" << this->getId() |
---|
| 350 | << "\' ne contient pas d\'enfant !"); |
---|
| 351 | } |
---|
| 352 | } |
---|
| 353 | else |
---|
| 354 | { |
---|
| 355 | do { // Parcours pour traitement. |
---|
| 356 | |
---|
| 357 | StdString name = node.getElementName(); |
---|
| 358 | attributes.clear(); |
---|
| 359 | attributes = node.getAttributes(); |
---|
| 360 | |
---|
| 361 | if (name.compare(V::GetName()) == 0) |
---|
| 362 | { |
---|
| 363 | if (attributes.end() == attributes.find("id")) |
---|
| 364 | CGroupFactory::CreateGroup(group_ptr->getShared())->parse(node); |
---|
| 365 | else |
---|
| 366 | CGroupFactory::CreateGroup(group_ptr->getShared(), attributes["id"])->parse(node); |
---|
| 367 | continue; |
---|
| 368 | } |
---|
| 369 | |
---|
| 370 | if (name.compare(U::GetName()) == 0) |
---|
| 371 | { |
---|
| 372 | if (attributes.end() == attributes.find("id")) |
---|
| 373 | CGroupFactory::CreateChild(group_ptr->getShared())->parse(node); |
---|
| 374 | else |
---|
| 375 | CGroupFactory::CreateChild(group_ptr->getShared(), attributes["id"])->parse(node); |
---|
| 376 | continue; |
---|
| 377 | } |
---|
| 378 | |
---|
| 379 | DEBUG(<< "Dans le contexte \'" << CContext::getCurrent()->getId() |
---|
| 380 | << "\', un objet de type \'" << V::GetName() |
---|
| 381 | << "\' ne peut contenir qu'un objet de type \'" << V::GetName() |
---|
| 382 | << "\' ou de type \'" << U::GetName() |
---|
| 383 | << "\' (reçu : " << name << ") !"); |
---|
| 384 | |
---|
| 385 | } while (node.goToNextElement()); |
---|
| 386 | node.goToParentElement(); // Retour au parent |
---|
| 387 | } |
---|
| 388 | } |
---|
[472] | 389 | |
---|
| 390 | template <class U, class V, class W> |
---|
| 391 | void CGroupTemplate<U, V, W>::parseChild(xml::CXMLNode & node) |
---|
| 392 | { |
---|
| 393 | |
---|
| 394 | |
---|
| 395 | // PARSING POUR GESTION DES ENFANTS |
---|
| 396 | V* group_ptr = (this->hasId()) |
---|
| 397 | ? V::get(this->getId()) |
---|
[2629] | 398 | : xios_polymorphic_downcast<V*>(this); |
---|
[472] | 399 | |
---|
| 400 | StdString name = node.getElementName(); |
---|
| 401 | xml::THashAttributes attributes = node.getAttributes(); |
---|
| 402 | |
---|
| 403 | if (name.compare(V::GetName()) == 0) |
---|
| 404 | { |
---|
| 405 | if (attributes.end() == attributes.find("id")) |
---|
| 406 | CGroupFactory::CreateGroup(group_ptr->getShared())->parse(node); |
---|
| 407 | else |
---|
| 408 | CGroupFactory::CreateGroup(group_ptr->getShared(), attributes["id"])->parse(node); |
---|
| 409 | return ; |
---|
| 410 | } |
---|
| 411 | else if (name.compare(U::GetName()) == 0) |
---|
| 412 | { |
---|
| 413 | if (attributes.end() == attributes.find("id")) |
---|
| 414 | CGroupFactory::CreateChild(group_ptr->getShared())->parse(node); |
---|
| 415 | else |
---|
| 416 | CGroupFactory::CreateChild(group_ptr->getShared(), attributes["id"])->parse(node); |
---|
| 417 | return ; |
---|
| 418 | } |
---|
| 419 | |
---|
| 420 | DEBUG(<< "Dans le contexte \'" << CContext::getCurrent()->getId() |
---|
| 421 | << "\', un objet de type \'" << V::GetName() |
---|
| 422 | << "\' ne peut contenir qu'un objet de type \'" << V::GetName() |
---|
| 423 | << "\' ou de type \'" << U::GetName() |
---|
| 424 | << "\' (reçu : " << name << ") !"); |
---|
| 425 | |
---|
| 426 | } |
---|
[335] | 427 | } // namespace xios |
---|
[219] | 428 | |
---|
| 429 | |
---|
[591] | 430 | #endif // __XIOS_CGroupTemplate_impl__ |
---|