GCC Code Coverage Report


Directory: ./
File: include/hpp/util/exception-factory.hh
Date: 2025-05-17 13:07:10
Exec Total Coverage
Lines: 8 8 100.0%
Functions: 20 20 100.0%
Branches: 2 4 50.0%

Line Branch Exec Source
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 {
42 /// \cond
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 20 static inline type run(ExceptionFactory<exception>& be, const In& t) {
54 20 be.ss << t;
55 20 return be;
56 }
57 };
58 } // namespace internal
59 /// \endcond
60
61 /// \brief Class to ease exception creation.
62 ///
63 /// You can use equivalently
64 /// \code
65 /// throw ::hpp::ExceptionFactory<std::runtime_error>() << "message" <<
66 /// variable << ::hpp::ThrowException();
67 /// \endcode
68 /// or
69 /// \code
70 /// HPP_THROW(std::runtime_error>, "message" << variable);
71 /// \endcode
72 template <typename exception>
73 struct HPP_UTIL_DLLAPI ExceptionFactory {
74 std::stringstream ss;
75
76 template <typename T>
77 inline typename internal::conditional_insertion_operator<exception, T>::type
78 26 operator<<(const T& t) {
79 20 return internal::conditional_insertion_operator<exception, T>::run(*this,
80 26 t);
81 }
82 };
83
84 /// \cond
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 3 static inline type run(ExceptionFactory<exception>& be,
94 const ThrowException&) {
95
2/4
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 3 times.
✗ Branch 6 not taken.
3 return exception(be.ss.str().c_str());
96 }
97 };
98 } // namespace internal
99 /// \endcond
100 } // end of namespace hpp.
101
102 /// \addtogroup hpp_util_exceptions
103 /// \{
104
105 /// \brief Throw an exception of type using MSG as a string stream
106 /// \code
107 /// HPP_THROW(std::runtime_error, "message" << variable);
108 /// \endcode
109 #define HPP_THROW(TYPE, MSG) \
110 throw ::hpp::ExceptionFactory<TYPE>() << MSG << ::hpp::ThrowException()
111
112 /// \brief Throw an exception of type using MSG as a string stream
113 /// \code
114 /// HPP_THROW_WITH_LINEINFO(std::runtime_error>, "message" << variable);
115 /// \endcode
116 #define HPP_THROW_WITH_LINEINFO(TYPE, MSG) \
117 HPP_THROW(TYPE, MSG << " at " << __FILE__ << ":" << __LINE__)
118
119 /// \}
120
121 #endif //! HPP_UTIL_EXCEPTION_FACTORY_HH
122