Zserio C++ runtime library  1.3.0
Built for Zserio 2.18.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 #include "zserio/Types.h"
14 
15 namespace zserio
16 {
17 
21 class CppRuntimeException : public std::exception
22 {
23 public:
29  explicit CppRuntimeException(const char* message = "");
30 
35  ~CppRuntimeException() override = default;
36 
37  CppRuntimeException(const CppRuntimeException& other) = default;
39 
46  const char* what() const noexcept override;
47 
53  void append(const char* message);
54 
61  void append(const char* message, size_t messageLen);
62 
63 private:
64  void appendImpl(Span<const char> message);
65 
66  std::array<char, 512> m_buffer; // note fixed sized array is deeply copied on copy operations and it's OK
67  size_t m_len = 0;
68 };
69 
78 CppRuntimeException& operator<<(CppRuntimeException& exception, const char* message);
79 
88 CppRuntimeException& operator<<(CppRuntimeException& exception, bool value);
89 
98 CppRuntimeException& operator<<(CppRuntimeException& exception, float value);
99 
108 CppRuntimeException& operator<<(CppRuntimeException& exception, double value);
109 
118 template <typename T, typename std::enable_if<std::is_integral<T>::value, int>::type = 0>
119 CppRuntimeException& operator<<(CppRuntimeException& exception, T value)
120 {
121  std::array<char, 24> buffer = {};
122  const char* stringValue = convertIntToString(buffer, value);
123  return exception << stringValue;
124 }
125 
134 template <typename T, typename std::enable_if<is_bitmask<T>::value, int>::type = 0>
136 {
137  exception << value.getValue();
138  return exception;
139 }
140 
149 template <typename ALLOC>
151  CppRuntimeException& exception, const std::basic_string<char, std::char_traits<char>, ALLOC>& value)
152 {
153  exception.append(value.c_str(), value.size());
154  return exception;
155 }
156 
165 template <typename T, typename ALLOC>
166 CppRuntimeException& operator<<(CppRuntimeException& exception, const std::vector<T, ALLOC>& value)
167 {
168  return exception << "vector([...], " << value.size() << ")";
169 }
170 
171 namespace detail
172 {
173 
174 // inspired by C++ ostreams - see https://cplusplus.github.io/LWG/issue1203
175 // note that e.g. in gcc implementation of ostreams there are two constraints, but the second one:
176 // typename = decltype(std::declval<EXCEPTION&>() << std::declval<const VALUE&>())
177 // is probably unnecessary and since it caused a compilation error in MSVC 2017 Conformance Mode,
178 // we intentionally skipped it (even though it was probably a compiler bug)
179 template <typename EXCEPTION, typename VALUE,
180  typename = typename std::enable_if<std::is_base_of<CppRuntimeException, EXCEPTION>::value, int>::type>
181 using CppRuntimeExceptionRValueInsertion = EXCEPTION&&;
182 
183 } // namespace detail
184 
196 template <typename CPP_RUNTIME_EXCEPTION, typename T>
197 detail::CppRuntimeExceptionRValueInsertion<CPP_RUNTIME_EXCEPTION, T> operator<<(
198  CPP_RUNTIME_EXCEPTION&& exception, const T& value)
199 {
200  exception << value;
201  return std::forward<CPP_RUNTIME_EXCEPTION>(exception);
202 }
203 
204 } // namespace zserio
205 
206 #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:454