First commit
[anna.git] / include / anna / core / Exception.hpp
1 // ANNA - Anna is Not 'N' Anymore
2 //
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
4 //
5 // https://bitbucket.org/testillano/anna
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 //
11 //     * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 //     * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
16 // distribution.
17 //     * Neither the name of Google Inc. nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 //
33 // Authors: eduardo.ramos.testillano@gmail.com
34 //          cisco.tierra@gmail.com
35
36
37 #ifndef anna_core_Exception_hpp
38 #define anna_core_Exception_hpp
39
40 #include <string>
41 #include <exception>
42
43 namespace anna {
44
45 /**
46    Excepcion generica usada en las aplicaciones functions.
47 */
48 class Exception : public std::exception {
49 public:
50   /**
51    * Normaliza las acciones que puede tomar un método a la hora de configurar
52    * el tratamiento de errores/excepciones.
53    */
54   struct Mode { enum _v { Ignore, Throw, Trace }; };
55
56   /**
57      Constructor.
58      @param text Texto explicativo de la excepcion.
59      @param fromFile Fichero en el que se provoco la situacion de error.
60      @param fromLine Linea del fichero en la que se detecto el error.
61   */
62   Exception(const char* text, const char* fromFile, const int fromLine);
63
64   /**
65      Constructor copia.
66
67      @param other Instancia de la una excepcion a partir de la que vamos a obtener los datos.
68   */
69   Exception(const Exception& other);
70
71   /**
72      Destructor.
73   */
74   virtual ~Exception() throw() {;}
75
76   // Accesores
77   /**
78      @return Devuelve el texto explicativo asociado a esta excepcion.
79   */
80   const std::string& getText() const throw() { return m_text;}
81
82   /**
83      @return El nombre del fichero donde se genero la excepcion. Coincidira con el indicado
84      en el constructor.
85   */
86   const char* getFromFile() const { return m_fromFile.c_str(); }
87
88   /**
89      @return La linea del fichero donde se genero la excepcion. Coincidira con la indicada
90      en el constructor.
91   */
92   int getFromLine() const throw() { return m_fromLine; }
93
94   /**
95      Establecer un codigo de error asociado a esta excepcion.
96
97      @param errorCode Valor a establecer. El significado de este error dependera de la
98      interpretacion particular que queramos darle en nuestra aplicacion.
99   */
100   void setErrorCode(const int errorCode) throw() { m_errorCode = errorCode; }
101
102   /**
103      @return El codigo de error asociado a esta excepcion.
104   */
105   int getErrorCode() const throw() { return m_errorCode; }
106
107   // Operadores
108   /**
109      Operador copia.
110
111      @param right Instancia de la excepcion de la que vamos a obtener los datos.
112
113      @return Referencia a esta instancia.
114    */
115   Exception& operator = (const Exception& right) throw();
116
117   // Metodos
118   /**
119      Devuelve una cadena conteniendo toda la informacion referente a la excepcion
120      en un formato que sea facil de interpretar.
121
122      @return Instancia de la cadena conteniendo la informacion de la excepcion.
123   */
124   std::string asString() const throw();
125
126   /**
127      Devuelve una cadena conteniendo toda la informacion referente a la excepcion
128      en un formato que sea facil de interpretar.
129
130      Sobreescribe el metodo de la clase base.
131
132      @return Puntero a la cadena conteniendo la informacion de la excepcion.
133   */
134   const char* what() const throw() { return asString().c_str(); }     // JASC, 23/9/2003 antes dAata() ( no tenia el trailing null )
135
136   /**
137      Saca una traza de error en el fichero de log con el texto asociado a este excepcion.
138
139      @see asString
140      @see Logger#write
141   */
142   void trace() const throw();
143
144 protected:
145   Exception(const char* text, const char* name, const char* fromFile, const int fromLine);
146
147   /**
148      Establece el texto asociado a esta excepcion.
149
150      @param text Nuevo texto asociado a esta excepcion.
151   */
152   void setText(const char* text) throw() { m_text = text; }
153
154   /**
155      Establece el texto asociado a esta excepcion.
156
157      @param text Nuevo texto asociado a esta excepcion.
158   */
159   void setText(const std::string& text) throw() { m_text = text; }
160
161 private:
162   std::string m_text;
163   std::string m_name;
164   std::string m_fromFile;
165   int m_fromLine;
166   int m_errorCode;
167 };
168
169 } //namespace anna
170
171 #endif