#ifndef __XIOS_CGroupTemplate_impl__ #define __XIOS_CGroupTemplate_impl__ #include "xios_spl.hpp" #include "event_server.hpp" #include "object_template.hpp" #include "group_template.hpp" #include "context.hpp" #include "event_client.hpp" #include "context_client.hpp" #include "message.hpp" #include "type.hpp" #include "type_util.hpp" #include namespace xios { /// ////////////////////// Définitions ////////////////////// /// template CGroupTemplate::CGroupTemplate(void) : CObjectTemplate() //, V() , childMap(), childList() , groupMap(), groupList() { /* Ne rien faire de plus */ } template CGroupTemplate::CGroupTemplate(const StdString & id) : CObjectTemplate(id) //, V() , childMap(), childList() , groupMap(), groupList() { /* Ne rien faire de plus */ } template CGroupTemplate::~CGroupTemplate(void) { /* Ne rien faire de plus */ } template StdString CGroupTemplate::toString(void) const { StdOStringStream oss; StdString name = (this->getId().compare(V::GetDefName()) != 0) ? V::GetName() : V::GetDefName(); oss << "<" << name << " "; if (this->hasId() && (this->getId().compare(V::GetDefName()) != 0)) oss << " id=\"" << this->getId() << "\" "; if (this->hasChild()) { oss << SuperClassAttribute::toString() << ">" << std::endl; typename std::vector::const_iterator itg = this->groupList.begin(), endg = this->groupList.end(); typename std::vector::const_iterator itc = this->childList.begin(), endc = this->childList.end(); for (; itg != endg; itg++) { V* group = *itg; oss << *group << std::endl; } for (; itc != endc; itc++) { U* child = *itc; oss << *child << std::endl; } oss << ""; } else { oss << SuperClassAttribute::toString() << "/>"; } return (oss.str()); } template void CGroupTemplate::fromString(const StdString & str) { ERROR("CGroupTemplate::toString(void)", << "[ str = " << str << "] Not implemented yet !"); } //--------------------------------------------------------------- template StdString CGroupTemplate::GetName(void) { return (U::GetName().append("_group")); } template StdString CGroupTemplate::GetDefName(void) { return (U::GetName().append("_definition")); } //--------------------------------------------------------------- template const xios_map& CGroupTemplate::getChildMap(void) const { return (this->childMap); } //--------------------------------------------------------------- template const xios_map& CGroupTemplate::getGroupMap(void) const { return (this->groupMap); } //--------------------------------------------------------------- template bool CGroupTemplate::hasChild(void) const { return ((groupList.size() + childList.size()) > 0); } //--------------------------------------------------------------- template void CGroupTemplate::solveDescInheritance(bool apply, const CAttributeMap * const parent) { if (parent != NULL) SuperClassAttribute::setAttributes(parent, apply); typename std::vector::const_iterator itc = this->childList.begin(), endc = this->childList.end(); typename std::vector::const_iterator itg = this->groupList.begin(), endg = this->groupList.end(); for (; itc != endc; itc++) { U* child = *itc; child->solveDescInheritance(apply,this); } for (; itg != endg; itg++) { V* group = *itg; if (apply) group->solveRefInheritance(); group->solveDescInheritance(apply,this); } } //--------------------------------------------------------------- template void CGroupTemplate::getAllChildren(std::vector& allc) const { allc.insert (allc.end(), childList.begin(), childList.end()); typename std::vector::const_iterator itg = this->groupList.begin(), endg = this->groupList.end(); for (; itg != endg; itg++) { V* group = *itg; group->getAllChildren(allc); } } //--------------------------------------------------------------- template std::vector CGroupTemplate::getAllChildren(void) const { std::vector allc; this->getAllChildren(allc); return (allc); } //--------------------------------------------------------------- template void CGroupTemplate::solveRefInheritance(void) { /* Ne rien faire de plus */ } ///-------------------------------------------------------------- template U* CGroupTemplate::createChild(const string& id) { return CGroupFactory::CreateChild(this->getShared(), id).get() ; } template void CGroupTemplate::addChild(U* child) { return CGroupFactory::AddChild(this->getShared(),child->getShared()) ; } template V* CGroupTemplate::createChildGroup(const string& id) { return CGroupFactory::CreateGroup(this->getShared(), id).get() ; } template void CGroupTemplate::addChildGroup(V* childGroup) { return CGroupFactory::AddGroup(this->getShared(), childGroup->getShared()) ; } template void CGroupTemplate::sendCreateChild(const string& id, CContextClient* client, const string& objectId) { CEventClient event(this->getType(),EVENT_ID_CREATE_CHILD) ; if (client->isServerLeader()) { CMessage msg ; if (objectId.empty()) msg << this->getId(); else msg << objectId; msg<& ranks = client->getRanksServerLeader(); for (std::list::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank) event.push(*itRank,1,msg) ; client->sendEvent(event) ; } else client->sendEvent(event) ; } template void CGroupTemplate::sendCreateChildGroup(const string& id, CContextClient* client, const string& objectId) { CEventClient event(this->getType(),EVENT_ID_CREATE_CHILD_GROUP) ; if (client->isServerLeader()) { CMessage msg ; if (objectId.empty()) msg << this->getId(); else msg << objectId; msg<& ranks = client->getRanksServerLeader(); for (std::list::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank) event.push(*itRank,1,msg) ; client->sendEvent(event) ; } else client->sendEvent(event) ; } template void CGroupTemplate::recvCreateChild(CEventServer& event) { CBufferIn* buffer=event.subEvents.begin()->buffer; string id; *buffer>>id ; V::get(id)->recvCreateChild(*buffer) ; } template void CGroupTemplate::recvCreateChild(CBufferIn& buffer) { string id ; buffer>>id ; createChild(id) ; } template void CGroupTemplate::recvCreateChildGroup(CEventServer& event) { CBufferIn* buffer=event.subEvents.begin()->buffer; string id; *buffer>>id ; V::get(id)->recvCreateChildGroup(*buffer) ; } template void CGroupTemplate::recvCreateChildGroup(CBufferIn& buffer) { string id ; buffer>>id ; createChildGroup(id) ; } template bool CGroupTemplate::dispatchEvent(CEventServer& event) { if (CObjectTemplate::dispatchEvent(event)) return true ; else { switch(event.type) { case EVENT_ID_CREATE_CHILD : recvCreateChild(event) ; return true ; break ; case EVENT_ID_CREATE_CHILD_GROUP : recvCreateChildGroup(event) ; return true ; break ; default : return false ; } } } template void CGroupTemplate::parse(xml::CXMLNode & node) { this->parse(node, true); } template void CGroupTemplate::parse(xml::CXMLNode & node, bool withAttr, const std::set& parseContextList) { ERROR("void CGroupTemplate::parse(xml::CXMLNode & node, bool withAttr, const std::set& parseContextList)", <<"must not be called by this kind of object : "< void CGroupTemplate::parse(xml::CXMLNode & node, bool withAttr) { StdString name = node.getElementName(); xml::THashAttributes attributes = node.getAttributes(); if (withAttr) { CGroupTemplate::SuperClass::parse(node); if (attributes.end() != attributes.find("src")) xml::CXMLParser::ParseInclude(attributes["src"].c_str(), *this); } // PARSING POUR GESTION DES ENFANTS V* group_ptr = (this->hasId()) ? V::get(this->getId()) : xios_polymorphic_downcast(this); if (!(node.goToChildElement())) { if (this->hasId()) { DEBUG(<< "L'objet de type \'" << V::GetName() << "\' nommé \'" << this->getId() << "\' ne contient pas d\'enfant !"); } } else { do { // Parcours pour traitement. StdString name = node.getElementName(); attributes.clear(); attributes = node.getAttributes(); if (name.compare(V::GetName()) == 0) { if (attributes.end() == attributes.find("id")) CGroupFactory::CreateGroup(group_ptr->getShared())->parse(node); else CGroupFactory::CreateGroup(group_ptr->getShared(), attributes["id"])->parse(node); continue; } if (name.compare(U::GetName()) == 0) { if (attributes.end() == attributes.find("id")) CGroupFactory::CreateChild(group_ptr->getShared())->parse(node); else CGroupFactory::CreateChild(group_ptr->getShared(), attributes["id"])->parse(node); continue; } DEBUG(<< "Dans le contexte \'" << CContext::getCurrent()->getId() << "\', un objet de type \'" << V::GetName() << "\' ne peut contenir qu'un objet de type \'" << V::GetName() << "\' ou de type \'" << U::GetName() << "\' (reçu : " << name << ") !"); } while (node.goToNextElement()); node.goToParentElement(); // Retour au parent } } template void CGroupTemplate::parseChild(xml::CXMLNode & node) { // PARSING POUR GESTION DES ENFANTS V* group_ptr = (this->hasId()) ? V::get(this->getId()) : xios_polymorphic_downcast(this); StdString name = node.getElementName(); xml::THashAttributes attributes = node.getAttributes(); if (name.compare(V::GetName()) == 0) { if (attributes.end() == attributes.find("id")) CGroupFactory::CreateGroup(group_ptr->getShared())->parse(node); else CGroupFactory::CreateGroup(group_ptr->getShared(), attributes["id"])->parse(node); return ; } else if (name.compare(U::GetName()) == 0) { if (attributes.end() == attributes.find("id")) CGroupFactory::CreateChild(group_ptr->getShared())->parse(node); else CGroupFactory::CreateChild(group_ptr->getShared(), attributes["id"])->parse(node); return ; } DEBUG(<< "Dans le contexte \'" << CContext::getCurrent()->getId() << "\', un objet de type \'" << V::GetName() << "\' ne peut contenir qu'un objet de type \'" << V::GetName() << "\' ou de type \'" << U::GetName() << "\' (reçu : " << name << ") !"); } } // namespace xios #endif // __XIOS_CGroupTemplate_impl__