Coverage Report

Created: 2026-10-19 14:15

src/zserio/ParsingInfo.h
Line
Count
Source
1
#ifndef ZSERIO_PARSING_INFO_INC_H
2
#define ZSERIO_PARSING_INFO_INC_H
3
4
#include <limits>
5
6
#include "zserio/CppRuntimeException.h"
7
#include "zserio/Types.h"
8
9
namespace zserio
10
{
11
12
/**
13
 * The parsing information returned from parsingInfo() method generated in case of '-withParsingInfo' command
14
 * line option.
15
 */
16
class ParsingInfo
17
{
18
public:
19
    /**
20
     * Default constructor.
21
     */
22
    ParsingInfo() :
23
            m_bitPosition(INVALID_BIT_POSITION),
24
            m_bitSize(INVALID_BIT_SIZE)
25
394
    {}
26
27
    /**
28
     * Constructor from bit position.
29
     *
30
     * \param bitPosition Bit position in bits.
31
     */
32
    explicit ParsingInfo(size_t bitPosition) :
33
            m_bitPosition(bitPosition),
34
            m_bitSize(INVALID_BIT_SIZE)
35
840
    {}
36
37
    /**
38
     * Initializes the bit size using the end bit position.
39
     *
40
     * It is supposed that bit position has been already correctly set before this method is called.
41
     *
42
     * \param endBitPosition End bit position to calculate the bit size from.
43
     *
44
     * \throw CppRuntimeException In case of invalid end bit position argument.
45
     */
46
    void initializeBitSize(size_t endBitPosition)
47
813
    {
48
813
        const size_t bitPosition = getBitPosition();
49
813
        if (endBitPosition < bitPosition)
50
1
        {
51
1
            throw CppRuntimeException("Invalid end bit position argument (")
52
1
                    << endBitPosition << " < " << bitPosition << ")!";
53
1
        }
54
55
812
        m_bitSize = endBitPosition - getBitPosition();
56
812
    }
57
58
    /**
59
     * Gets the bit position.
60
     *
61
     * \return The blob offset of the object in bits counted from zero.
62
     *
63
     * \throw CppRuntimeException In case of invalid (uninitialized) bit position.
64
     */
65
    size_t getBitPosition() const
66
1.63k
    {
67
1.63k
        if (m_bitPosition == INVALID_BIT_POSITION)
68
2
        {
69
2
            throw CppRuntimeException("Invalid bit position, call the reader constructor first!");
70
2
        }
71
72
1.62k
        return m_bitPosition;
73
1.63k
    }
74
75
    /**
76
     * Gets the bit size.
77
     *
78
     * \return Blob size of the object in bits.
79
     *
80
     * \throw CppRuntimeException In case of invalid (uninitialized) bit size.
81
     */
82
    size_t getBitSize() const
83
5
    {
84
5
        if (m_bitSize == INVALID_BIT_SIZE)
85
3
        {
86
3
            throw CppRuntimeException("Invalid bit size, call the reader constructor first!");
87
3
        }
88
89
2
        return m_bitSize;
90
5
    }
91
92
private:
93
    static constexpr size_t INVALID_BIT_POSITION = std::numeric_limits<size_t>::max();
94
    static constexpr size_t INVALID_BIT_SIZE = std::numeric_limits<size_t>::max();
95
96
    size_t m_bitPosition;
97
    size_t m_bitSize;
98
};
99
100
} // namespace zserio
101
102
#endif // ZSERIO_PARSING_INFO_INC_H