src/zserio/CppRuntimeException.cpp
Line | Count | Source |
1 | | #include <cstring> |
2 | | #include <array> |
3 | | #include <algorithm> |
4 | | |
5 | | #include "zserio/CppRuntimeException.h" |
6 | | |
7 | | namespace zserio |
8 | | { |
9 | | |
10 | | CppRuntimeException::CppRuntimeException(const char* message) : |
11 | | m_buffer() |
12 | 17.0k | { |
13 | 17.0k | append(message); |
14 | 17.0k | } |
15 | | |
16 | | const char* CppRuntimeException::what() const noexcept |
17 | 193 | { |
18 | 193 | return m_buffer.data(); |
19 | 193 | } |
20 | | |
21 | | void CppRuntimeException::append(const char* message) |
22 | 36.8k | { |
23 | 36.8k | const size_t available = m_buffer.size() - 1 - m_len; |
24 | 36.8k | const size_t numCharsToAppend = strnlen(message, available); |
25 | 36.8k | appendImpl(Span<const char>(message, numCharsToAppend)); |
26 | 36.8k | } |
27 | | |
28 | | void CppRuntimeException::append(const char* message, size_t messageLen) |
29 | 15.5k | { |
30 | 15.5k | const size_t available = m_buffer.size() - 1 - m_len; |
31 | 15.5k | const size_t numCharsToAppend = std::min(messageLen, available); |
32 | 15.5k | appendImpl(Span<const char>(message, numCharsToAppend)); |
33 | 15.5k | } |
34 | | |
35 | | void CppRuntimeException::appendImpl(Span<const char> message) |
36 | 52.3k | { |
37 | 52.3k | if (message.size() > 0) |
38 | 52.1k | { |
39 | 52.1k | std::copy(message.begin(), message.end(), m_buffer.begin() + m_len); |
40 | 52.1k | m_len += message.size(); |
41 | 52.1k | } |
42 | 52.3k | m_buffer.at(m_len) = 0; |
43 | 52.3k | } |
44 | | |
45 | | CppRuntimeException& operator<<(CppRuntimeException& exception, const char* message) |
46 | 19.7k | { |
47 | 19.7k | exception.append(message); |
48 | 19.7k | return exception; |
49 | 19.7k | } |
50 | | |
51 | | CppRuntimeException& operator<<(CppRuntimeException& exception, bool value) |
52 | 12 | { |
53 | 12 | return exception << (value ? "true"11 : "false"1 ); |
54 | 12 | } |
55 | | |
56 | | CppRuntimeException& operator<<(CppRuntimeException& exception, float value) |
57 | 14 | { |
58 | 14 | std::array<char, 24> integerPartBuffer = {}; |
59 | 14 | std::array<char, 24> floatingPartBuffer = {}; |
60 | 14 | const char* integerPartString = nullptr; |
61 | 14 | const char* floatingPartString = nullptr; |
62 | 14 | convertFloatToString(integerPartBuffer, floatingPartBuffer, value, integerPartString, floatingPartString); |
63 | 14 | CppRuntimeException& result = exception << integerPartString; |
64 | 14 | if (floatingPartString != nullptr) |
65 | 13 | result = result << "." << floatingPartString; |
66 | | |
67 | 14 | return result; |
68 | 14 | } |
69 | | |
70 | | CppRuntimeException& operator<<(CppRuntimeException& exception, double value) |
71 | 2 | { |
72 | 2 | return exception << (static_cast<float>(value)); |
73 | 2 | } |
74 | | |
75 | | } // namespace zserio |