hpp-util  4.9.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 // This file is part of the hpp-util.
5 //
6 // This software is provided "as is" without warranty of any kind,
7 // either expressed or implied, including but not limited to the
8 // implied warranties of fitness for a particular purpose.
9 //
10 // See the COPYING file for more information.
11 
12 #ifndef HPP_UTIL_EXCEPTION_FACTORY_HH
13 # define HPP_UTIL_EXCEPTION_FACTORY_HH
14 
15 # include <sstream>
16 
17 # include <hpp/util/config.hh>
18 
19 namespace hpp
20 {
22  struct ThrowException {};
23 
24  template <typename exception> struct ExceptionFactory;
25 
26  namespace internal {
27  template <typename exception, typename In>
28  struct conditional_insertion_operator {
29  typedef ExceptionFactory<exception>& type;
30 
31  static inline type run(ExceptionFactory<exception>& be, const In& t) { be.ss << t; return be; }
32  };
33  }
35 
46  template <typename exception>
47  struct HPP_UTIL_DLLAPI ExceptionFactory
48  {
49  std::stringstream ss;
50 
51  template <typename T> inline
52  typename internal::conditional_insertion_operator<exception, T>::type
53  operator<< (const T& t) {
54  return internal::conditional_insertion_operator<exception, T>::run (*this, t);
55  }
56  };
57 
59  // ----------------------------------------
60  // ExceptionFactory - template specialization
61  // ----------------------------------------
62  namespace internal {
63  template <typename exception>
64  struct conditional_insertion_operator<exception, ThrowException> {
65  typedef exception type;
66 
67  static inline type run(ExceptionFactory<exception>& be, const ThrowException&) { return exception(be.ss.str().c_str()); }
68  };
69  }
71 } // end of namespace hpp.
72 
75 
80 # define HPP_THROW(TYPE, MSG) \
81  throw ::hpp::ExceptionFactory<TYPE>() << MSG << ::hpp::ThrowException()
82 
87 # define HPP_THROW_WITH_LINEINFO(TYPE, MSG) \
88  HPP_THROW(TYPE,MSG << " at " << __FILE__ << ":" << __LINE__)
89 
91 
92 #endif
Definition: assertion.hh:24
Class to ease exception creation.
Definition: exception-factory.hh:47
HPP_UTIL_DLLAPI std::ostream & operator<<(std::ostream &o, const Exception &exception)
Override operator<< to handle exception display.
Definition: exception.cc:75
std::stringstream ss
Definition: exception-factory.hh:49