rmw_fastrtps_shared_cpp  master
Code shared on static and dynamic type support of rmw_fastrtps_cpp.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
guid_utils.hpp
Go to the documentation of this file.
1 // Copyright 2020 Open Source Robotics Foundation, Inc.
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 #ifndef RMW_FASTRTPS_SHARED_CPP__GUID_UTILS_HPP_
16 #define RMW_FASTRTPS_SHARED_CPP__GUID_UTILS_HPP_
17 
18 #include <cassert>
19 #include <cstddef>
20 #include <cstring>
21 #include <type_traits>
22 
23 #include "fastrtps/rtps/common/Guid.h"
24 
26 {
27 
28 template<typename ByteT>
29 void
31  const ByteT * guid_byte_array,
32  eprosima::fastrtps::rtps::GUID_t * guid)
33 {
34  static_assert(
36  "ByteT should be either int8_t or uint8_t");
37  assert(guid_byte_array);
38  assert(guid);
39  constexpr auto prefix_size = sizeof(guid->guidPrefix.value);
40  memcpy(guid->guidPrefix.value, guid_byte_array, prefix_size);
41  memcpy(guid->entityId.value, &guid_byte_array[prefix_size], guid->entityId.size);
42 }
43 
44 template<typename ByteT>
45 void
47  const eprosima::fastrtps::rtps::GUID_t & guid,
48  ByteT * guid_byte_array)
49 {
50  static_assert(
52  "ByteT should be either int8_t or uint8_t");
53  assert(guid_byte_array);
54  constexpr auto prefix_size = sizeof(guid.guidPrefix.value);
55  memcpy(guid_byte_array, &guid.guidPrefix, prefix_size);
56  memcpy(&guid_byte_array[prefix_size], &guid.entityId, guid.entityId.size);
57 }
58 
60 {
61  std::size_t operator()(const eprosima::fastrtps::rtps::GUID_t & guid) const
62  {
63  union u_convert {
64  uint8_t plain_value[sizeof(guid)];
65  uint32_t plain_ints[sizeof(guid) / sizeof(uint32_t)];
66  } u;
67 
68  static_assert(
69  sizeof(guid) == 16 &&
70  sizeof(u.plain_value) == sizeof(u.plain_ints) &&
71  offsetof(u_convert, plain_value) == offsetof(u_convert, plain_ints),
72  "Plain guid should be easily convertible to uint32_t[4]");
73 
74  copy_from_fastrtps_guid_to_byte_array(guid, u.plain_value);
75 
76  constexpr std::size_t prime_1 = 7;
77  constexpr std::size_t prime_2 = 31;
78  constexpr std::size_t prime_3 = 59;
79 
80  size_t ret_val = prime_1 * u.plain_ints[0];
81  ret_val = prime_2 * (u.plain_ints[1] + ret_val);
82  ret_val = prime_3 * (u.plain_ints[2] + ret_val);
83  ret_val = u.plain_ints[3] + ret_val;
84 
85  return ret_val;
86  }
87 };
88 
89 } // namespace rmw_fastrtps_shared_cpp
90 
91 #endif // RMW_FASTRTPS_SHARED_CPP__GUID_UTILS_HPP_
std::is_same
rmw_fastrtps_shared_cpp
Definition: create_rmw_gid.hpp:24
rmw_fastrtps_shared_cpp::hash_fastrtps_guid
Definition: guid_utils.hpp:59
rmw_fastrtps_shared_cpp::copy_from_fastrtps_guid_to_byte_array
void copy_from_fastrtps_guid_to_byte_array(const eprosima::fastrtps::rtps::GUID_t &guid, ByteT *guid_byte_array)
Definition: guid_utils.hpp:46
rmw_fastrtps_shared_cpp::copy_from_byte_array_to_fastrtps_guid
void copy_from_byte_array_to_fastrtps_guid(const ByteT *guid_byte_array, eprosima::fastrtps::rtps::GUID_t *guid)
Definition: guid_utils.hpp:30
rmw_fastrtps_shared_cpp::hash_fastrtps_guid::operator()
std::size_t operator()(const eprosima::fastrtps::rtps::GUID_t &guid) const
Definition: guid_utils.hpp:61
std::size_t