RemoteLocators.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright 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 RemoteLocators.hpp
  16. */
  17. #ifndef _FASTDDS_RTPS_COMMON_REMOTELOCATORS_HPP_
  18. #define _FASTDDS_RTPS_COMMON_REMOTELOCATORS_HPP_
  19. #include <fastdds/rtps/common/Locator.h>
  20. #include <fastrtps/utils/collections/ResourceLimitedVector.hpp>
  21. namespace eprosima {
  22. namespace fastrtps {
  23. namespace rtps {
  24. /**
  25. * Holds information about the locators of a remote entity.
  26. */
  27. struct RemoteLocatorList
  28. {
  29. /**
  30. * Construct a RemoteLocatorList.
  31. *
  32. * @param max_unicast_locators Maximum number of unicast locators to hold.
  33. * @param max_multicast_locators Maximum number of multicast locators to hold.
  34. */
  35. RemoteLocatorList(
  36. size_t max_unicast_locators,
  37. size_t max_multicast_locators)
  38. : unicast(ResourceLimitedContainerConfig::fixed_size_configuration(max_unicast_locators))
  39. , multicast(ResourceLimitedContainerConfig::fixed_size_configuration(max_multicast_locators))
  40. {
  41. }
  42. /**
  43. * Copy-construct a RemoteLocatorList.
  44. *
  45. * @param other RemoteLocatorList to copy data from.
  46. */
  47. RemoteLocatorList(const RemoteLocatorList& other)
  48. : unicast(ResourceLimitedContainerConfig::fixed_size_configuration(other.unicast.max_size()))
  49. , multicast(ResourceLimitedContainerConfig::fixed_size_configuration(other.multicast.max_size()))
  50. {
  51. *this = other;
  52. }
  53. /**
  54. * Assign locator values from other RemoteLocatorList.
  55. *
  56. * @param other RemoteLocatorList to copy data from.
  57. *
  58. * @remarks Using the assignment operator is different from copy-constructing as in the first case the
  59. * configuration with the maximum number of locators is not copied. This means that, for two lists with
  60. * different maximum number of locators, the expression `(a = b) == b` may not be true.
  61. */
  62. RemoteLocatorList& operator = (const RemoteLocatorList& other)
  63. {
  64. unicast = other.unicast;
  65. multicast = other.multicast;
  66. return *this;
  67. }
  68. /**
  69. * Adds a locator to the unicast list.
  70. *
  71. * If the locator already exists in the unicast list, or the maximum number of unicast locators has been reached,
  72. * the new locator is silently discarded.
  73. *
  74. * @param locator Unicast locator to be added.
  75. */
  76. void add_unicast_locator(const Locator_t& locator)
  77. {
  78. for (const Locator_t& loc : unicast)
  79. {
  80. if (loc == locator)
  81. {
  82. return;
  83. }
  84. }
  85. unicast.push_back(locator);
  86. }
  87. /**
  88. * Adds a locator to the multicast list.
  89. *
  90. * If the locator already exists in the multicast list, or the maximum number of multicast locators has been reached,
  91. * the new locator is silently discarded.
  92. *
  93. * @param locator Multicast locator to be added.
  94. */
  95. void add_multicast_locator(const Locator_t& locator)
  96. {
  97. for (const Locator_t& loc : multicast)
  98. {
  99. if (loc == locator)
  100. {
  101. return;
  102. }
  103. }
  104. multicast.push_back(locator);
  105. }
  106. //! List of unicast locators
  107. ResourceLimitedVector<Locator_t> unicast;
  108. //! List of multicast locators
  109. ResourceLimitedVector<Locator_t> multicast;
  110. };
  111. inline std::ostream& operator<<(std::ostream& output, const RemoteLocatorList& remote_locators)
  112. {
  113. for (auto it = remote_locators.multicast.begin(); it != remote_locators.multicast.end(); ++it)
  114. {
  115. output << *it << ",";
  116. }
  117. for (auto it = remote_locators.unicast.begin(); it != remote_locators.unicast.end(); ++it)
  118. {
  119. output << *it << ",";
  120. }
  121. return output;
  122. }
  123. } /* namespace rtps */
  124. } /* namespace fastrtps */
  125. } /* namespace eprosima */
  126. #endif /* _FASTDDS_RTPS_COMMON_REMOTELOCATORS_HPP_ */