Remove warnings
[anna.git] / source / core / util / TextManager.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/core/tracing/TraceMethod.hpp>
10 #include <anna/core/tracing/Logger.hpp>
11 #include <anna/core/mt/Guard.hpp>
12
13 #include <anna/core/util/TextManager.hpp>
14 #include <anna/core/util/TextComposer.hpp>
15
16
17 using namespace std;
18 using namespace anna;
19
20 TextManager::TextManager(const char* name) :
21   a_name(name) {
22 }
23
24 void TextManager::clear()
25 throw() {
26   Guard guard(*this, "TextManager::clear");
27
28   for(TextComposerVector::iterator ii = a_composers.begin(), maxii = a_composers.end(); ii != maxii; ii ++)
29     delete *ii;
30
31   a_composers.clear();
32 }
33
34 void TextManager::create(const int composer, const char* expression)
35 throw(RuntimeException) {
36   TextComposer* result = NULL;
37   Guard guard(*this, "TextManager::create");
38
39   if((result = xfind(composer)) != NULL) {
40     string msg("Id: ");
41     msg += functions::asString(composer);
42     msg += " | Id already used in ";
43     msg += result->asString();
44     throw RuntimeException(msg, ANNA_FILE_LOCATION);
45   }
46
47   try {
48     if((result = createTextComposer(composer, expression)) == NULL) {
49       string msg(a_name);
50       msg += "::createTextComposer must not return NULL";
51       throw RuntimeException(msg, ANNA_FILE_LOCATION);
52     }
53
54     LOGDEBUG(
55       string msg("Compositor created | ");
56       msg += result->asString();
57       Logger::write(Logger::Debug, msg, ANNA_FILE_LOCATION);
58     );
59     result->initialize();
60     a_composers.push_back(result);
61   } catch(RuntimeException&) {
62     delete result;
63     throw;
64   }
65 }
66
67 TextComposer&  TextManager::find(const int composer)
68 throw(RuntimeException) {
69   TextComposer* result;
70   Guard guard(*this, "TextManager::find");
71
72   if((result = xfind(composer)) == NULL) {
73     string msg("Id: ");
74     msg += functions::asString(composer);
75     msg += " | Was not created";
76     throw RuntimeException(msg, ANNA_FILE_LOCATION);
77   }
78
79   return *result;
80 }
81
82 const TextComposer&  TextManager::find(const int composer) const
83 throw(RuntimeException) {
84   const TextComposer& result = const_cast <TextManager*>(this)->find(composer);
85   return result;
86 }
87
88 TextComposer* TextManager::createTextComposer(const int composer, const char* expression)
89 throw() {
90   return new TextComposer(composer, expression);
91 }
92
93 TextComposer* TextManager::xfind(const int composer)
94 throw() {
95   for(TextComposerVector::iterator ii = a_composers.begin(), maxii = a_composers.end(); ii != maxii; ii ++)
96     if((*ii)->getId() == composer) {
97       return (*ii);
98     }
99
100   return NULL;
101 }