removed unused stuff in in_loop_invoke.hpp
[beacon-ss.git] / beacon / exceptions.hpp
blob789e7fb0d4264977712588704dbc8e6e637e2cbc
1 /**
2 * beacon
3 * Author: Lukas Krejci <krejci.l@centrum.cz>, (C) 2008
4 * Copyright: See COPYING file that comes with this distribution
5 */
7 #ifndef BEACON_EXCEPTIONS_H
8 #define BEACON_EXCEPTIONS_H
10 #include <exception>
11 #include <string>
12 #include <iostream>
13 #include <typeinfo>
14 #include <vector>
15 #include <algorithm>
16 #include <boost/bind.hpp>
18 namespace beacon {
20 using namespace std;
22 struct exception_cause {
23 exception_cause(char const * mes, char const * tn) : message(mes), type_name(tn) {}
25 char const * message;
26 char const * type_name;
27 //some more diag?
30 /**
31 * Base class for all exceptions thrown in beacon library.
33 class exception : public std::exception {
34 public:
36 explicit exception() throw() {}
37 explicit exception(string const & message) throw() : _message(message) {}
38 explicit exception(exception const & cause) throw() {
39 init_causes(cause);
42 explicit exception(string const & message, exception const & cause) throw() : _message(message) {
43 init_causes(cause);
46 ~exception() throw() {}
48 string const message() const throw() {
49 return _message;
52 char const * what() const throw() {
53 return _message.c_str();
56 vector<exception_cause> const causes() const throw () {
57 return vector<exception_cause>(_causes);
60 void print_stack_trace() {
61 print_stack_trace(std::cerr);
64 void print_stack_trace(std::ostream & os) {
65 os << typeid(*this).name() << " : " << what() << endl;
66 os << "Caused by:" << endl;
67 for(vector<exception_cause>::iterator i = _causes.begin(); i != _causes.end(); ++i) {
68 exception_cause & cause = *i;
69 os << cause.type_name << " : " << cause.message << endl;
72 private:
74 void init_causes(exception const & ex) {
75 type_info const & t = typeid(ex);
76 _causes.push_back(exception_cause(ex.message().c_str(), t.name()));
77 for_each(ex.causes().begin(), ex.causes().end(),
78 boost::bind(&vector<exception_cause>::push_back, _causes, _1));
81 string _message;
82 vector<exception_cause> _causes;
85 /**
86 * Thrown when some asynchronous operation times out.
88 class timeout_exception : public exception
90 public:
91 timeout_exception(std::string message, exception cause) : exception(message, cause) {}
92 timeout_exception(exception const& cause) : exception(cause) {}
93 timeout_exception(std::string message) : exception(message) {}
94 timeout_exception() {}
97 /**
98 * Thrown when some asynchronous operation fails.
100 class execution_exception : public exception
102 public:
103 execution_exception(std::string message, exception cause) : exception(message, cause) {}
104 execution_exception(exception const& cause) : exception(cause) {}
105 execution_exception(std::string message) : exception(message) {}
106 execution_exception() {}
109 } //namespace
111 #endif