Coverage Report

Created: 2024-09-23 09:09

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