Remove dynamic exceptions
[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 {
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 noexcept(false) {
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       std::string msg(already->getClassName());
44       msg += " | Was previously attached !";
45       anna::Logger::information(msg, ANNA_FILE_LOCATION);
46     )
47     return;
48   }
49
50   LOGDEBUG(
51     std::string msg("anna::ComponentManager::attach | ");
52     msg += component->asString();
53     anna::Logger::debug(msg, ANNA_FILE_LOCATION);
54   );
55   a_components[className] = component;
56 }
57
58 void anna::ComponentManager::detach(anna::Component* component)
59 noexcept(false) {
60   LOGMETHOD(anna::TraceMethod tm("anna::ComponentManager", "detach(component)", ANNA_FILE_LOCATION));
61
62   if(component == NULL)
63     throw anna::RuntimeException("Cannot detach <null> component", ANNA_FILE_LOCATION);
64
65   const char* className = component->getClassName();
66   std::map <std::string, Component*>::iterator it = a_components.find(className);
67
68   if(it == a_components.end()) {
69     LOGINFORMATION(
70       std::string msg(component->asString());
71       msg += " | Not found: cannot erase !";
72       anna::Logger::information(msg, ANNA_FILE_LOCATION);
73     )
74     return;
75   }
76
77   LOGDEBUG(
78     std::string msg("anna::ComponentManager::detach | ");
79     msg += component->asString();
80     anna::Logger::debug(msg, ANNA_FILE_LOCATION);
81   );
82   a_components.erase(it);
83 }
84
85 anna::xml::Node* anna::ComponentManager::asXML(anna::xml::Node* parent) const
86 {
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());
92   Component *cc;
93
94   for(it = it_min; it != it_max; it++) {
95     anna::Guard guard(cc = (*it).second);
96     cc->asXML(node);
97   }
98
99   return parent;
100 }
101
102