Zserio C++17 runtime library  0.5.0
Built for Zserio 2.17.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 <string_view>
8 #include <type_traits>
9 #include <vector>
10 
12 
13 namespace zserio
14 {
15 
19 class CppRuntimeException : public std::exception
20 {
21 public:
27  explicit CppRuntimeException(const char* message = "");
28 
33  ~CppRuntimeException() override = default;
34 
35  CppRuntimeException(const CppRuntimeException& other) = default;
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 
61 private:
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 
76 CppRuntimeException& operator<<(CppRuntimeException& exception, const char* message);
77 
86 CppRuntimeException& operator<<(CppRuntimeException& exception, bool value);
87 
96 CppRuntimeException& operator<<(CppRuntimeException& exception, float value);
97 
106 CppRuntimeException& operator<<(CppRuntimeException& exception, double value);
107 
116 CppRuntimeException& operator<<(CppRuntimeException& exception, std::string_view value);
117 
126 template <typename T, typename std::enable_if<std::is_integral<T>::value, int>::type = 0>
127 CppRuntimeException& operator<<(CppRuntimeException& exception, T value)
128 {
129  std::array<char, 24> buffer = {};
130  const char* stringValue = convertIntToString(buffer, value);
131  return exception << stringValue;
132 }
133 
142 template <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 
158 template <typename T, typename ALLOC>
159 CppRuntimeException& operator<<(CppRuntimeException& exception, const std::vector<T, ALLOC>& value)
160 {
161  return exception << "vector([...], " << value.size() << ")";
162 }
163 
164 namespace 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)
172 template <typename EXCEPTION, typename VALUE,
173  typename = typename std::enable_if<std::is_base_of<CppRuntimeException, EXCEPTION>::value, int>::type>
174 using CppRuntimeExceptionRValueInsertion = EXCEPTION&&;
175 
176 } // namespace detail
177 
189 template <typename CPP_RUNTIME_EXCEPTION, typename T>
190 detail::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=(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)
Definition: BitBuffer.h:602
const char * convertIntToString(std::array< char, 24 > &buffer, T value)
CppRuntimeException & operator<<(CppRuntimeException &exception, const BasicBitBuffer< ALLOC > &bitBuffer)
Definition: BitBuffer.h:553