GuidPrefix_t.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright 2016-2019 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 GuidPrefix_t.hpp
  16. */
  17. #ifndef _FASTDDS_RTPS_COMMON_GUIDPREFIX_T_HPP_
  18. #define _FASTDDS_RTPS_COMMON_GUIDPREFIX_T_HPP_
  19. #include <fastrtps/fastrtps_dll.h>
  20. #include <fastdds/rtps/common/Types.h>
  21. #include <cstdint>
  22. #include <cstring>
  23. #include <sstream>
  24. namespace eprosima {
  25. namespace fastrtps {
  26. namespace rtps {
  27. //!@brief Structure GuidPrefix_t, Guid Prefix of GUID_t.
  28. //!@ingroup COMMON_MODULE
  29. struct RTPS_DllAPI GuidPrefix_t
  30. {
  31. static constexpr unsigned int size = 12;
  32. octet value[size];
  33. //!Default constructor. Set the Guid prefix to 0.
  34. GuidPrefix_t()
  35. {
  36. memset(value, 0, size);
  37. }
  38. static GuidPrefix_t unknown()
  39. {
  40. return GuidPrefix_t();
  41. }
  42. #ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
  43. /**
  44. * Guid prefix comparison operator
  45. * @param prefix guid prefix to compare
  46. * @return True if the guid prefixes are equal
  47. */
  48. bool operator==(
  49. const GuidPrefix_t& prefix) const
  50. {
  51. return (memcmp(value, prefix.value, size) == 0);
  52. }
  53. /**
  54. * Guid prefix comparison operator
  55. * @param prefix Second guid prefix to compare
  56. * @return True if the guid prefixes are not equal
  57. */
  58. bool operator!=(
  59. const GuidPrefix_t& prefix) const
  60. {
  61. return (memcmp(value, prefix.value, size) != 0);
  62. }
  63. #endif
  64. };
  65. const GuidPrefix_t c_GuidPrefix_Unknown;
  66. inline std::ostream& operator <<(
  67. std::ostream& output,
  68. const GuidPrefix_t& guiP)
  69. {
  70. output << std::hex;
  71. for (uint8_t i = 0; i < 11; ++i)
  72. {
  73. output << (int)guiP.value[i] << ".";
  74. }
  75. output << (int)guiP.value[11];
  76. return output << std::dec;
  77. }
  78. inline std::istream& operator >>(
  79. std::istream& input,
  80. GuidPrefix_t& guiP)
  81. {
  82. std::istream::sentry s(input);
  83. if (s)
  84. {
  85. char point;
  86. unsigned short hex;
  87. std::ios_base::iostate excp_mask = input.exceptions();
  88. try
  89. {
  90. input.exceptions(excp_mask | std::ios_base::failbit | std::ios_base::badbit);
  91. input >> std::hex >> hex;
  92. if (hex > 255)
  93. {
  94. input.setstate(std::ios_base::failbit);
  95. }
  96. guiP.value[0] = static_cast<octet>(hex);
  97. for (int i = 1; i < 12; ++i)
  98. {
  99. input >> point >> hex;
  100. if ( point != '.' || hex > 255 )
  101. {
  102. input.setstate(std::ios_base::failbit);
  103. }
  104. guiP.value[i] = static_cast<octet>(hex);
  105. }
  106. input >> std::dec;
  107. }
  108. catch (std::ios_base::failure& ){}
  109. input.exceptions(excp_mask);
  110. }
  111. return input;
  112. }
  113. } // namespace rtps
  114. } // namespace fastrtps
  115. } // namespace eprosima
  116. #endif /* _FASTDDS_RTPS_COMMON_GUIDPREFIX_T_HPP_ */