InstanceHandle.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 InstanceHandle.h
  16. */
  17. #ifndef _FASTDDS_RTPS_INSTANCEHANDLE_H_
  18. #define _FASTDDS_RTPS_INSTANCEHANDLE_H_
  19. #include <fastrtps/fastrtps_dll.h>
  20. #include <fastdds/rtps/common/Types.h>
  21. #include <fastdds/rtps/common/Guid.h>
  22. namespace eprosima {
  23. namespace fastrtps {
  24. namespace rtps {
  25. /**
  26. * Struct InstanceHandle_t, used to contain the key for WITH_KEY topics.
  27. * @ingroup COMMON_MODULE
  28. */
  29. struct RTPS_DllAPI InstanceHandle_t
  30. {
  31. //!Value
  32. octet value[16];
  33. InstanceHandle_t()
  34. {
  35. for (uint8_t i = 0; i < 16; i++)
  36. {
  37. value[i] = 0;
  38. }
  39. }
  40. InstanceHandle_t(
  41. const InstanceHandle_t& ihandle)
  42. {
  43. for (uint8_t i = 0; i < 16; i++)
  44. {
  45. value[i] = ihandle.value[i];
  46. }
  47. }
  48. InstanceHandle_t(
  49. const GUID_t& guid)
  50. {
  51. for (uint8_t i = 0; i < 16; ++i)
  52. {
  53. if (i < 12)
  54. {
  55. value[i] = guid.guidPrefix.value[i];
  56. }
  57. else
  58. {
  59. value[i] = guid.entityId.value[i - 12];
  60. }
  61. }
  62. }
  63. /**
  64. * Assingment operator
  65. * @param ihandle Instance handle to copy the data from
  66. */
  67. InstanceHandle_t& operator =(
  68. const InstanceHandle_t& ihandle)
  69. {
  70. for (uint8_t i = 0; i < 16; i++)
  71. {
  72. value[i] = ihandle.value[i];
  73. }
  74. return *this;
  75. }
  76. /**
  77. * Assingment operator
  78. * @param guid GUID to copy the data from
  79. */
  80. InstanceHandle_t& operator =(
  81. const GUID_t& guid)
  82. {
  83. for (uint8_t i = 0; i < 16; i++)
  84. {
  85. if (i < 12)
  86. {
  87. value[i] = guid.guidPrefix.value[i];
  88. }
  89. else
  90. {
  91. value[i] = guid.entityId.value[i - 12];
  92. }
  93. }
  94. return *this;
  95. }
  96. /**
  97. * Know if the instance handle is defined
  98. * @return True if the values are not zero.
  99. */
  100. bool isDefined() const
  101. {
  102. for (uint8_t i = 0; i < 16; ++i)
  103. {
  104. if (value[i] != 0)
  105. {
  106. return true;
  107. }
  108. }
  109. return false;
  110. }
  111. // TODO Review this conversion once InstanceHandle_t is implemented as DDS standard defines
  112. explicit operator const GUID_t&() const
  113. {
  114. return *reinterpret_cast<const GUID_t*>(this);
  115. }
  116. };
  117. const InstanceHandle_t c_InstanceHandle_Unknown;
  118. #ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
  119. /**
  120. * Comparison operator
  121. * @param ihandle1 First InstanceHandle_t to compare
  122. * @param ihandle2 Second InstanceHandle_t to compare
  123. * @return True if equal
  124. */
  125. inline bool operator ==(
  126. const InstanceHandle_t& ihandle1,
  127. const InstanceHandle_t& ihandle2)
  128. {
  129. for (uint8_t i = 0; i < 16; ++i)
  130. {
  131. if (ihandle1.value[i] != ihandle2.value[i])
  132. {
  133. return false;
  134. }
  135. }
  136. return true;
  137. }
  138. inline bool operator !=(
  139. const InstanceHandle_t& ihandle1,
  140. const InstanceHandle_t& ihandle2)
  141. {
  142. return !(ihandle1 == ihandle2);
  143. }
  144. #endif
  145. /**
  146. * Convert InstanceHandle_t to GUID
  147. * @param guid GUID to store the results
  148. * @param ihandle InstanceHandle_t to copy
  149. */
  150. inline void iHandle2GUID(
  151. GUID_t& guid,
  152. const InstanceHandle_t& ihandle)
  153. {
  154. for (uint8_t i = 0; i < 16; ++i)
  155. {
  156. if (i < 12)
  157. {
  158. guid.guidPrefix.value[i] = ihandle.value[i];
  159. }
  160. else
  161. {
  162. guid.entityId.value[i - 12] = ihandle.value[i];
  163. }
  164. }
  165. return;
  166. }
  167. /**
  168. * Convert GUID to InstanceHandle_t
  169. * @param ihandle InstanceHandle_t to store the results
  170. * @return GUID_t
  171. */
  172. inline GUID_t iHandle2GUID(
  173. const InstanceHandle_t& ihandle)
  174. {
  175. GUID_t guid;
  176. for (uint8_t i = 0; i < 16; ++i)
  177. {
  178. if (i < 12)
  179. {
  180. guid.guidPrefix.value[i] = ihandle.value[i];
  181. }
  182. else
  183. {
  184. guid.entityId.value[i - 12] = ihandle.value[i];
  185. }
  186. }
  187. return guid;
  188. }
  189. inline bool operator <(
  190. const InstanceHandle_t& h1,
  191. const InstanceHandle_t& h2)
  192. {
  193. return memcmp(h1.value, h2.value, 16) < 0;
  194. }
  195. #ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
  196. /**
  197. *
  198. * @param output
  199. * @param iHandle
  200. */
  201. inline std::ostream& operator <<(
  202. std::ostream& output,
  203. const InstanceHandle_t& iHandle)
  204. {
  205. output << std::hex;
  206. for (uint8_t i = 0; i < 15; ++i)
  207. {
  208. output << (int)iHandle.value[i] << ".";
  209. }
  210. output << (int)iHandle.value[15] << std::dec;
  211. return output;
  212. }
  213. #endif
  214. }
  215. }
  216. }
  217. #endif /* _FASTDDS_RTPS_INSTANCEHANDLE_H_ */