Coverage Report

Created: 2025-06-13 06:46

/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 Default constructor. Constructs an unknown SampleIdentity.
39
     */
40
    SampleIdentity()
41
8
        : writer_guid_(GUID_t::unknown())
42
8
        , sequence_number_(SequenceNumber_t::unknown())
43
8
    {
44
8
    }
45
46
    /*!
47
     * @brief Copy constructor.
48
     */
49
    SampleIdentity(
50
            const SampleIdentity& sample_id)
51
0
        : writer_guid_(sample_id.writer_guid_)
52
0
        , sequence_number_(sample_id.sequence_number_)
53
0
    {
54
0
    }
55
56
    /*!
57
     * @brief Move constructor.
58
     */
59
    SampleIdentity(
60
            SampleIdentity&& sample_id)
61
0
        : writer_guid_(std::move(sample_id.writer_guid_))
62
0
        , sequence_number_(std::move(sample_id.sequence_number_))
63
0
    {
64
0
    }
65
66
    /*!
67
     * @brief Assignment operator.
68
     */
69
    SampleIdentity& operator =(
70
            const SampleIdentity& sample_id)
71
0
    {
72
0
        writer_guid_ = sample_id.writer_guid_;
73
0
        sequence_number_ = sample_id.sequence_number_;
74
0
        return *this;
75
0
    }
76
77
    /*!
78
     * @brief Move constructor.
79
     */
80
    SampleIdentity& operator =(
81
            SampleIdentity&& sample_id)
82
0
    {
83
0
        writer_guid_ = std::move(sample_id.writer_guid_);
84
0
        sequence_number_ = std::move(sample_id.sequence_number_);
85
0
        return *this;
86
0
    }
87
88
    /*!
89
     * @brief
90
     */
91
    bool operator ==(
92
            const SampleIdentity& sample_id) const
93
0
    {
94
0
        return (writer_guid_ == sample_id.writer_guid_) && (sequence_number_ == sample_id.sequence_number_);
95
0
    }
96
97
    /*!
98
     * @brief
99
     */
100
    bool operator !=(
101
            const SampleIdentity& sample_id) const
102
0
    {
103
0
        return !(*this == sample_id);
104
0
    }
105
106
    /**
107
     * @brief To allow using SampleIdentity as map key.
108
     * @param sample
109
     * @return
110
     */
111
    bool operator <(
112
            const SampleIdentity& sample) const
113
0
    {
114
0
        return writer_guid_ < sample.writer_guid_
115
0
               || (writer_guid_ == sample.writer_guid_
116
0
               && sequence_number_ < sample.sequence_number_);
117
0
    }
118
119
    SampleIdentity& writer_guid(
120
            const GUID_t& guid)
121
0
    {
122
0
        writer_guid_ = guid;
123
0
        return *this;
124
0
    }
125
126
    SampleIdentity& writer_guid(
127
            GUID_t&& guid)
128
0
    {
129
0
        writer_guid_ = std::move(guid);
130
0
        return *this;
131
0
    }
132
133
    const GUID_t& writer_guid() const
134
0
    {
135
0
        return writer_guid_;
136
0
    }
137
138
    GUID_t& writer_guid()
139
0
    {
140
0
        return writer_guid_;
141
0
    }
142
143
    SampleIdentity& sequence_number(
144
            const SequenceNumber_t& seq)
145
0
    {
146
0
        sequence_number_ = seq;
147
0
        return *this;
148
0
    }
149
150
    SampleIdentity& sequence_number(
151
            SequenceNumber_t&& seq)
152
0
    {
153
0
        sequence_number_ = std::move(seq);
154
0
        return *this;
155
0
    }
156
157
    const SequenceNumber_t& sequence_number() const
158
0
    {
159
0
        return sequence_number_;
160
0
    }
161
162
    SequenceNumber_t& sequence_number()
163
0
    {
164
0
        return sequence_number_;
165
0
    }
166
167
    static SampleIdentity unknown()
168
0
    {
169
0
        return SampleIdentity();
170
0
    }
171
172
private:
173
174
    GUID_t writer_guid_;
175
176
    SequenceNumber_t sequence_number_;
177
178
    friend std::istream& operator >>(
179
            std::istream& input,
180
            SampleIdentity& sid);
181
    friend std::ostream& operator <<(
182
            std::ostream& output,
183
            const SampleIdentity& sid);
184
};
185
186
#ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
187
188
/**
189
 * Stream operator, retrieves a GUID.
190
 * @param input Input stream.
191
 * @param sid SampleIdentity to read.
192
 * @return Stream operator.
193
 */
194
inline std::istream& operator >>(
195
        std::istream& input,
196
        SampleIdentity& sid)
197
0
{
198
0
    std::istream::sentry s(input);
199
200
0
    if (s)
201
0
    {
202
0
        std::ios_base::iostate excp_mask = input.exceptions();
203
204
0
        try
205
0
        {
206
0
            input.exceptions(excp_mask | std::ios_base::failbit | std::ios_base::badbit);
207
208
0
            char sep;
209
0
            input >> sid.writer_guid_ >> sep >> sid.sequence_number_;
210
211
0
            if (sep != '|')
212
0
            {
213
0
                input.setstate(std::ios_base::failbit);
214
0
            }
215
0
        }
216
0
        catch (std::ios_base::failure&)
217
0
        {
218
            // maybe is unknown or just invalid
219
0
            sid.writer_guid_ = GUID_t::unknown();
220
0
            sid.sequence_number_ = SequenceNumber_t::unknown();
221
0
        }
222
223
0
        input.exceptions(excp_mask);
224
0
    }
225
226
0
    return input;
227
0
}
228
229
/**
230
 * Stream operator, prints a GUID.
231
 * @param output Output stream.
232
 * @param sid SampleIdentity to print.
233
 * @return Stream operator.
234
 */
235
inline std::ostream& operator <<(
236
        std::ostream& output,
237
        const SampleIdentity& sid)
238
0
{
239
0
    output << sid.writer_guid_ << '|' << sid.sequence_number_;
240
241
0
    return output;
242
0
}
243
244
#endif // DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
245
246
} //namespace rtps
247
} //namespace fastdds
248
} //namespace eprosima
249
250
#endif // FASTDDS_RTPS_COMMON__SAMPLEIDENTITY_HPP