19
19
#ifndef _FASTDDS_RTPS_INSTANCEHANDLE_H_
20
20
#define _FASTDDS_RTPS_INSTANCEHANDLE_H_
21
21
22
+ #include < array>
23
+
22
24
#include < fastrtps/fastrtps_dll.h>
23
25
#include < fastdds/rtps/common/Types.h>
24
26
#include < fastdds/rtps/common/Guid.h>
@@ -27,101 +29,164 @@ namespace eprosima {
27
29
namespace fastrtps {
28
30
namespace rtps {
29
31
32
+ using KeyHash_t = std::array<octet, 16 >;
33
+
34
+ struct RTPS_DllAPI InstanceHandleValue_t
35
+ {
36
+ /* *
37
+ * Write access indexing operator.
38
+ *
39
+ * Provides a reference to the byte value at position @c i.
40
+ *
41
+ * @param [in] i index of the byte to return.
42
+ *
43
+ * @post Method has_been_set() returns @c true.
44
+ *
45
+ * @remark Do not use this method to check if this value has been set.
46
+ * Use method has_been_set() instead.
47
+ */
48
+ octet& operator [] (
49
+ size_t i) noexcept
50
+ {
51
+ has_been_set_ = true ;
52
+ return value_[i];
53
+ }
54
+
55
+ /* *
56
+ * Read access indexing operator.
57
+ *
58
+ * Provides the byte value at position @c i.
59
+ *
60
+ * @param [in] i index of the byte to return.
61
+ *
62
+ * @remark Do not use this method to check if this value has been set.
63
+ * Use method has_been_set() instead.
64
+ */
65
+ octet operator [] (
66
+ size_t i) const noexcept
67
+ {
68
+ return value_[i];
69
+ }
70
+
71
+ /* *
72
+ * Write access pointer cast operator.
73
+ *
74
+ * Provides a pointer to the start of the raw data.
75
+ *
76
+ * @post Method has_been_set() returns @c true.
77
+ *
78
+ * @remark Do not use this method to check if this value has been set.
79
+ * Use method has_been_set() instead.
80
+ */
81
+ operator octet* () noexcept
82
+ {
83
+ has_been_set_ = true ;
84
+ return value_.data ();
85
+ }
86
+
87
+ /* *
88
+ * Read access pointer cast operator.
89
+ *
90
+ * Provides a pointer to the start of the raw data.
91
+ *
92
+ * @remark Do not use this method to check if this value has been set.
93
+ * Use method has_been_set() instead.
94
+ */
95
+ operator const octet* () const noexcept
96
+ {
97
+ return value_.data ();
98
+ }
99
+
100
+ /* *
101
+ * Return whether any of the write access operators of this value has been used.
102
+ */
103
+ bool has_been_set () const noexcept
104
+ {
105
+ return has_been_set_;
106
+ }
107
+
108
+ /* *
109
+ * Equality comparison operator.
110
+ */
111
+ bool operator == (
112
+ const InstanceHandleValue_t& other) const noexcept
113
+ {
114
+ return (has_been_set_ == other.has_been_set_ ) && (value_ == other.value_ );
115
+ }
116
+
117
+ /* *
118
+ * Less than comparisor operator.
119
+ */
120
+ bool operator < (
121
+ const InstanceHandleValue_t& other) const noexcept
122
+ {
123
+ if (has_been_set_)
124
+ {
125
+ return other.has_been_set_ && value_ < other.value_ ;
126
+ }
127
+
128
+ return other.has_been_set_ ;
129
+ }
130
+
131
+ private:
132
+
133
+ // ! Hash value
134
+ KeyHash_t value_ {};
135
+ // ! Flag indicating if value_ has been modified since the creation of this object
136
+ bool has_been_set_ = false ;
137
+ };
138
+
30
139
/* *
31
140
* Struct InstanceHandle_t, used to contain the key for WITH_KEY topics.
32
141
* @ingroup COMMON_MODULE
33
142
*/
34
143
struct RTPS_DllAPI InstanceHandle_t
35
144
{
36
145
// !Value
37
- octet value[16 ];
38
- InstanceHandle_t ()
39
- {
40
- for (uint8_t i = 0 ; i < 16 ; i++)
41
- {
42
- value[i] = 0 ;
43
- }
44
- }
146
+ InstanceHandleValue_t value;
147
+
148
+ InstanceHandle_t () noexcept = default ;
45
149
46
150
InstanceHandle_t (
47
- const InstanceHandle_t& ihandle)
48
- {
49
- for (uint8_t i = 0 ; i < 16 ; i++)
50
- {
51
- value[i] = ihandle.value [i];
52
- }
53
- }
151
+ const InstanceHandle_t& ihandle) noexcept = default ;
54
152
55
153
InstanceHandle_t (
56
- const GUID_t& guid)
154
+ const GUID_t& guid) noexcept
57
155
{
58
- for (uint8_t i = 0 ; i < 16 ; ++i)
59
- {
60
- if (i < 12 )
61
- {
62
- value[i] = guid.guidPrefix .value [i];
63
- }
64
- else
65
- {
66
- value[i] = guid.entityId .value [i - 12 ];
67
- }
68
- }
156
+ *this = guid;
69
157
}
70
158
71
159
/* *
72
160
* Assignment operator
73
161
* @param ihandle Instance handle to copy the data from
74
162
*/
75
163
InstanceHandle_t& operator =(
76
- const InstanceHandle_t& ihandle)
77
- {
78
-
79
- for (uint8_t i = 0 ; i < 16 ; i++)
80
- {
81
- value[i] = ihandle.value [i];
82
- }
83
- return *this ;
84
- }
164
+ const InstanceHandle_t& ihandle) noexcept = default ;
85
165
86
166
/* *
87
167
* Assignment operator
88
168
* @param guid GUID to copy the data from
89
169
*/
90
170
InstanceHandle_t& operator =(
91
- const GUID_t& guid)
171
+ const GUID_t& guid) noexcept
92
172
{
93
- for (uint8_t i = 0 ; i < 16 ; i++)
94
- {
95
- if (i < 12 )
96
- {
97
- value[i] = guid.guidPrefix .value [i];
98
- }
99
- else
100
- {
101
- value[i] = guid.entityId .value [i - 12 ];
102
- }
103
- }
173
+ octet* dst = value;
174
+ memcpy (dst, guid.guidPrefix .value , 12 );
175
+ memcpy (&dst[12 ], guid.entityId .value , 4 );
104
176
return *this ;
105
177
}
106
178
107
179
/* *
108
180
* Know if the instance handle is defined
109
181
* @return True if the values are not zero.
110
182
*/
111
- bool isDefined () const
183
+ bool isDefined () const noexcept
112
184
{
113
- for (uint8_t i = 0 ; i < 16 ; ++i)
114
- {
115
- if (value[i] != 0 )
116
- {
117
- return true ;
118
- }
119
- }
120
- return false ;
185
+ return value.has_been_set ();
121
186
}
122
187
123
188
// TODO Review this conversion once InstanceHandle_t is implemented as DDS standard defines
124
- explicit operator const GUID_t&() const
189
+ explicit operator const GUID_t&() const noexcept
125
190
{
126
191
return *reinterpret_cast <const GUID_t*>(this );
127
192
}
@@ -140,21 +205,14 @@ const InstanceHandle_t c_InstanceHandle_Unknown;
140
205
*/
141
206
inline bool operator ==(
142
207
const InstanceHandle_t& ihandle1,
143
- const InstanceHandle_t& ihandle2)
208
+ const InstanceHandle_t& ihandle2) noexcept
144
209
{
145
- for (uint8_t i = 0 ; i < 16 ; ++i)
146
- {
147
- if (ihandle1.value [i] != ihandle2.value [i])
148
- {
149
- return false ;
150
- }
151
- }
152
- return true ;
210
+ return ihandle1.value == ihandle2.value ;
153
211
}
154
212
155
213
inline bool operator !=(
156
214
const InstanceHandle_t& ihandle1,
157
- const InstanceHandle_t& ihandle2)
215
+ const InstanceHandle_t& ihandle2) noexcept
158
216
{
159
217
return !(ihandle1 == ihandle2);
160
218
}
@@ -168,20 +226,11 @@ inline bool operator !=(
168
226
*/
169
227
inline void iHandle2GUID (
170
228
GUID_t& guid,
171
- const InstanceHandle_t& ihandle)
229
+ const InstanceHandle_t& ihandle) noexcept
172
230
{
173
- for (uint8_t i = 0 ; i < 16 ; ++i)
174
- {
175
- if (i < 12 )
176
- {
177
- guid.guidPrefix .value [i] = ihandle.value [i];
178
- }
179
- else
180
- {
181
- guid.entityId .value [i - 12 ] = ihandle.value [i];
182
- }
183
- }
184
- return ;
231
+ const octet* value = ihandle.value ;
232
+ memcpy (guid.guidPrefix .value , value, 12 );
233
+ memcpy (guid.entityId .value , &value[12 ], 4 );
185
234
}
186
235
187
236
/* *
@@ -190,28 +239,18 @@ inline void iHandle2GUID(
190
239
* @return GUID_t
191
240
*/
192
241
inline GUID_t iHandle2GUID (
193
- const InstanceHandle_t& ihandle)
242
+ const InstanceHandle_t& ihandle) noexcept
194
243
{
195
244
GUID_t guid;
196
- for (uint8_t i = 0 ; i < 16 ; ++i)
197
- {
198
- if (i < 12 )
199
- {
200
- guid.guidPrefix .value [i] = ihandle.value [i];
201
- }
202
- else
203
- {
204
- guid.entityId .value [i - 12 ] = ihandle.value [i];
205
- }
206
- }
245
+ iHandle2GUID (guid, ihandle);
207
246
return guid;
208
247
}
209
248
210
249
inline bool operator <(
211
250
const InstanceHandle_t& h1,
212
- const InstanceHandle_t& h2)
251
+ const InstanceHandle_t& h2) noexcept
213
252
{
214
- return memcmp ( h1.value , h2.value , 16 ) < 0 ;
253
+ return h1.value < h2.value ;
215
254
}
216
255
217
256
#ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
0 commit comments