Kernel Quantum Probability Library
The KQP library aims at providing tools for working with quantums probabilities
exceptions.hpp
1 /*
2  This file is part of the Kernel Quantum Probability library (KQP).
3 
4  KQP is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  KQP is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with KQP. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef __KQP_EXCEPTIONS_H__
19 #define __KQP_EXCEPTIONS_H__
20 
21 #include <boost/throw_exception.hpp>
22 #include <boost/exception/errinfo_at_line.hpp>
23 #include <boost/exception/info.hpp>
24 #include <boost/exception/exception.hpp>
25 #include <boost/format.hpp>
26 
27 // Stack information
28 #include <execinfo.h>
29 #include <dlfcn.h>
30 
31 namespace kqp {
33  typedef boost::error_info<struct errinfo_message_,std::string> errinfo_message;
34 
36  template <typename T> struct trace_info_struct_ {
37  int count;
38  T **pointers;
39  T *_pointers[100];
41  pointers = _pointers + 1;
42  count = backtrace( pointers, 100 ) - 1;
43  }
44  };
45 
47  typedef boost::error_info<struct tag_stack, trace_info_struct> stack_info;
48  inline stack_info trace_info() { return stack_info(trace_info_struct()); }
49 
50  template<typename T>
51  inline std::ostream & operator<<( std::ostream & x, const trace_info_struct_<T>& trace ) {
52  char **stack_syms(backtrace_symbols( trace.pointers, trace.count ));
53  Dl_info info;
54  x << "[" << trace.count << "]\n";
55  for ( int i = 0 ; i < trace.count ; ++i )
56  {
57  dladdr(trace.pointers[i], &info);
58  x << stack_syms[i]
59  << "\t" << (boost::format("%x") % info.dli_fbase)
60  << (boost::format(" start+%x ") % ((char*)trace.pointers[i]-(char*)info.dli_fbase))
61  << info.dli_sname << "\n";
62  }
63  std::free( stack_syms );
64  return x;
65  }
66 
68  class exception : public virtual std::exception, public virtual boost::exception {};
69 
71  class illegal_argument_exception : public virtual exception {};
72 
74  class arithmetic_exception : public virtual exception {};
75 
77  class out_of_bound_exception : public virtual exception {};
78 
80  class illegal_operation_exception : public virtual exception {};
81 
83  class not_implemented_exception : public virtual exception {};
84 
86  class assertion_exception : public virtual exception {};
87 
89  class bad_cast_exception : public virtual exception {};
90 
91  #undef BOOST_THROW_EXCEPTION
92  #define BOOST_THROW_EXCEPTION(x) kqp::throw_exception_(x,BOOST_CURRENT_FUNCTION,__FILE__,__LINE__)
93 
94  // Throw an exception
95  template <class E>
96  BOOST_ATTRIBUTE_NORETURN
97  void
98  throw_exception_( E const & x, char const * current_function, char const * file, int line )
99  {
100  using namespace boost;
101  using namespace boost::exception_detail;
102  throw_exception(
103  set_info(
104  set_info(
105  set_info(
106  set_info(
107  enable_error_info(x),
108  kqp::trace_info()),
109  throw_function(current_function)),
110  throw_file(file)),
111  throw_line(line)));
112  }
113 
114 }
115 
116 #endif