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