Remove dynamic exceptions
[anna.git] / include / anna / core / util / Configuration.hpp
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 #ifndef anna_core_util_Configuration_hpp
10 #define anna_core_util_Configuration_hpp
11
12 #include <stdlib.h>
13
14 #include <vector>
15 #include <map>
16 #include <string>
17
18 #include <anna/core/functions.hpp>
19 #include <anna/core/util/Variable.hpp>
20 #include <anna/core/RuntimeException.hpp>
21
22 namespace anna {
23
24 /**
25    Clase para recoger parametros de un determinado archivo de configuracion.
26 */
27 class Configuration {
28 public:
29   static const char* defaultSection;
30
31   /**
32      Constructor.
33   */
34   Configuration() {;}
35
36   /**
37   * Destructor.
38   */
39   ~Configuration() { removeAll(); }
40
41   /**
42      Carga en memoria el archivo indicado como argumento.
43
44      @param configFile Ruta completa con el nombre del archivo de configuracion a cargar.
45      Cualquier otro archivo procesado anteriormente con esta instancia se perdera.
46   */
47   void load(const char* configFile) noexcept(false);
48
49   /**
50     Establece el valor por defecto para una determinada variable, es decir, en caso de que
51     la variable no exista en el fichero de configuracion cargado (ver #load) devolvera
52     el valor establecido mediante este metodo.
53
54     @param sectionName Nombre de la seccion a la que pertenece la variable.
55     @param variableName Nombre de la variable de configuracion.
56     @param defaultValue Valor por defecto de la variable. Ă‰ste valor solo sera devuelto en caso
57     de que la variable indicada por la seccion y el nombre de variable no este contenido en
58     el archivo de configuracion cargado.
59   */
60   void setDefaultValue(const char* sectionName, const char* variableName, const char* defaultValue)
61   noexcept(false);
62
63   /**
64      Devuelve el valor asociada a la variable indicada.
65
66     @param sectionName Nombre de la seccion a la que pertenece la variable.
67     @param variableName Nombre de la variable de configuracion.
68     @param strict Si es true indica que debe devolver el valor estricto de la variable, de forma,
69     que si esta variable no esta contenida en el archivo de configuracion aun habiendo asociado
70     un valor por defecto (ver #setDefaultValue) devolvera NULL.
71
72     @return El valor asociado a la variable. Puede ser NULL.
73   */
74   const char* getValue(const char* sectionName, const char* variableName, const bool strict = false) const
75   noexcept(false);
76
77   /**
78      Devuelve el valor asociada a la variable indicada.
79     @param sectionName Nombre de la seccion a la que pertenece la variable.
80     @param variableName Nombre de la variable de configuracion.
81     @param strict Si es true indica que debe devolver el valor estricto de la variable, de forma,
82     que si esta variable no esta contenida en el archivo de configuracion aun habiendo asociado
83     un valor por defecto (ver #setDefaultValue) devolvera NULL.
84
85     @return El valor asociado a la variable.
86   */
87   int getIntegerValue(const char* sectionName, const char* variableName, const bool strict = false) const
88   noexcept(false);
89
90   /**
91      Devuelve el estado de existencia o no de la variable indicada.
92     @param sectionName Nombre de la seccion a la que pertenece la variable.
93     @param variableName Nombre de la variable de configuracion.
94
95     @return true si la variable existe, y false en otro caso. Solo deberia invocarse despues de
96     invocar al metodo #load.
97   */
98   bool exists(const char* sectionName, const char* variableName) const ;
99
100   /**
101      Devuelve la cadena por la que podemos buscar el componente.
102      \return La cadena por la que podemos buscar el componente.
103      \see Application::find
104   */
105   static const char* getClassName() { return "anna::Configuration"; }
106
107 private:
108   class VariableEx : public Variable {
109   public:
110     typedef std::vector <VariableEx*> Vector;
111
112     VariableEx(const char* variableName) :
113       Variable(variableName, Variable::Type::String),
114       a_defaultValue(NULL) {}
115
116     void setDefaultValue(const char* defaultValue) { a_defaultValue = defaultValue; }
117
118     const char* getDefaultValue() const { return a_defaultValue; }
119
120   private:
121     const char* a_defaultValue;
122   };
123
124   std::map <std::string, VariableEx::Vector*> a_sections;
125
126   Configuration(const Configuration& other);  // No implementado
127
128   void initialize() noexcept(false) {;}
129   void stop() {;}
130
131   void removeAll() ;
132   bool processSection(const int nline, char* buffer, std::string& currentSection);
133   void processVariable(const int nline, char* buffer, const std::string& currentSection) noexcept(false);
134   VariableEx* createVariable(const std::string& section, const char* variableName) ;
135   VariableEx* find(const std::string& section, const char* variableName) ;
136   const VariableEx* find(const std::string& section, const char* variableName) const
137   {
138     return const_cast <Configuration*>(this)->find(section, variableName);
139   }
140
141   static char* strip(char* buffer);
142 };
143
144 }
145
146 #endif
147