1 #ifndef __VMMLIB__EXCEPTION__HPP__
2 #define __VMMLIB__EXCEPTION__HPP__
3 
4 #include <iostream>
5 #include <stdexcept>
6 #include <string>
7 #include <sstream>
8 #include <cassert>
9 
10 #include <vmmlib/vmmlib_config.hpp>
11 
12 
13 #define VMMLIB_HERE ( except_here( __FILE__, __LINE__ ) )
14 
15 #ifdef VMMLIB_THROW_EXCEPTIONS
16 #define VMMLIB_ERROR( desc, here ) throw( exception( desc, here ) )
17 #else
18 #define VMMLIB_ERROR( desc, here ) error_noexcept( desc, here )
19 #endif
20 
21 
22 namespace vmml
23 {
24 
25 struct except_here
26 {
except_herevmml::except_here27     except_here( const char* file_, int line_ ) : file( file_ ), line( line_ ) {}
28     const char*     file;
29     int             line;
30 }; // struct except_here
31 
32 
33 inline void
error_noexcept(const std::string & desc,const except_here & here)34 error_noexcept( const std::string& desc, const except_here& here )
35 {
36     std::cerr
37         << "vmmlib error at " << here.file << ":" << here.line << "\n"
38         << desc
39         << std::endl;
40     assert( 0 );
41 }
42 
43 
44 
45 class exception : public std::exception
46 {
47 public:
exception(const std::string & desc,const except_here & here)48     exception( const std::string& desc, const except_here& here )
49     : _description( desc )
50     , _here( here )
51     {}
52 
~exception()53     virtual ~exception() throw() {}
54 
what() const55     virtual const char* what() const throw()
56     {
57         std::stringstream ss;
58         ss
59             << _here.file << "(" << _here.line << "): - "
60             << _description
61             << std::endl;
62         return ss.str().c_str();
63     }
64 
65 protected:
66     std::string         _description;
67     const except_here&  _here;
68 
69 private:
70     // disallow std ctor
exception()71     exception() : _here( *new except_here( "", 0 ) ){};
72     // disallow assignment operator
operator =(const exception &)73     virtual const exception& operator=( const exception& ){ return *this; }
74 
75 
76 };
77 
78 
79 } // namespace stream_process
80 
81 #endif
82