Re-integrate export-optimization branch.
[ardour.git] / libs / audiographer / audiographer / exception.h
1 #ifndef AUDIOGRAPHER_EXCEPTION_H
2 #define AUDIOGRAPHER_EXCEPTION_H
3
4 #include <exception>
5 #include <string>
6 #include <cxxabi.h>
7
8 #include <boost/format.hpp>
9
10 namespace AudioGrapher
11 {
12
13 class Exception : public std::exception
14 {
15   public:
16         template<typename T>
17         Exception (T const & thrower, std::string const & reason)
18           : reason (boost::str (boost::format (
19                         "Exception thrown by %1%: %2%") % name (thrower) % reason))
20         {}
21
22         virtual ~Exception () throw() { }
23
24         const char* what() const throw()
25         {
26                 return reason.c_str();
27         }
28         
29   protected:
30         template<typename T>
31         std::string name (T const & obj)
32         {
33 #ifdef __GNUC__
34                 int status;
35                 char * res = abi::__cxa_demangle (typeid(obj).name(), 0, 0, &status);
36                 if (status == 0) {
37                         std::string s(res);
38                         free (res);
39                         return s;
40                 }
41 #endif
42                 return typeid(obj).name();
43         }
44
45   private:
46         std::string const reason;
47
48 };
49
50 } // namespace AudioGrapher
51
52 #endif // AUDIOGRAPHER_EXCEPTION_H