1 |
|
|
#include <cstring> |
2 |
|
|
#include <array> |
3 |
|
|
#include <algorithm> |
4 |
|
|
|
5 |
|
|
#include "zserio/CppRuntimeException.h" |
6 |
|
|
|
7 |
|
|
namespace zserio |
8 |
|
|
{ |
9 |
|
|
|
10 |
|
17083 |
CppRuntimeException::CppRuntimeException(const char* message) : |
11 |
|
17083 |
m_buffer() |
12 |
|
|
{ |
13 |
✓✗ |
17083 |
append(message); |
14 |
|
17083 |
} |
15 |
|
|
|
16 |
|
193 |
const char* CppRuntimeException::what() const noexcept |
17 |
|
|
{ |
18 |
|
193 |
return m_buffer.data(); |
19 |
|
|
} |
20 |
|
|
|
21 |
|
36855 |
void CppRuntimeException::append(const char* message) |
22 |
|
|
{ |
23 |
|
36855 |
const size_t available = m_buffer.size() - 1 - m_len; |
24 |
|
36855 |
const size_t numCharsToAppend = strnlen(message, available); |
25 |
✓✗ |
36855 |
appendImpl(Span<const char>(message, numCharsToAppend)); |
26 |
|
36855 |
} |
27 |
|
|
|
28 |
|
15529 |
void CppRuntimeException::append(const char* message, size_t messageLen) |
29 |
|
|
{ |
30 |
|
15529 |
const size_t available = m_buffer.size() - 1 - m_len; |
31 |
|
15529 |
const size_t numCharsToAppend = std::min(messageLen, available); |
32 |
✓✗✓✗
|
15529 |
appendImpl(Span<const char>(message, numCharsToAppend)); |
33 |
|
15529 |
} |
34 |
|
|
|
35 |
|
52384 |
void CppRuntimeException::appendImpl(Span<const char> message) |
36 |
|
|
{ |
37 |
✓✓ |
52384 |
if (message.size() > 0) |
38 |
|
|
{ |
39 |
|
52132 |
std::copy(message.begin(), message.end(), m_buffer.begin() + m_len); |
40 |
|
52132 |
m_len += message.size(); |
41 |
|
|
} |
42 |
|
52384 |
m_buffer.at(m_len) = 0; |
43 |
|
52384 |
} |
44 |
|
|
|
45 |
|
19772 |
CppRuntimeException& operator<<(CppRuntimeException& exception, const char* message) |
46 |
|
|
{ |
47 |
|
19772 |
exception.append(message); |
48 |
|
19772 |
return exception; |
49 |
|
|
} |
50 |
|
|
|
51 |
|
12 |
CppRuntimeException& operator<<(CppRuntimeException& exception, bool value) |
52 |
|
|
{ |
53 |
✓✓ |
12 |
return exception << (value ? "true" : "false"); |
54 |
|
|
} |
55 |
|
|
|
56 |
|
14 |
CppRuntimeException& operator<<(CppRuntimeException& exception, float value) |
57 |
|
|
{ |
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 |
|
|
} |
69 |
|
|
|
70 |
|
2 |
CppRuntimeException& operator<<(CppRuntimeException& exception, double value) |
71 |
|
|
{ |
72 |
|
2 |
return exception << (static_cast<float>(value)); |
73 |
|
|
} |
74 |
|
|
|
75 |
|
|
} // namespace zserio |