Fast DDS  Version 3.1.0
Fast DDS
Loading...
Searching...
No Matches
InstanceHandle.hpp
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
19#ifndef FASTDDS_RTPS_COMMON__INSTANCEHANDLE_HPP
20#define FASTDDS_RTPS_COMMON__INSTANCEHANDLE_HPP
21
22#include <array>
23
24#include <fastdds/fastdds_dll.hpp>
25#include <fastdds/rtps/common/Types.hpp>
26#include <fastdds/rtps/common/Guid.hpp>
27
28namespace eprosima {
29namespace fastdds {
30namespace rtps {
31
32constexpr const uint8_t RTPS_KEY_HASH_SIZE = 16;
33
34using KeyHash_t = std::array<octet, RTPS_KEY_HASH_SIZE>;
35
36struct FASTDDS_EXPORTED_API InstanceHandleValue_t
37{
50 template<typename T>
51 octet& operator [] (
52 T i) noexcept
53 {
54 has_been_set_ = true;
55 return value_[i];
56 }
57
68 template<typename T>
69 octet operator [] (
70 T i) const noexcept
71 {
72 return value_[i];
73 }
74
85 operator octet* () noexcept
86 {
87 has_been_set_ = true;
88 return value_.data();
89 }
90
99 operator const octet* () const noexcept
100 {
101 return value_.data();
102 }
103
107 bool has_been_set() const noexcept
108 {
109 return has_been_set_;
110 }
111
112 void clear() noexcept
113 {
114 value_.fill(0);
115 has_been_set_ = false;
116 }
117
121 bool operator == (
122 const InstanceHandleValue_t& other) const noexcept
123 {
124 return (has_been_set_ == other.has_been_set_) && (value_ == other.value_);
125 }
126
130 bool operator < (
131 const InstanceHandleValue_t& other) const noexcept
132 {
133 if (has_been_set_)
134 {
135 return other.has_been_set_ && value_ < other.value_;
136 }
137
138 return other.has_been_set_;
139 }
140
141private:
142
144 KeyHash_t value_ {};
146 bool has_been_set_ = false;
147};
148
153struct FASTDDS_EXPORTED_API InstanceHandle_t
154{
157
158 InstanceHandle_t() noexcept = default;
159
161 const InstanceHandle_t& ihandle) noexcept = default;
162
164 const GUID_t& guid) noexcept
165 {
166 *this = guid;
167 }
168
173 InstanceHandle_t& operator =(
174 const InstanceHandle_t& ihandle) noexcept = default;
175
180 InstanceHandle_t& operator =(
181 const GUID_t& guid) noexcept
182 {
183 octet* dst = value;
184 memcpy(dst, guid.guidPrefix.value, 12);
185 memcpy(&dst[12], guid.entityId.value, 4);
186 return *this;
187 }
188
193 bool isDefined() const noexcept
194 {
195 return value.has_been_set();
196 }
197
198 void clear() noexcept
199 {
200 value.clear();
201 }
202
203 // TODO Review this conversion once InstanceHandle_t is implemented as DDS standard defines
204 explicit operator const GUID_t&() const noexcept
205 {
206 return *reinterpret_cast<const GUID_t*>(this);
207 }
208
209};
210
212
213#ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
214
221inline bool operator ==(
222 const InstanceHandle_t& ihandle1,
223 const InstanceHandle_t& ihandle2) noexcept
224{
225 return ihandle1.value == ihandle2.value;
226}
227
235inline bool operator !=(
236 const InstanceHandle_t& ihandle1,
237 const InstanceHandle_t& ihandle2) noexcept
238{
239 return !(ihandle1 == ihandle2);
240}
241
242#endif // ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
243
249inline void iHandle2GUID(
250 GUID_t& guid,
251 const InstanceHandle_t& ihandle) noexcept
252{
253 const octet* value = ihandle.value;
254 memcpy(guid.guidPrefix.value, value, 12);
255 memcpy(guid.entityId.value, &value[12], 4);
256}
257
264 const InstanceHandle_t& ihandle) noexcept
265{
266 GUID_t guid;
267 iHandle2GUID(guid, ihandle);
268 return guid;
269}
270
278inline bool operator <(
279 const InstanceHandle_t& h1,
280 const InstanceHandle_t& h2) noexcept
281{
282 return h1.value < h2.value;
283}
284
285#ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
286
294inline std::ostream& operator <<(
295 std::ostream& output,
296 const InstanceHandle_t& iHandle)
297{
298 std::stringstream ss;
299 ss << std::hex;
300 for (uint8_t i = 0; i < 15; ++i)
301 {
302 ss << (int)iHandle.value[i] << ".";
303 }
304 ss << (int)iHandle.value[15u] << std::dec;
305 return output << ss.str();
306}
307
315inline std::istream& operator >>(
316 std::istream& input,
317 InstanceHandle_t& iHandle)
318{
319 std::istream::sentry s(input);
320
321 if (s)
322 {
323 char point;
324 unsigned short hex;
325 std::ios_base::iostate excp_mask = input.exceptions();
326
327 try
328 {
329 input.exceptions(excp_mask | std::ios_base::failbit | std::ios_base::badbit);
330 input >> std::hex >> hex;
331
332 if (hex > 255)
333 {
334 input.setstate(std::ios_base::failbit);
335 }
336
337 iHandle.value[0u] = static_cast<octet>(hex);
338
339 for (uint8_t i = 1; i < 16; ++i)
340 {
341 input >> point >> hex;
342 if ( point != '.' || hex > 255 )
343 {
344 input.setstate(std::ios_base::failbit);
345 }
346 iHandle.value[i] = static_cast<octet>(hex);
347 }
348
349 input >> std::dec;
350 }
351 catch (std::ios_base::failure& )
352 {
353 }
354
355 input.exceptions(excp_mask);
356 }
357
358 return input;
359}
360
361#endif // ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
362
363} // namespace rtps
364} // namespace fastdds
365} // namespace eprosima
366
367#endif // FASTDDS_RTPS_COMMON__INSTANCEHANDLE_HPP
std::istream & operator>>(std::istream &input, EntityId_t &enP)
Definition EntityId_t.hpp:289
bool operator==(const BuiltinTransportsOptions &bto1, const BuiltinTransportsOptions &bto2)
Equal to operator.
Definition BuiltinTransports.hpp:79
std::ostream & operator<<(std::ostream &output, BuiltinTransports transports)
Definition BuiltinTransports.hpp:117
unsigned char octet
Definition Types.hpp:83
bool operator!=(const EntityId_t &id1, const EntityId_t &id2)
Guid prefix comparison operator.
Definition EntityId_t.hpp:267
std::array< octet, RTPS_KEY_HASH_SIZE > KeyHash_t
Definition InstanceHandle.hpp:34
bool operator<(const GUID_t &g1, const GUID_t &g2)
Definition Guid.hpp:192
constexpr const uint8_t RTPS_KEY_HASH_SIZE
Definition InstanceHandle.hpp:32
const InstanceHandle_t c_InstanceHandle_Unknown
Definition InstanceHandle.hpp:211
void iHandle2GUID(GUID_t &guid, const InstanceHandle_t &ihandle) noexcept
Convert InstanceHandle_t to GUID.
Definition InstanceHandle.hpp:249
eProsima namespace.
Structure GUID_t, entity identifier, unique in DDS-RTPS Domain.
Definition Guid.hpp:40
Struct InstanceHandle_t, used to contain the key for WITH_KEY topics.
Definition InstanceHandle.hpp:154
void clear() noexcept
Definition InstanceHandle.hpp:198
bool isDefined() const noexcept
Know if the instance handle is defined.
Definition InstanceHandle.hpp:193
InstanceHandleValue_t value
Value.
Definition InstanceHandle.hpp:156
Definition InstanceHandle.hpp:37
void clear() noexcept
Definition InstanceHandle.hpp:112
bool has_been_set() const noexcept
Return whether any of the write access operators of this value has been used.
Definition InstanceHandle.hpp:107