CosmoBolognaLib
Free Software C++/Python libraries for cosmological calculations
Exception.h
Go to the documentation of this file.
1 /********************************************************************
2  * Copyright (C) 2010 by Federico Marulli *
3  * federico.marulli3@unibo.it *
4  * *
5  * This program is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU General Public License as *
7  * published by the Free Software Foundation; either version 2 of *
8  * the License, or (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public *
16  * License along with this program; if not, write to the Free *
17  * Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ********************************************************************/
20 
34 #ifndef __EXCEP__
35 #define __EXCEP__
36 
37 namespace cbl {
38 
39  namespace glob {
40 
45  enum class ExitCode {
46 
48  _error_,
49 
51  _IO_,
52 
55 
56  };
57 
64  inline std::vector<std::string> ExitCodeNames ()
65  { return {"error", "IO", "workInProgress"}; }
66 
73  inline ExitCode ExitCodeCast (const int exitCodeIndex)
74  { return castFromValue<ExitCode>(exitCodeIndex); }
75 
82  inline ExitCode ExitCodeCast (const std::string exitCodeName) {return castFromName<ExitCode>(exitCodeName, ExitCodeNames());}
83 
90  inline std::vector<ExitCode> ExitCodeCast (const std::vector<int> exitCodeIndeces)
91  { return castFromValues<ExitCode>(exitCodeIndeces); }
92 
99  inline std::vector<ExitCode> ExitCodeCast (const std::vector<std::string> exitCodeNames)
100  { return castFromNames<ExitCode>(exitCodeNames, ExitCodeNames()); }
101 
110  class Exception : public std::exception
111  {
112 
113  protected:
114 
116  std::string m_message;
117 
120 
122  std::string m_functionCBL;
123 
125  std::string m_fileCBL;
126 
127  public:
128 
133 
139  : m_message(par::col_red+"\n*** CosmoBolognaLib generic error! ***\n"+par::col_default), m_exitCode(ExitCode::_error_), m_functionCBL(cbl::par::defaultString), m_fileCBL(cbl::par::defaultString) {}
140 
156  explicit Exception (const std::string message, const ExitCode exitCode=ExitCode::_error_, const std::string header="\n", const std::string functionCBL=cbl::par::defaultString, const std::string fileCBL=cbl::par::defaultString)
158  {
159  m_message = header;
160 
161  switch(exitCode)
162  {
163  case(ExitCode::_error_):
165  ? par::col_red+"*** Error in the CBL function "+par::col_purple+m_functionCBL+par::col_red+" of "+m_fileCBL+" ***\n"
166  : par::col_red+"*** Error! ***\n";
167  break;
168 
169  case(ExitCode::_IO_):
171  ? par::col_red+"*** Input/Output error in the CBL function "+par::col_blue+m_functionCBL+" of "+m_fileCBL+par::col_red+" ***\n"
172  : par::col_red+"*** Input/Output error! ***\n";
173  break;
174 
177  ? par::col_purple+"*** Work in progress in the CBL function "+par::col_blue+m_functionCBL+" of "+m_fileCBL+par::col_red+" ***\n"
178  : par::col_purple+"*** Work in progress! ***\n";
179  break;
180  }
181 
182  m_message += par::col_yellow+message+"\n\n"+par::col_default;
183  }
184 
188  virtual ~Exception () noexcept = default;
189 
191 
192 
197 
203  const char* what () const noexcept override
204  { return m_message.c_str(); }
205 
210  ExitCode exitCode () const noexcept
211  { return m_exitCode; }
212 
218  std::string functionCBL () const noexcept
219  { return m_functionCBL; }
220 
227  std::string fileCBL () const noexcept
228  { return m_fileCBL; }
229 
231 
232  };
233 
234  }
235 }
236 
237 #endif
The class Exception.
Definition: Exception.h:111
Exception()
default constructor
Definition: Exception.h:138
Exception(const std::string message, const ExitCode exitCode=ExitCode::_error_, const std::string header="\n", const std::string functionCBL=cbl::par::defaultString, const std::string fileCBL=cbl::par::defaultString)
constructor
Definition: Exception.h:156
virtual ~Exception() noexcept=default
default destructor
ExitCode exitCode() const noexcept
get the exit status
Definition: Exception.h:210
std::string m_message
the message describing the exception
Definition: Exception.h:116
std::string fileCBL() const noexcept
get the CBL file containing the function where the exception is raised
Definition: Exception.h:227
std::string m_fileCBL
the CBL file containing the function where the exception is raised
Definition: Exception.h:125
std::string m_functionCBL
the CBL function where the exception is raised
Definition: Exception.h:122
ExitCode m_exitCode
the exit status
Definition: Exception.h:119
const char * what() const noexcept override
the error description
Definition: Exception.h:203
std::string functionCBL() const noexcept
get the CBL function where the exception is raised
Definition: Exception.h:218
static const std::string col_red
red colour (used when printing something on the screen)
Definition: Constants.h:300
static const std::string col_default
default colour (used when printing something on the screen)
Definition: Constants.h:297
static const std::string col_yellow
yellow colour (used when printing something on the screen)
Definition: Constants.h:315
static const std::string col_blue
blue colour (used when printing something on the screen)
Definition: Constants.h:312
static const std::string col_purple
purple colour (used when printing something on the screen)
Definition: Constants.h:318
static const std::string defaultString
default std::string value
Definition: Constants.h:336
std::vector< std::string > ExitCodeNames()
return a std::vector containing the ExitCode names
Definition: Exception.h:64
ExitCode
the exit status
Definition: Exception.h:45
@ _IO_
error related to the Input/Output
@ _error_
generic error
@ _workInProgress_
error due to work in progress
ExitCode ExitCodeCast(const int exitCodeIndex)
cast an enum of type ExitCode from its index
Definition: Exception.h:73
The global namespace of the CosmoBolognaLib
Definition: CAMB.h:38