summaryrefslogtreecommitdiff
path: root/libs/audiographer/audiographer/exception.h
blob: a179b30f91c95349ef2e5d4ec15835a5f21ab38d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#ifndef AUDIOGRAPHER_EXCEPTION_H
#define AUDIOGRAPHER_EXCEPTION_H

#include <exception>
#include <string>
#include <cxxabi.h>

#include <boost/format.hpp>

namespace AudioGrapher
{

class Exception : public std::exception
{
  public:
	template<typename T>
	Exception (T const & thrower, std::string const & reason)
	  : reason (boost::str (boost::format (
			"Exception thrown by %1%: %2%") % name (thrower) % reason))
	{}

	virtual ~Exception () throw() { }

	const char* what() const throw()
	{
		return reason.c_str();
	}
	
  protected:
	template<typename T>
	std::string name (T const & obj)
	{
#ifdef __GNUC__
		int status;
		char * res = abi::__cxa_demangle (typeid(obj).name(), 0, 0, &status);
		if (status == 0) {
			std::string s(res);
			free (res);
			return s;
		}
#endif
		return typeid(obj).name();
	}

  private:
	std::string const reason;

};

} // namespace AudioGrapher

#endif // AUDIOGRAPHER_EXCEPTION_H