Guid.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. * @file Guid.h
  16. */
  17. #ifndef _FASTDDS_RTPS_RTPS_GUID_H_
  18. #define _FASTDDS_RTPS_RTPS_GUID_H_
  19. #include <fastrtps/fastrtps_dll.h>
  20. #include <fastdds/rtps/common/Types.h>
  21. #include <fastdds/rtps/common/GuidPrefix_t.hpp>
  22. #include <fastdds/rtps/common/EntityId_t.hpp>
  23. #include <cstdint>
  24. #include <cstring>
  25. #include <sstream>
  26. namespace eprosima {
  27. namespace fastrtps {
  28. namespace rtps {
  29. struct InstanceHandle_t;
  30. //!@brief Structure GUID_t, entity identifier, unique in DDS-RTPS Domain.
  31. //!@ingroup COMMON_MODULE
  32. struct RTPS_DllAPI GUID_t
  33. {
  34. //!Guid prefix
  35. GuidPrefix_t guidPrefix;
  36. //!Entity id
  37. EntityId_t entityId;
  38. /*!
  39. * Default constructor. Contructs an unknown GUID.
  40. */
  41. GUID_t() noexcept
  42. {
  43. }
  44. /**
  45. * Construct
  46. * @param guid_prefix Guid prefix
  47. * @param id Entity id
  48. */
  49. GUID_t(
  50. const GuidPrefix_t& guid_prefix,
  51. uint32_t id) noexcept
  52. : guidPrefix(guid_prefix)
  53. , entityId(id)
  54. {
  55. }
  56. /**
  57. * @param guid_prefix Guid prefix
  58. * @param entity_id Entity id
  59. */
  60. GUID_t(
  61. const GuidPrefix_t& guid_prefix,
  62. const EntityId_t& entity_id) noexcept
  63. : guidPrefix(guid_prefix)
  64. , entityId(entity_id)
  65. {
  66. }
  67. /**
  68. * Checks whether this guid is for an entity on the same host as another guid.
  69. *
  70. * @param other_guid GUID_t to compare to.
  71. *
  72. * @return true when this guid is on the same host, false otherwise.
  73. */
  74. bool is_on_same_host_as(
  75. const GUID_t& other_guid) const
  76. {
  77. return memcmp(guidPrefix.value, other_guid.guidPrefix.value, 4) == 0;
  78. }
  79. /**
  80. * Checks whether this guid is for an entity on the same host and process as another guid.
  81. *
  82. * @param other_guid GUID_t to compare to.
  83. *
  84. * @return true when this guid is on the same host and process, false otherwise.
  85. */
  86. bool is_on_same_process_as(
  87. const GUID_t& other_guid) const
  88. {
  89. return memcmp(guidPrefix.value, other_guid.guidPrefix.value, 8) == 0;
  90. }
  91. /**
  92. * Checks whether this guid corresponds to a builtin entity.
  93. *
  94. * @return true when this guid corresponds to a builtin entity, false otherwise.
  95. */
  96. bool is_builtin() const
  97. {
  98. return entityId.value[3] >= 0xC0;
  99. }
  100. static GUID_t unknown() noexcept
  101. {
  102. return GUID_t();
  103. };
  104. // TODO Review this conversion once InstanceHandle_t is implemented as DDS standard defines
  105. explicit operator const InstanceHandle_t&() const
  106. {
  107. return *reinterpret_cast<const InstanceHandle_t*>(this);
  108. }
  109. };
  110. #ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
  111. /**
  112. * GUID comparison operator
  113. * @param g1 First GUID to compare
  114. * @param g2 Second GUID to compare
  115. * @return True if equal
  116. */
  117. inline bool operator ==(
  118. const GUID_t& g1,
  119. const GUID_t& g2)
  120. {
  121. if (g1.guidPrefix == g2.guidPrefix && g1.entityId == g2.entityId)
  122. {
  123. return true;
  124. }
  125. else
  126. {
  127. return false;
  128. }
  129. }
  130. /**
  131. * GUID comparison operator
  132. * @param g1 First GUID to compare
  133. * @param g2 Second GUID to compare
  134. * @return True if not equal
  135. */
  136. inline bool operator !=(
  137. const GUID_t& g1,
  138. const GUID_t& g2)
  139. {
  140. if (g1.guidPrefix != g2.guidPrefix || g1.entityId != g2.entityId)
  141. {
  142. return true;
  143. }
  144. else
  145. {
  146. return false;
  147. }
  148. }
  149. inline bool operator <(
  150. const GUID_t& g1,
  151. const GUID_t& g2)
  152. {
  153. for (uint8_t i = 0; i < 12; ++i)
  154. {
  155. if (g1.guidPrefix.value[i] < g2.guidPrefix.value[i])
  156. {
  157. return true;
  158. }
  159. else if (g1.guidPrefix.value[i] > g2.guidPrefix.value[i])
  160. {
  161. return false;
  162. }
  163. }
  164. for (uint8_t i = 0; i < 4; ++i)
  165. {
  166. if (g1.entityId.value[i] < g2.entityId.value[i])
  167. {
  168. return true;
  169. }
  170. else if (g1.entityId.value[i] > g2.entityId.value[i])
  171. {
  172. return false;
  173. }
  174. }
  175. return false;
  176. }
  177. #endif
  178. const GUID_t c_Guid_Unknown;
  179. #ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
  180. /**
  181. * Stream operator, prints a GUID.
  182. * @param output Output stream.
  183. * @param guid GUID_t to print.
  184. * @return Stream operator.
  185. */
  186. inline std::ostream& operator <<(
  187. std::ostream& output,
  188. const GUID_t& guid)
  189. {
  190. if (guid !=c_Guid_Unknown)
  191. {
  192. output << guid.guidPrefix << "|" << guid.entityId;
  193. }
  194. else
  195. {
  196. output << "|GUID UNKNOWN|";
  197. }
  198. return output;
  199. }
  200. /**
  201. * Stream operator, retrieves a GUID.
  202. * @param input Input stream.
  203. * @param guid GUID_t to print.
  204. * @return Stream operator.
  205. */
  206. inline std::istream& operator >>(
  207. std::istream& input,
  208. GUID_t& guid)
  209. {
  210. std::istream::sentry s(input);
  211. if (s)
  212. {
  213. std::ios_base::iostate excp_mask = input.exceptions();
  214. try
  215. {
  216. input.exceptions(excp_mask | std::ios_base::failbit | std::ios_base::badbit);
  217. input >> guid.guidPrefix;
  218. input >> guid.entityId;
  219. }
  220. catch (std::ios_base::failure&)
  221. {
  222. // maybe is unknown or just invalid
  223. guid = c_Guid_Unknown;
  224. }
  225. input.exceptions(excp_mask);
  226. }
  227. return input;
  228. }
  229. #endif
  230. } // namespace rtps
  231. } // namespace fastrtps
  232. } // namespace eprosima
  233. #endif /* _FASTDDS_RTPS_RTPS_GUID_H_ */