Coverage Report

Created: 2025-06-13 06:46

/src/Fast-DDS/include/fastdds/rtps/common/Guid.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 Guid.hpp
17
 */
18
19
#ifndef FASTDDS_RTPS_COMMON__GUID_HPP
20
#define FASTDDS_RTPS_COMMON__GUID_HPP
21
22
#include <fastdds/fastdds_dll.hpp>
23
#include <fastdds/rtps/common/Types.hpp>
24
#include <fastdds/rtps/common/GuidPrefix_t.hpp>
25
#include <fastdds/rtps/common/EntityId_t.hpp>
26
27
#include <cstdint>
28
#include <cstring>
29
#include <sstream>
30
31
namespace eprosima {
32
namespace fastdds {
33
namespace rtps {
34
35
struct InstanceHandle_t;
36
37
//!@brief Structure GUID_t, entity identifier, unique in DDS-RTPS Domain.
38
//!@ingroup COMMON_MODULE
39
struct FASTDDS_EXPORTED_API GUID_t
40
{
41
    //!Guid prefix
42
    GuidPrefix_t guidPrefix;
43
    //!Entity id
44
    EntityId_t entityId;
45
46
    /*!
47
     * Default constructor. Contructs an unknown GUID.
48
     */
49
    GUID_t() noexcept
50
25.1k
    {
51
25.1k
    }
52
53
    /**
54
     * Construct
55
     * @param guid_prefix Guid prefix
56
     * @param id Entity id
57
     */
58
    GUID_t(
59
            const GuidPrefix_t& guid_prefix,
60
            uint32_t id) noexcept
61
0
        : guidPrefix(guid_prefix)
62
0
        , entityId(id)
63
0
    {
64
0
    }
65
66
    /**
67
     * @param guid_prefix Guid prefix
68
     * @param entity_id Entity id
69
     */
70
    GUID_t(
71
            const GuidPrefix_t& guid_prefix,
72
            const EntityId_t& entity_id) noexcept
73
0
        : guidPrefix(guid_prefix)
74
0
        , entityId(entity_id)
75
0
    {
76
0
    }
77
78
    /**
79
     * Checks whether this guid is from an entity on the same host as another guid.
80
     *
81
     * @note This method assumes the value of \c other_guid was originally assigned by Fast-DDS vendor.
82
     *
83
     * @param other_guid GUID_t to compare to.
84
     *
85
     * @return true when this guid is on the same host, false otherwise.
86
     */
87
    bool is_on_same_host_as(
88
            const GUID_t& other_guid) const
89
0
    {
90
0
        return guidPrefix.is_on_same_host_as(other_guid.guidPrefix);
91
0
    }
92
93
    /**
94
     * Checks whether this guid is from a (Fast-DDS) entity created on this host (from where this method is called).
95
     *
96
     * @return true when this guid is from a (Fast-DDS) entity created on this host, false otherwise.
97
     */
98
    bool is_from_this_host() const
99
0
    {
100
0
        return guidPrefix.is_from_this_host();
101
0
    }
102
103
    /**
104
     * Checks whether this guid is for an entity on the same host and process as another guid.
105
     *
106
     * @note This method assumes the value of \c other_guid was originally assigned by Fast-DDS vendor.
107
     *
108
     * @param other_guid GUID_t to compare to.
109
     *
110
     * @return true when this guid is on the same host and process, false otherwise.
111
     */
112
    bool is_on_same_process_as(
113
            const GUID_t& other_guid) const
114
0
    {
115
0
        return guidPrefix.is_on_same_process_as(other_guid.guidPrefix);
116
0
    }
117
118
    /**
119
     * Checks whether this guid is from a (Fast-DDS) entity created on this process (from where this method is called).
120
     *
121
     * @return true when this guid is from a (Fast-DDS) entity created on this process, false otherwise.
122
     */
123
    bool is_from_this_process() const
124
0
    {
125
0
        return guidPrefix.is_from_this_process();
126
0
    }
127
128
    /**
129
     * Checks whether this guid corresponds to a builtin entity.
130
     *
131
     * @return true when this guid corresponds to a builtin entity, false otherwise.
132
     */
133
    bool is_builtin() const
134
0
    {
135
0
        return entityId.value[3] >= 0xC0;
136
0
    }
137
138
    static GUID_t unknown() noexcept
139
8
    {
140
8
        return GUID_t();
141
8
    }
142
143
    // TODO Review this conversion once InstanceHandle_t is implemented as DDS standard defines
144
    explicit operator const InstanceHandle_t&() const
145
0
    {
146
0
        return *reinterpret_cast<const InstanceHandle_t*>(this);
147
0
    }
148
};
149
150
#ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
151
152
/**
153
 * GUID comparison operator
154
 * @param g1 First GUID to compare
155
 * @param g2 Second GUID to compare
156
 * @return True if equal
157
 */
158
inline bool operator ==(
159
        const GUID_t& g1,
160
        const GUID_t& g2)
161
0
{
162
0
    if (g1.guidPrefix == g2.guidPrefix && g1.entityId == g2.entityId)
163
0
    {
164
0
        return true;
165
0
    }
166
0
    else
167
0
    {
168
0
        return false;
169
0
    }
170
0
}
171
172
/**
173
 * GUID comparison operator
174
 * @param g1 First GUID to compare
175
 * @param g2 Second GUID to compare
176
 * @return True if not equal
177
 */
178
inline bool operator !=(
179
        const GUID_t& g1,
180
        const GUID_t& g2)
181
0
{
182
0
    if (g1.guidPrefix != g2.guidPrefix || g1.entityId != g2.entityId)
183
0
    {
184
0
        return true;
185
0
    }
186
0
    else
187
0
    {
188
0
        return false;
189
0
    }
190
0
}
191
192
inline bool operator <(
193
        const GUID_t& g1,
194
        const GUID_t& g2)
195
0
{
196
0
    auto prefix_cmp = GuidPrefix_t::cmp(g1.guidPrefix, g2.guidPrefix);
197
0
    if (prefix_cmp < 0)
198
0
    {
199
0
        return true;
200
0
    }
201
0
    else if (prefix_cmp > 0)
202
0
    {
203
0
        return false;
204
0
    }
205
0
    else
206
0
    {
207
0
        return g1.entityId < g2.entityId;
208
0
    }
209
0
}
210
211
#endif // ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
212
213
const GUID_t c_Guid_Unknown;
214
215
#ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
216
217
/**
218
 * Stream operator, prints a GUID.
219
 * @param output Output stream.
220
 * @param guid GUID_t to print.
221
 * @return Stream operator.
222
 */
223
inline std::ostream& operator <<(
224
        std::ostream& output,
225
        const GUID_t& guid)
226
0
{
227
0
    if (guid != c_Guid_Unknown)
228
0
    {
229
0
        output << guid.guidPrefix << "|" << guid.entityId;
230
0
    }
231
0
    else
232
0
    {
233
0
        output << "|GUID UNKNOWN|";
234
0
    }
235
0
    return output;
236
0
}
237
238
/**
239
 * Stream operator, retrieves a GUID.
240
 * @param input Input stream.
241
 * @param guid GUID_t to print.
242
 * @return Stream operator.
243
 */
244
inline std::istream& operator >>(
245
        std::istream& input,
246
        GUID_t& guid)
247
0
{
248
0
    std::istream::sentry s(input);
249
250
0
    if (s)
251
0
    {
252
0
        std::ios_base::iostate excp_mask = input.exceptions();
253
254
0
        try
255
0
        {
256
0
            input.exceptions(excp_mask | std::ios_base::failbit | std::ios_base::badbit);
257
258
0
            char sep;
259
0
            input >> guid.guidPrefix >> sep >> guid.entityId;
260
261
0
            if (sep != '|')
262
0
            {
263
0
                input.setstate(std::ios_base::failbit);
264
0
            }
265
0
        }
266
0
        catch (std::ios_base::failure&)
267
0
        {
268
            // maybe is unknown or just invalid
269
0
            guid = c_Guid_Unknown;
270
0
        }
271
272
0
        input.exceptions(excp_mask);
273
0
    }
274
275
0
    return input;
276
0
}
277
278
#endif // ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
279
280
} // namespace rtps
281
} // namespace fastdds
282
} // namespace eprosima
283
284
#endif // FASTDDS_RTPS_COMMON__GUID_HPP