Changed LICENSE. Now referenced to web site and file on project root directory
[anna.git] / source / core / util / ComponentManager.cpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
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 //
7
8
9 #include <anna/config/defines.hpp>
10 #include <anna/core/util/ComponentManager.hpp>
11 #include <anna/core/util/Component.hpp>
12
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>
19
20
21 anna::Component* anna::ComponentManager::find(const char* className)
22 throw() {
23   std::map <std::string, Component*>::iterator it = a_components.find(className);
24
25   if(it != a_components.end()) return (*it).second;
26
27   return NULL;
28 }
29
30 void anna::ComponentManager::attach(anna::Component* component)
31 throw(anna::RuntimeException) {
32   LOGMETHOD(anna::TraceMethod tm("anna::ComponentManager", "attach(component)", ANNA_FILE_LOCATION));
33
34   if(component == NULL)
35     throw anna::RuntimeException("Cannot attach <null> component", ANNA_FILE_LOCATION);
36
37   const char* className = component->getClassName();
38   Component *already = find(className);
39
40   if(already) {
41     LOGINFORMATION(
42       std::string msg(already->asString());
43       msg += " | Was previously attached !";
44       anna::Logger::information(msg, ANNA_FILE_LOCATION);
45     )
46     return;
47   }
48
49   LOGDEBUG(
50     std::string msg("anna::ComponentManager::attach | ");
51     msg += component->asString();
52     anna::Logger::debug(msg, ANNA_FILE_LOCATION);
53   );
54   a_components[className] = component;
55 }
56
57 void anna::ComponentManager::detach(anna::Component* component)
58 throw(anna::RuntimeException) {
59   LOGMETHOD(anna::TraceMethod tm("anna::ComponentManager", "detach(component)", ANNA_FILE_LOCATION));
60
61   if(component == NULL)
62     throw anna::RuntimeException("Cannot detach <null> component", ANNA_FILE_LOCATION);
63
64   const char* className = component->getClassName();
65   std::map <std::string, Component*>::iterator it = a_components.find(className);
66
67   if(it == a_components.end()) {
68     LOGINFORMATION(
69       std::string msg(component->asString());
70       msg += " | Not found: cannot erase !";
71       anna::Logger::information(msg, ANNA_FILE_LOCATION);
72     )
73     return;
74   }
75
76   LOGDEBUG(
77     std::string msg("anna::ComponentManager::detach | ");
78     msg += component->asString();
79     anna::Logger::debug(msg, ANNA_FILE_LOCATION);
80   );
81   a_components.erase(it);
82 }
83
84 anna::xml::Node* anna::ComponentManager::asXML(anna::xml::Node* parent) const
85 throw() {
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());
91   Component *cc;
92
93   for(it = it_min; it != it_max; it++) {
94     anna::Guard guard(cc = (*it).second);
95     cc->asXML(node);
96   }
97
98   return parent;
99 }
100
101