Zserio C++17 runtime library  0.5.0
Built for Zserio 2.17.0
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 
15 const char* CppRuntimeException::what() const noexcept
16 {
17  return m_buffer.data();
18 }
19 
20 void CppRuntimeException::append(const char* message)
21 {
22  const size_t available = m_buffer.size() - 1 - m_len;
23  const size_t numCharsToAppend = strnlen(message, available);
24  appendImpl(std::string_view(message, numCharsToAppend));
25 }
26 
27 void CppRuntimeException::append(const char* message, size_t messageLen)
28 {
29  const size_t available = m_buffer.size() - 1 - m_len;
30  const size_t numCharsToAppend = std::min(messageLen, available);
31  appendImpl(std::string_view(message, numCharsToAppend));
32 }
33 
34 void CppRuntimeException::appendImpl(std::string_view message)
35 {
36  if (message.size() > 0)
37  {
38  (void)std::copy(message.begin(), message.end(), m_buffer.begin() + m_len);
39  m_len += message.size();
40  }
41  m_buffer.at(m_len) = '\0';
42 }
43 
44 CppRuntimeException& operator<<(CppRuntimeException& exception, const char* message)
45 {
46  exception.append(message);
47  return exception;
48 }
49 
51 {
52  return exception << (value ? "true" : "false");
53 }
54 
56 {
57  std::array<char, 24> integerPartBuffer = {};
58  std::array<char, 24> floatingPartBuffer = {};
59  const char* integerPartString = nullptr;
60  const char* floatingPartString = nullptr;
61  convertFloatToString(integerPartBuffer, floatingPartBuffer, value, integerPartString, floatingPartString);
62  CppRuntimeException& result = exception << integerPartString;
63  if (floatingPartString != nullptr)
64  {
65  result = result << "." << floatingPartString;
66  }
67 
68  return result;
69 }
70 
72 {
73  return exception << (static_cast<float>(value));
74 }
75 
76 CppRuntimeException& operator<<(CppRuntimeException& exception, std::string_view value)
77 {
78  exception.append(value.data(), value.size());
79  return exception;
80 }
81 
82 } // 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)