Zserio C++ runtime library  1.1.0
Built for Zserio 2.15.0
CppRuntimeException.cpp
Go to the documentation of this file.
1 #include <algorithm>
2 #include <array>
3 #include <cstring>
4 
6 
7 namespace zserio
8 {
9 
11  m_buffer()
12 {
13  append(message);
14 }
15 
16 const char* CppRuntimeException::what() const noexcept
17 {
18  return m_buffer.data();
19 }
20 
21 void CppRuntimeException::append(const char* message)
22 {
23  const size_t available = m_buffer.size() - 1 - m_len;
24  const size_t numCharsToAppend = strnlen(message, available);
25  appendImpl(Span<const char>(message, numCharsToAppend));
26 }
27 
28 void CppRuntimeException::append(const char* message, size_t messageLen)
29 {
30  const size_t available = m_buffer.size() - 1 - m_len;
31  const size_t numCharsToAppend = std::min(messageLen, available);
32  appendImpl(Span<const char>(message, numCharsToAppend));
33 }
34 
35 void CppRuntimeException::appendImpl(Span<const char> message)
36 {
37  if (message.size() > 0)
38  {
39  (void)std::copy(message.begin(), message.end(), m_buffer.begin() + m_len);
40  m_len += message.size();
41  }
42  m_buffer.at(m_len) = '\0';
43 }
44 
45 CppRuntimeException& operator<<(CppRuntimeException& exception, const char* message)
46 {
47  exception.append(message);
48  return exception;
49 }
50 
52 {
53  return exception << (value ? "true" : "false");
54 }
55 
57 {
58  std::array<char, 24> integerPartBuffer = {};
59  std::array<char, 24> floatingPartBuffer = {};
60  const char* integerPartString = nullptr;
61  const char* floatingPartString = nullptr;
62  convertFloatToString(integerPartBuffer, floatingPartBuffer, value, integerPartString, floatingPartString);
63  CppRuntimeException& result = exception << integerPartString;
64  if (floatingPartString != nullptr)
65  {
66  result = result << "." << floatingPartString;
67  }
68 
69  return result;
70 }
71 
73 {
74  return exception << (static_cast<float>(value));
75 }
76 
77 } // namespace zserio
const char * what() const noexcept override
CppRuntimeException(const char *message="")
void append(const char *message)
constexpr size_type size() const noexcept
Definition: Span.h:281
constexpr iterator end() const noexcept
Definition: Span.h:210
constexpr iterator begin() const noexcept
Definition: Span.h:200
CppRuntimeException & operator<<(CppRuntimeException &exception, const BasicBitBuffer< ALLOC > &bitBuffer)
Definition: BitBuffer.h:455
void convertFloatToString(std::array< char, 24 > &integerPartBuffer, std::array< char, 24 > &floatingPartBuffer, float value, const char *&integerPartString, const char *&floatingPartString)