vmmlib  1.7.0
 All Classes Namespaces Functions Pages
exception.hpp
1 /*
2  * Copyright (c) 2006-2014, Visualization and Multimedia Lab,
3  * University of Zurich <http://vmml.ifi.uzh.ch>,
4  * Eyescale Software GmbH,
5  * Blue Brain Project, EPFL
6  *
7  * This file is part of VMMLib <https://github.com/VMML/vmmlib/>
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  *
12  * Redistributions of source code must retain the above copyright notice, this
13  * list of conditions and the following disclaimer. Redistributions in binary
14  * form must reproduce the above copyright notice, this list of conditions and
15  * the following disclaimer in the documentation and/or other materials provided
16  * with the distribution. Neither the name of the Visualization and Multimedia
17  * Lab, University of Zurich nor the names of its contributors may be used to
18  * endorse or promote products derived from this software without specific prior
19  * written permission.
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 #ifndef __VMMLIB__EXCEPTION__HPP__
33 #define __VMMLIB__EXCEPTION__HPP__
34 
35 #include <iostream>
36 #include <stdexcept>
37 #include <string>
38 #include <sstream>
39 #include <cassert>
40 
41 #include <vmmlib/vmmlib_config.hpp>
42 
43 
44 #define VMMLIB_HERE ( except_here( __FILE__, __LINE__ ) )
45 
46 #ifdef VMMLIB_THROW_EXCEPTIONS
47 #define VMMLIB_ERROR( desc, here ) throw( exception( desc, here ) )
48 #else
49 #define VMMLIB_ERROR( desc, here ) error_noexcept( desc, here )
50 #endif
51 
52 
53 namespace vmml
54 {
55 
57 {
58  except_here( const char* file_, int line_ ) : file( file_ ), line( line_ ) {}
59  const char* file;
60  int line;
61 }; // struct except_here
62 
63 
64 inline void
65 error_noexcept( const std::string& desc, const except_here& here )
66 {
67  std::cerr
68  << "vmmlib error at " << here.file << ":" << here.line << "\n"
69  << desc
70  << std::endl;
71  assert( 0 );
72 }
73 
74 
75 
76 class exception : public std::exception
77 {
78 public:
79  exception( const std::string& desc, const except_here& here )
80  {
81  std::ostringstream os;
82  os << here.file << "(" << here.line << "): - " << desc << std::endl;
83  // cppcheck-suppress useInitializationList
84  _description = os.str();
85  }
86 
87  virtual ~exception() throw() {}
88 
89  virtual const char* what() const throw() { return _description.c_str(); }
90 
91 protected:
92  std::string _description;
93 
94 private:
95  // disallow std ctor
96  exception() {}
97  // disallow assignment operator
98  virtual const exception& operator=( const exception& ){ return *this; }
99 
100 
101 };
102 
103 
104 } // namespace stream_process
105 
106 #endif