Coverage Report

Created: 2022-08-24 06:19

/src/Fast-DDS/include/fastdds/rtps/common/Guid.h
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.h
17
 */
18
19
#ifndef _FASTDDS_RTPS_RTPS_GUID_H_
20
#define _FASTDDS_RTPS_RTPS_GUID_H_
21
22
#include <fastrtps/fastrtps_dll.h>
23
#include <fastdds/rtps/common/Types.h>
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 fastrtps {
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 RTPS_DllAPI 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
54.3k
    {
51
54.3k
    }
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
        : guidPrefix(guid_prefix)
62
        , 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
        : guidPrefix(guid_prefix)
74
        , entityId(entity_id)
75
0
    {
76
0
    }
77
78
    /**
79
     * Checks whether this guid is for an entity on the same host as another guid.
80
     *
81
     * @param other_guid GUID_t to compare to.
82
     *
83
     * @return true when this guid is on the same host, false otherwise.
84
     */
85
    bool is_on_same_host_as(
86
            const GUID_t& other_guid) const
87
0
    {
88
0
        return memcmp(guidPrefix.value, other_guid.guidPrefix.value, 4) == 0;
89
0
    }
90
91
    /**
92
     * Checks whether this guid is for an entity on the same host and process as another guid.
93
     *
94
     * @param other_guid GUID_t to compare to.
95
     *
96
     * @return true when this guid is on the same host and process, false otherwise.
97
     */
98
    bool is_on_same_process_as(
99
            const GUID_t& other_guid) const
100
0
    {
101
0
        return memcmp(guidPrefix.value, other_guid.guidPrefix.value, 8) == 0;
102
0
    }
103
104
    /**
105
     * Checks whether this guid corresponds to a builtin entity.
106
     *
107
     * @return true when this guid corresponds to a builtin entity, false otherwise.
108
     */
109
    bool is_builtin() const
110
0
    {
111
0
        return entityId.value[3] >= 0xC0;
112
0
    }
113
114
    static GUID_t unknown() noexcept
115
6
    {
116
6
        return GUID_t();
117
6
    }
118
119
    // TODO Review this conversion once InstanceHandle_t is implemented as DDS standard defines
120
    explicit operator const InstanceHandle_t&() const
121
0
    {
122
0
        return *reinterpret_cast<const InstanceHandle_t*>(this);
123
0
    }
124
125
};
126
127
#ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
128
129
/**
130
 * GUID comparison operator
131
 * @param g1 First GUID to compare
132
 * @param g2 Second GUID to compare
133
 * @return True if equal
134
 */
135
inline bool operator ==(
136
        const GUID_t& g1,
137
        const GUID_t& g2)
138
0
{
139
0
    if (g1.guidPrefix == g2.guidPrefix && g1.entityId == g2.entityId)
140
0
    {
141
0
        return true;
142
0
    }
143
0
    else
144
0
    {
145
0
        return false;
146
0
    }
147
0
}
148
149
/**
150
 * GUID comparison operator
151
 * @param g1 First GUID to compare
152
 * @param g2 Second GUID to compare
153
 * @return True if not equal
154
 */
155
inline bool operator !=(
156
        const GUID_t& g1,
157
        const GUID_t& g2)
158
0
{
159
0
    if (g1.guidPrefix != g2.guidPrefix || g1.entityId != g2.entityId)
160
0
    {
161
0
        return true;
162
0
    }
163
0
    else
164
0
    {
165
0
        return false;
166
0
    }
167
0
}
168
169
inline bool operator <(
170
        const GUID_t& g1,
171
        const GUID_t& g2)
172
0
{
173
0
    for (uint8_t i = 0; i < 12; ++i)
174
0
    {
175
0
        if (g1.guidPrefix.value[i] < g2.guidPrefix.value[i])
176
0
        {
177
0
            return true;
178
0
        }
179
0
        else if (g1.guidPrefix.value[i] > g2.guidPrefix.value[i])
180
0
        {
181
0
            return false;
182
0
        }
183
0
    }
184
0
    for (uint8_t i = 0; i < 4; ++i)
185
0
    {
186
0
        if (g1.entityId.value[i] < g2.entityId.value[i])
187
0
        {
188
0
            return true;
189
0
        }
190
0
        else if (g1.entityId.value[i] > g2.entityId.value[i])
191
0
        {
192
0
            return false;
193
0
        }
194
0
    }
195
0
    return false;
196
0
}
197
198
#endif // ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
199
200
const GUID_t c_Guid_Unknown;
201
202
#ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
203
204
/**
205
 * Stream operator, prints a GUID.
206
 * @param output Output stream.
207
 * @param guid GUID_t to print.
208
 * @return Stream operator.
209
 */
210
inline std::ostream& operator <<(
211
        std::ostream& output,
212
        const GUID_t& guid)
213
0
{
214
0
    if (guid != c_Guid_Unknown)
215
0
    {
216
0
        output << guid.guidPrefix << "|" << guid.entityId;
217
0
    }
218
0
    else
219
0
    {
220
0
        output << "|GUID UNKNOWN|";
221
0
    }
222
0
    return output;
223
0
}
224
225
/**
226
 * Stream operator, retrieves a GUID.
227
 * @param input Input stream.
228
 * @param guid GUID_t to print.
229
 * @return Stream operator.
230
 */
231
inline std::istream& operator >>(
232
        std::istream& input,
233
        GUID_t& guid)
234
0
{
235
0
    std::istream::sentry s(input);
236
0
237
0
    if (s)
238
0
    {
239
0
        std::ios_base::iostate excp_mask = input.exceptions();
240
0
241
0
        try
242
0
        {
243
0
            input.exceptions(excp_mask | std::ios_base::failbit | std::ios_base::badbit);
244
0
245
0
            char sep;
246
0
            input >> guid.guidPrefix >> sep >> guid.entityId;
247
0
248
0
            if (sep != '|')
249
0
            {
250
0
                input.setstate(std::ios_base::failbit);
251
0
            }
252
0
        }
253
0
        catch (std::ios_base::failure&)
254
0
        {
255
0
            // maybe is unknown or just invalid
256
0
            guid = c_Guid_Unknown;
257
0
        }
258
0
259
0
        input.exceptions(excp_mask);
260
0
    }
261
0
262
0
    return input;
263
0
}
264
265
#endif // ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
266
267
} // namespace rtps
268
} // namespace fastrtps
269
} // namespace eprosima
270
271
#endif /* _FASTDDS_RTPS_RTPS_GUID_H_ */