1 // ANNA - Anna is Not Nothingness Anymore //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
9 #include <anna/config/defines.hpp>
10 #include <anna/core/util/ComponentManager.hpp>
11 #include <anna/core/util/Component.hpp>
13 #include <anna/core/RuntimeException.hpp>
14 #include <anna/core/functions.hpp>
15 #include <anna/xml/Node.hpp>
16 #include <anna/xml/Attribute.hpp>
17 #include <anna/core/tracing/Logger.hpp>
18 #include <anna/core/tracing/TraceMethod.hpp>
21 anna::Component* anna::ComponentManager::find(const char* className)
23 std::map <std::string, Component*>::iterator it = a_components.find(className);
25 if(it != a_components.end()) return (*it).second;
30 void anna::ComponentManager::attach(anna::Component* component)
31 throw(anna::RuntimeException) {
32 LOGMETHOD(anna::TraceMethod tm("anna::ComponentManager", "attach(component)", ANNA_FILE_LOCATION));
35 throw anna::RuntimeException("Cannot attach <null> component", ANNA_FILE_LOCATION);
37 const char* className = component->getClassName();
38 Component *already = find(className);
42 std::string msg(already->asString());
43 msg += " | Was previously attached !";
44 anna::Logger::information(msg, ANNA_FILE_LOCATION);
50 std::string msg("anna::ComponentManager::attach | ");
51 msg += component->asString();
52 anna::Logger::debug(msg, ANNA_FILE_LOCATION);
54 a_components[className] = component;
57 void anna::ComponentManager::detach(anna::Component* component)
58 throw(anna::RuntimeException) {
59 LOGMETHOD(anna::TraceMethod tm("anna::ComponentManager", "detach(component)", ANNA_FILE_LOCATION));
62 throw anna::RuntimeException("Cannot detach <null> component", ANNA_FILE_LOCATION);
64 const char* className = component->getClassName();
65 std::map <std::string, Component*>::iterator it = a_components.find(className);
67 if(it == a_components.end()) {
69 std::string msg(component->asString());
70 msg += " | Not found: cannot erase !";
71 anna::Logger::information(msg, ANNA_FILE_LOCATION);
77 std::string msg("anna::ComponentManager::detach | ");
78 msg += component->asString();
79 anna::Logger::debug(msg, ANNA_FILE_LOCATION);
81 a_components.erase(it);
84 anna::xml::Node* anna::ComponentManager::asXML(anna::xml::Node* parent) const
86 anna::xml::Node* node(NULL);
87 node = parent->createChild("anna.Components");
88 std::map <std::string, Component*>::const_iterator it;
89 std::map <std::string, Component*>::const_iterator it_min(a_components.begin());
90 std::map <std::string, Component*>::const_iterator it_max(a_components.end());
93 for(it = it_min; it != it_max; it++) {
94 anna::Guard guard(cc = (*it).second);