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 std::string msg(already->getClassName());
44 msg += " | Was previously attached !";
45 anna::Logger::information(msg, ANNA_FILE_LOCATION);
51 std::string msg("anna::ComponentManager::attach | ");
52 msg += component->asString();
53 anna::Logger::debug(msg, ANNA_FILE_LOCATION);
55 a_components[className] = component;
58 void anna::ComponentManager::detach(anna::Component* component)
59 throw(anna::RuntimeException) {
60 LOGMETHOD(anna::TraceMethod tm("anna::ComponentManager", "detach(component)", ANNA_FILE_LOCATION));
63 throw anna::RuntimeException("Cannot detach <null> component", ANNA_FILE_LOCATION);
65 const char* className = component->getClassName();
66 std::map <std::string, Component*>::iterator it = a_components.find(className);
68 if(it == a_components.end()) {
70 std::string msg(component->asString());
71 msg += " | Not found: cannot erase !";
72 anna::Logger::information(msg, ANNA_FILE_LOCATION);
78 std::string msg("anna::ComponentManager::detach | ");
79 msg += component->asString();
80 anna::Logger::debug(msg, ANNA_FILE_LOCATION);
82 a_components.erase(it);
85 anna::xml::Node* anna::ComponentManager::asXML(anna::xml::Node* parent) const
87 anna::xml::Node* node(NULL);
88 node = parent->createChild("anna.Components");
89 std::map <std::string, Component*>::const_iterator it;
90 std::map <std::string, Component*>::const_iterator it_min(a_components.begin());
91 std::map <std::string, Component*>::const_iterator it_max(a_components.end());
94 for(it = it_min; it != it_max; it++) {
95 anna::Guard guard(cc = (*it).second);