Zserio C++ runtime library  1.1.0
Built for Zserio 2.15.0
CppRuntimeException.h
Go to the documentation of this file.
1 #ifndef ZSERIO_CPP_RUNTIME_EXCEPTION_H_INC
2 #define ZSERIO_CPP_RUNTIME_EXCEPTION_H_INC
3 
4 #include <array>
5 #include <exception>
6 #include <string>
7 #include <type_traits>
8 #include <vector>
9 
10 #include "zserio/Span.h"
12 #include "zserio/Traits.h"
13 
14 namespace zserio
15 {
16 
20 class CppRuntimeException : public std::exception
21 {
22 public:
28  explicit CppRuntimeException(const char* message = "");
29 
34  ~CppRuntimeException() override = default;
35 
36  CppRuntimeException(const CppRuntimeException& other) = default;
38 
45  const char* what() const noexcept override;
46 
52  void append(const char* message);
53 
60  void append(const char* message, size_t messageLen);
61 
62 private:
63  void appendImpl(Span<const char> message);
64 
65  std::array<char, 512> m_buffer; // note fixed sized array is deeply copied on copy operations and it's OK
66  size_t m_len = 0;
67 };
68 
77 CppRuntimeException& operator<<(CppRuntimeException& exception, const char* message);
78 
87 CppRuntimeException& operator<<(CppRuntimeException& exception, bool value);
88 
97 CppRuntimeException& operator<<(CppRuntimeException& exception, float value);
98 
107 CppRuntimeException& operator<<(CppRuntimeException& exception, double value);
108 
117 template <typename T, typename std::enable_if<std::is_integral<T>::value, int>::type = 0>
118 CppRuntimeException& operator<<(CppRuntimeException& exception, T value)
119 {
120  std::array<char, 24> buffer = {};
121  const char* stringValue = convertIntToString(buffer, value);
122  return exception << stringValue;
123 }
124 
133 template <typename T, typename std::enable_if<is_bitmask<T>::value, int>::type = 0>
135 {
136  exception << value.getValue();
137  return exception;
138 }
139 
148 template <typename ALLOC>
150  CppRuntimeException& exception, const std::basic_string<char, std::char_traits<char>, ALLOC>& value)
151 {
152  exception.append(value.c_str(), value.size());
153  return exception;
154 }
155 
164 template <typename T, typename ALLOC>
165 CppRuntimeException& operator<<(CppRuntimeException& exception, const std::vector<T, ALLOC>& value)
166 {
167  return exception << "vector([...], " << value.size() << ")";
168 }
169 
170 namespace detail
171 {
172 
173 // inspired by C++ ostreams - see https://cplusplus.github.io/LWG/issue1203
174 // note that e.g. in gcc implementation of ostreams there are two constraints, but the second one:
175 // typename = decltype(std::declval<EXCEPTION&>() << std::declval<const VALUE&>())
176 // is probably unnecessary and since it caused a compilation error in MSVC 2017 Conformance Mode,
177 // we intentionally skipped it (even though it was probably a compiler bug)
178 template <typename EXCEPTION, typename VALUE,
179  typename = typename std::enable_if<std::is_base_of<CppRuntimeException, EXCEPTION>::value, int>::type>
180 using CppRuntimeExceptionRValueInsertion = EXCEPTION&&;
181 
182 } // namespace detail
183 
195 template <typename CPP_RUNTIME_EXCEPTION, typename T>
196 detail::CppRuntimeExceptionRValueInsertion<CPP_RUNTIME_EXCEPTION, T> operator<<(
197  CPP_RUNTIME_EXCEPTION&& exception, const T& value)
198 {
199  exception << value;
200  return std::forward<CPP_RUNTIME_EXCEPTION>(exception);
201 }
202 
203 } // namespace zserio
204 
205 #endif // ifndef ZSERIO_CPP_RUNTIME_EXCEPTION_H_INC
CppRuntimeException & operator=(const CppRuntimeException &other)=default
CppRuntimeException(CppRuntimeException &&other)=default
CppRuntimeException(const CppRuntimeException &other)=default
const char * what() const noexcept override
CppRuntimeException & operator=(CppRuntimeException &&other)=default
~CppRuntimeException() override=default
CppRuntimeException(const char *message="")
void append(const char *message)
const char * convertIntToString(std::array< char, 24 > &buffer, T value)
CppRuntimeException & operator<<(CppRuntimeException &exception, const BasicBitBuffer< ALLOC > &bitBuffer)
Definition: BitBuffer.h:455