Coverage Report

Created: 2025-07-03 06:58

/src/Fast-DDS/include/fastdds/rtps/common/SampleIdentity.hpp
Line
Count
Source (jump to first uncovered line)
1
// Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
/**
16
 * @file SampleIdentity.hpp
17
 */
18
19
#ifndef FASTDDS_RTPS_COMMON__SAMPLEIDENTITY_HPP
20
#define FASTDDS_RTPS_COMMON__SAMPLEIDENTITY_HPP
21
22
#include <fastdds/rtps/common/Guid.hpp>
23
#include <fastdds/rtps/common/SequenceNumber.hpp>
24
25
namespace eprosima {
26
namespace fastdds {
27
namespace rtps {
28
29
/*!
30
 * @brief This class is used to specify a sample
31
 * @ingroup COMMON_MODULE
32
 */
33
class FASTDDS_EXPORTED_API SampleIdentity
34
{
35
public:
36
37
    /*!
38
     * @brief
39
     */
40
    bool operator ==(
41
            const SampleIdentity& sample_id) const
42
0
    {
43
0
        return (writer_guid_ == sample_id.writer_guid_) && (sequence_number_ == sample_id.sequence_number_);
44
0
    }
45
46
    /*!
47
     * @brief
48
     */
49
    bool operator !=(
50
            const SampleIdentity& sample_id) const
51
0
    {
52
0
        return !(*this == sample_id);
53
0
    }
54
55
    /**
56
     * @brief To allow using SampleIdentity as map key.
57
     * @param sample
58
     * @return
59
     */
60
    bool operator <(
61
            const SampleIdentity& sample) const
62
0
    {
63
0
        return writer_guid_ < sample.writer_guid_
64
0
               || (writer_guid_ == sample.writer_guid_
65
0
               && sequence_number_ < sample.sequence_number_);
66
0
    }
67
68
    SampleIdentity& writer_guid(
69
            const GUID_t& guid)
70
0
    {
71
0
        writer_guid_ = guid;
72
0
        return *this;
73
0
    }
74
75
    SampleIdentity& writer_guid(
76
            GUID_t&& guid)
77
0
    {
78
0
        writer_guid_ = std::move(guid);
79
0
        return *this;
80
0
    }
81
82
    const GUID_t& writer_guid() const
83
0
    {
84
0
        return writer_guid_;
85
0
    }
86
87
    GUID_t& writer_guid()
88
0
    {
89
0
        return writer_guid_;
90
0
    }
91
92
    SampleIdentity& sequence_number(
93
            const SequenceNumber_t& seq)
94
0
    {
95
0
        sequence_number_ = seq;
96
0
        return *this;
97
0
    }
98
99
    SampleIdentity& sequence_number(
100
            SequenceNumber_t&& seq)
101
0
    {
102
0
        sequence_number_ = std::move(seq);
103
0
        return *this;
104
0
    }
105
106
    const SequenceNumber_t& sequence_number() const
107
0
    {
108
0
        return sequence_number_;
109
0
    }
110
111
    SequenceNumber_t& sequence_number()
112
0
    {
113
0
        return sequence_number_;
114
0
    }
115
116
    static SampleIdentity unknown()
117
0
    {
118
0
        return SampleIdentity();
119
0
    }
120
121
private:
122
123
    GUID_t writer_guid_ = GUID_t::unknown();
124
125
    SequenceNumber_t sequence_number_ = SequenceNumber_t::unknown();
126
127
    friend std::istream& operator >>(
128
            std::istream& input,
129
            SampleIdentity& sid);
130
    friend std::ostream& operator <<(
131
            std::ostream& output,
132
            const SampleIdentity& sid);
133
};
134
135
#ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
136
137
/**
138
 * Stream operator, retrieves a GUID.
139
 * @param input Input stream.
140
 * @param sid SampleIdentity to read.
141
 * @return Stream operator.
142
 */
143
inline std::istream& operator >>(
144
        std::istream& input,
145
        SampleIdentity& sid)
146
0
{
147
0
    std::istream::sentry s(input);
148
149
0
    if (s)
150
0
    {
151
0
        std::ios_base::iostate excp_mask = input.exceptions();
152
153
0
        try
154
0
        {
155
0
            input.exceptions(excp_mask | std::ios_base::failbit | std::ios_base::badbit);
156
157
0
            char sep;
158
0
            input >> sid.writer_guid_ >> sep >> sid.sequence_number_;
159
160
0
            if (sep != '|')
161
0
            {
162
0
                input.setstate(std::ios_base::failbit);
163
0
            }
164
0
        }
165
0
        catch (std::ios_base::failure&)
166
0
        {
167
            // maybe is unknown or just invalid
168
0
            sid.writer_guid_ = GUID_t::unknown();
169
0
            sid.sequence_number_ = SequenceNumber_t::unknown();
170
0
        }
171
172
0
        input.exceptions(excp_mask);
173
0
    }
174
175
0
    return input;
176
0
}
177
178
/**
179
 * Stream operator, prints a GUID.
180
 * @param output Output stream.
181
 * @param sid SampleIdentity to print.
182
 * @return Stream operator.
183
 */
184
inline std::ostream& operator <<(
185
        std::ostream& output,
186
        const SampleIdentity& sid)
187
0
{
188
0
    output << sid.writer_guid_ << '|' << sid.sequence_number_;
189
190
0
    return output;
191
0
}
192
193
#endif // DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
194
195
} //namespace rtps
196
} //namespace fastdds
197
} //namespace eprosima
198
199
#endif // FASTDDS_RTPS_COMMON__SAMPLEIDENTITY_HPP