hpp-util  5.0.0
Debugging tools for the HPP project.
exception-factory.hh
Go to the documentation of this file.
1 //
2 // Copyright (C) 2016 by Joseph Mirabel, CNRS.
3 //
4 
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // 1. Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //
12 // 2. Redistributions in binary form must reproduce the above copyright
13 // notice, this list of conditions and the following disclaimer in the
14 // documentation and/or other materials provided with the distribution.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
27 // DAMAGE.
28 //
29 // This software is provided "as is" without warranty of any kind,
30 // either expressed or implied, including but not limited to the
31 // implied warranties of fitness for a particular purpose.
32 //
33 // See the COPYING file for more information.
34 
35 #ifndef HPP_UTIL_EXCEPTION_FACTORY_HH
36 #define HPP_UTIL_EXCEPTION_FACTORY_HH
37 
38 #include <hpp/util/config.hh>
39 #include <sstream>
40 
41 namespace hpp {
43 struct ThrowException {};
44 
45 template <typename exception>
46 struct ExceptionFactory;
47 
48 namespace internal {
49 template <typename exception, typename In>
50 struct conditional_insertion_operator {
51  typedef ExceptionFactory<exception>& type;
52 
53  static inline type run(ExceptionFactory<exception>& be, const In& t) {
54  be.ss << t;
55  return be;
56  }
57 };
58 } // namespace internal
60 
72 template <typename exception>
74  std::stringstream ss;
75 
76  template <typename T>
77  inline typename internal::conditional_insertion_operator<exception, T>::type
78  operator<<(const T& t) {
79  return internal::conditional_insertion_operator<exception, T>::run(*this,
80  t);
81  }
82 };
83 
85 // ----------------------------------------
86 // ExceptionFactory - template specialization
87 // ----------------------------------------
88 namespace internal {
89 template <typename exception>
90 struct conditional_insertion_operator<exception, ThrowException> {
91  typedef exception type;
92 
93  static inline type run(ExceptionFactory<exception>& be,
94  const ThrowException&) {
95  return exception(be.ss.str().c_str());
96  }
97 };
98 } // namespace internal
100 } // end of namespace hpp.
101 
104 
109 #define HPP_THROW(TYPE, MSG) \
110  throw ::hpp::ExceptionFactory<TYPE>() << MSG << ::hpp::ThrowException()
111 
116 #define HPP_THROW_WITH_LINEINFO(TYPE, MSG) \
117  HPP_THROW(TYPE, MSG << " at " << __FILE__ << ":" << __LINE__)
118 
120 
121 #endif
Definition: assertion.hh:45
Class to ease exception creation.
Definition: exception-factory.hh:73
#define HPP_UTIL_DLLAPI
Definition: config.hh:88
HPP_UTIL_DLLAPI std::ostream & operator<<(std::ostream &o, const Exception &exception)
Override operator<< to handle exception display.
Definition: exception.cc:79
std::stringstream ss
Definition: exception-factory.hh:74