Coverage Report

Created: 2025-07-31 16:33

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