-
Notifications
You must be signed in to change notification settings - Fork 793
/
Copy pathInstanceHandle.h
294 lines (259 loc) · 6.38 KB
/
InstanceHandle.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @file InstanceHandle.h
*/
#ifndef _FASTDDS_RTPS_INSTANCEHANDLE_H_
#define _FASTDDS_RTPS_INSTANCEHANDLE_H_
#include <fastrtps/fastrtps_dll.h>
#include <fastdds/rtps/common/Types.h>
#include <fastdds/rtps/common/Guid.h>
namespace eprosima {
namespace fastrtps {
namespace rtps {
/**
* Struct InstanceHandle_t, used to contain the key for WITH_KEY topics.
* @ingroup COMMON_MODULE
*/
struct RTPS_DllAPI InstanceHandle_t
{
//!Value
octet value[16];
InstanceHandle_t()
{
for (uint8_t i = 0; i < 16; i++)
{
value[i] = 0;
}
}
InstanceHandle_t(
const InstanceHandle_t& ihandle)
{
for (uint8_t i = 0; i < 16; i++)
{
value[i] = ihandle.value[i];
}
}
InstanceHandle_t(
const GUID_t& guid)
{
for (uint8_t i = 0; i < 16; ++i)
{
if (i < 12)
{
value[i] = guid.guidPrefix.value[i];
}
else
{
value[i] = guid.entityId.value[i - 12];
}
}
}
/**
* Assignment operator
* @param ihandle Instance handle to copy the data from
*/
InstanceHandle_t& operator =(
const InstanceHandle_t& ihandle)
{
for (uint8_t i = 0; i < 16; i++)
{
value[i] = ihandle.value[i];
}
return *this;
}
/**
* Assignment operator
* @param guid GUID to copy the data from
*/
InstanceHandle_t& operator =(
const GUID_t& guid)
{
for (uint8_t i = 0; i < 16; i++)
{
if (i < 12)
{
value[i] = guid.guidPrefix.value[i];
}
else
{
value[i] = guid.entityId.value[i - 12];
}
}
return *this;
}
/**
* Know if the instance handle is defined
* @return True if the values are not zero.
*/
bool isDefined() const
{
for (uint8_t i = 0; i < 16; ++i)
{
if (value[i] != 0)
{
return true;
}
}
return false;
}
// TODO Review this conversion once InstanceHandle_t is implemented as DDS standard defines
explicit operator const GUID_t&() const
{
return *reinterpret_cast<const GUID_t*>(this);
}
};
const InstanceHandle_t c_InstanceHandle_Unknown;
#ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
/**
* Comparison operator
* @param ihandle1 First InstanceHandle_t to compare
* @param ihandle2 Second InstanceHandle_t to compare
* @return True if equal
*/
inline bool operator ==(
const InstanceHandle_t& ihandle1,
const InstanceHandle_t& ihandle2)
{
for (uint8_t i = 0; i < 16; ++i)
{
if (ihandle1.value[i] != ihandle2.value[i])
{
return false;
}
}
return true;
}
inline bool operator !=(
const InstanceHandle_t& ihandle1,
const InstanceHandle_t& ihandle2)
{
return !(ihandle1 == ihandle2);
}
#endif // ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
/**
* Convert InstanceHandle_t to GUID
* @param guid GUID to store the results
* @param ihandle InstanceHandle_t to copy
*/
inline void iHandle2GUID(
GUID_t& guid,
const InstanceHandle_t& ihandle)
{
for (uint8_t i = 0; i < 16; ++i)
{
if (i < 12)
{
guid.guidPrefix.value[i] = ihandle.value[i];
}
else
{
guid.entityId.value[i - 12] = ihandle.value[i];
}
}
return;
}
/**
* Convert GUID to InstanceHandle_t
* @param ihandle InstanceHandle_t to store the results
* @return GUID_t
*/
inline GUID_t iHandle2GUID(
const InstanceHandle_t& ihandle)
{
GUID_t guid;
for (uint8_t i = 0; i < 16; ++i)
{
if (i < 12)
{
guid.guidPrefix.value[i] = ihandle.value[i];
}
else
{
guid.entityId.value[i - 12] = ihandle.value[i];
}
}
return guid;
}
inline bool operator <(
const InstanceHandle_t& h1,
const InstanceHandle_t& h2)
{
return memcmp(h1.value, h2.value, 16) < 0;
}
#ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
/**
*
* @param output
* @param iHandle
*/
inline std::ostream& operator <<(
std::ostream& output,
const InstanceHandle_t& iHandle)
{
output << std::hex;
for (uint8_t i = 0; i < 15; ++i)
{
output << (int)iHandle.value[i] << ".";
}
output << (int)iHandle.value[15] << std::dec;
return output;
}
/**
*
* @param input
* @param iHandle
*/
inline std::istream& operator >>(
std::istream& input,
InstanceHandle_t& iHandle)
{
std::istream::sentry s(input);
if (s)
{
char point;
unsigned short hex;
std::ios_base::iostate excp_mask = input.exceptions();
try
{
input.exceptions(excp_mask | std::ios_base::failbit | std::ios_base::badbit);
input >> std::hex >> hex;
if (hex > 255)
{
input.setstate(std::ios_base::failbit);
}
iHandle.value[0] = static_cast<octet>(hex);
for (int i = 1; i < 16; ++i)
{
input >> point >> hex;
if ( point != '.' || hex > 255 )
{
input.setstate(std::ios_base::failbit);
}
iHandle.value[i] = static_cast<octet>(hex);
}
input >> std::dec;
}
catch (std::ios_base::failure& )
{
}
input.exceptions(excp_mask);
}
return input;
}
#endif // ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
} // namespace rtps
} // namespace fastrtps
} // namespace eprosima
#endif /* _FASTDDS_RTPS_INSTANCEHANDLE_H_ */