Zserio C++17 runtime library 1.3.0
Built for Zserio 2.20.0
Loading...
Searching...
No Matches
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 <string_view>
8#include <type_traits>
9#include <vector>
10
12
13namespace zserio
14{
15
19class CppRuntimeException : public std::exception
20{
21public:
27 explicit CppRuntimeException(const char* message = "");
28
34
37
44 const char* what() const noexcept override;
45
51 void append(const char* message);
52
59 void append(const char* message, size_t messageLen);
60
62 void appendImpl(std::string_view message);
63
64 std::array<char, 512> m_buffer; // note fixed sized array is deeply copied on copy operations and it's OK
65 size_t m_len = 0;
66};
67
77
86CppRuntimeException& operator<<(CppRuntimeException& exception, bool value);
87
96CppRuntimeException& operator<<(CppRuntimeException& exception, float value);
97
106CppRuntimeException& operator<<(CppRuntimeException& exception, double value);
107
117
126template <typename T, typename std::enable_if<std::is_integral<T>::value, int>::type = 0>
128{
129 std::array<char, 24> buffer = {};
130 const char* stringValue = convertIntToString(buffer, value);
131 return exception << stringValue;
132}
133
142template <typename ALLOC>
144 CppRuntimeException& exception, const std::basic_string<char, std::char_traits<char>, ALLOC>& value)
145{
146 exception.append(value.c_str(), value.size());
147 return exception;
148}
149
158template <typename T, typename ALLOC>
159CppRuntimeException& operator<<(CppRuntimeException& exception, const std::vector<T, ALLOC>& value)
160{
161 return exception << "vector([...], " << value.size() << ")";
162}
163
164namespace detail
165{
166
167// inspired by C++ ostreams - see https://cplusplus.github.io/LWG/issue1203
168// note that e.g. in gcc implementation of ostreams there are two constraints, but the second one:
169// typename = decltype(std::declval<EXCEPTION&>() << std::declval<const VALUE&>())
170// is probably unnecessary and since it caused a compilation error in MSVC 2017 Conformance Mode,
171// we intentionally skipped it (even though it was probably a compiler bug)
172template <typename EXCEPTION, typename VALUE,
173 typename = typename std::enable_if<std::is_base_of<CppRuntimeException, EXCEPTION>::value, int>::type>
174using CppRuntimeExceptionRValueInsertion = EXCEPTION&&;
175
176} // namespace detail
177
189template <typename CPP_RUNTIME_EXCEPTION, typename T>
190detail::CppRuntimeExceptionRValueInsertion<CPP_RUNTIME_EXCEPTION, T> operator<<(
191 CPP_RUNTIME_EXCEPTION&& exception, const T& value)
192{
193 exception << value;
194 return std::forward<CPP_RUNTIME_EXCEPTION>(exception);
195}
196
197} // namespace zserio
198
199#endif // ifndef ZSERIO_CPP_RUNTIME_EXCEPTION_H_INC
CppRuntimeException & operator=(CppRuntimeException &&other)=default
CppRuntimeException & operator=(const CppRuntimeException &other)=default
CppRuntimeException(CppRuntimeException &&other)=default
CppRuntimeException(const CppRuntimeException &other)=default
const char * what() const noexcept override
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:554