PaGMO  1.1.5
p_exceptions.h
1 /***************************************************************************
2  * Copyright (C) 2009 by Francesco Biscani *
3  * bluescarni@gmail.com *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (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 License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
20 
21 #ifndef P_EXCEPTIONS_H
22 #define P_EXCEPTIONS_H
23 
24 #include <exception>
25 #include <iostream>
26 #include <string>
27 
28 #define _P_EXCEPTION_QUOTEME(x) #x
29 #define P_EXCEPTION_QUOTEME(x) _P_EXCEPTION_QUOTEME(x)
30 #define P_EXCEPTION_EXCTOR(s) ((std::string(__FILE__ "," P_EXCEPTION_QUOTEME(__LINE__) ": ") + s) + ".")
31 #define P_EX_THROW(ex,s) (throw ex(P_EXCEPTION_EXCTOR(s)))
32 
33 class p_base_exception: public std::exception {
34  public:
35  p_base_exception(const std::string &s):m_what(s) {}
36  virtual const char *what() const throw() {
37  return m_what.c_str();
38  }
39  virtual ~p_base_exception() throw() {}
40  protected:
41  std::string m_what;
42 };
43 
44 struct index_error: public p_base_exception {
45  index_error(const std::string &s): p_base_exception(s) {}
46 };
47 
48 struct value_error: public p_base_exception {
49  value_error(const std::string &s): p_base_exception(s) {}
50 };
51 
52 struct io_error: public p_base_exception {
53  io_error(const std::string &s): p_base_exception(s) {}
54 };
55 
56 struct type_error: public p_base_exception {
57  type_error(const std::string &s): p_base_exception(s) {}
58 };
59 
60 struct assertion_error: public p_base_exception {
61  assertion_error(const std::string &s): p_base_exception(s) {}
62 };
63 
64 struct zero_division_error: public p_base_exception {
65  zero_division_error(const std::string &s): p_base_exception(s) {}
66 };
67 
68 struct not_implemented_error: public p_base_exception {
69  not_implemented_error(const std::string &s): p_base_exception(s) {}
70 };
71 
72 struct memory_error: public p_base_exception {
73  memory_error(const std::string &s): p_base_exception(s) {}
74 };
75 
76 #define P_EX_ASSERT(expr) \
77 if (!(expr)) { \
78  P_EX_THROW(assertion_error,"assertion error"); \
79 }
80 
81 #endif