Fast DDS  Version 3.0.0
Fast DDS
Loading...
Searching...
No Matches
LocatorList.hpp
1// Copyright 2021 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
19#ifndef FASTDDS_RTPS_COMMON__LOCATORLIST_HPP
20#define FASTDDS_RTPS_COMMON__LOCATORLIST_HPP
21
22#include <fastdds/fastdds_dll.hpp>
23
24#include <fastdds/rtps/common/Locator.hpp>
25#include <fastdds/rtps/common/LocatorsIterator.hpp>
26#include <fastdds/utils/collections/ResourceLimitedVector.hpp>
27
28#include <vector>
29#include <cstdint>
30#include <cstring>
31#include <iomanip>
32#include <algorithm>
33
34namespace eprosima {
35namespace fastdds {
36namespace rtps {
37
39typedef std::vector<Locator>::iterator LocatorListIterator;
41typedef std::vector<Locator>::const_iterator LocatorListConstIterator;
42
47{
48public:
49
53 : it_(it)
54 {
55 }
56
59 const Locators& other)
60 : it_(other.it_)
61 {
62 }
63
65 {
66 ++it_;
67 return *this;
68 }
69
71 const LocatorsIterator& other) const
72 {
73 return it_ == static_cast<const Locators&>(other).it_;
74 }
75
77 const LocatorsIterator& other) const
78 {
79 return it_ != static_cast<const Locators&>(other).it_;
80 }
81
82 const Locator& operator *() const
83 {
84 return (*it_);
85 }
86
87private:
88
90};
91
97{
98public:
99
100 using value_type = typename std::vector<Locator>::value_type;
101
103 FASTDDS_EXPORTED_API LocatorList()
104 {
105 }
106
108 FASTDDS_EXPORTED_API ~LocatorList()
109 {
110 }
111
113 FASTDDS_EXPORTED_API LocatorList(
114 const LocatorList& list)
115 : m_locators(list.m_locators)
116 {
117 }
118
120 FASTDDS_EXPORTED_API LocatorList(
121 LocatorList&& list)
122 : m_locators(std::move(list.m_locators))
123 {
124 }
125
127 FASTDDS_EXPORTED_API LocatorList& operator =(
128 const LocatorList& list)
129 {
130 m_locators = list.m_locators;
131 return *this;
132 }
133
135 FASTDDS_EXPORTED_API LocatorList& operator =(
136 LocatorList&& list)
137 {
138 m_locators = std::move(list.m_locators);
139 return *this;
140 }
141
143 FASTDDS_EXPORTED_API bool operator ==(
144 const LocatorList& locator_list) const
145 {
146 if (locator_list.m_locators.size() == m_locators.size())
147 {
148 bool returnedValue = true;
149
150 for (auto it = locator_list.m_locators.begin(); returnedValue &&
151 it != locator_list.m_locators.end(); ++it)
152 {
153 returnedValue = false;
154
155 for (auto it2 = m_locators.begin(); !returnedValue && it2 != m_locators.end(); ++it2)
156 {
157 if (*it == *it2)
158 {
159 returnedValue = true;
160 }
161 }
162 }
163
164 return returnedValue;
165 }
166
167 return false;
168 }
169
171 FASTDDS_EXPORTED_API bool operator !=(
172 const LocatorList& locator_list) const
173 {
174 return !(*this == locator_list);
175 }
176
182 FASTDDS_EXPORTED_API LocatorListIterator begin()
183 {
184 return m_locators.begin();
185 }
186
192 FASTDDS_EXPORTED_API LocatorListIterator end()
193 {
194 return m_locators.end();
195 }
196
202 FASTDDS_EXPORTED_API LocatorListConstIterator begin() const
203 {
204 return m_locators.begin();
205 }
206
212 FASTDDS_EXPORTED_API LocatorListConstIterator end() const
213 {
214 return m_locators.end();
215 }
216
222 FASTDDS_EXPORTED_API size_t size() const
223 {
224 return m_locators.size();
225 }
226
233 FASTDDS_EXPORTED_API LocatorList& assign(
234 const LocatorList& list)
235 {
236 if (!(*this == list))
237 {
238 m_locators = list.m_locators;
239 }
240 return *this;
241 }
242
246 FASTDDS_EXPORTED_API void clear()
247 {
248 return m_locators.clear();
249 }
250
256 FASTDDS_EXPORTED_API void reserve(
257 size_t num)
258 {
259 return m_locators.reserve(num);
260 }
261
269 FASTDDS_EXPORTED_API void resize(
270 size_t num)
271 {
272 return m_locators.resize(num);
273 }
274
280 FASTDDS_EXPORTED_API void push_back(
281 const Locator& loc)
282 {
283 bool already = false;
284 for (LocatorListIterator it = this->begin(); it != this->end(); ++it)
285 {
286 if (loc == *it)
287 {
288 already = true;
289 break;
290 }
291 }
292 if (!already)
293 {
294 m_locators.push_back(loc);
295 }
296 }
297
303 FASTDDS_EXPORTED_API void push_back(
304 const LocatorList& locList)
305 {
306 for (auto it = locList.m_locators.begin(); it != locList.m_locators.end(); ++it)
307 {
308 this->push_back(*it);
309 }
310 }
311
317 FASTDDS_EXPORTED_API bool empty() const
318 {
319 return m_locators.empty();
320 }
321
327 FASTDDS_EXPORTED_API void erase(
328 const Locator& loc)
329 {
330 auto it = std::find(m_locators.begin(), m_locators.end(), loc);
331 if (it != m_locators.end())
332 {
333 m_locators.erase(it);
334 }
335 }
336
342 FASTDDS_EXPORTED_API bool isValid() const
343 {
344 for (LocatorListConstIterator it = this->begin(); it != this->end(); ++it)
345 {
346 if (!IsLocatorValid(*it))
347 {
348 return false;
349 }
350 }
351 return true;
352 }
353
359 FASTDDS_EXPORTED_API void swap(
360 LocatorList& locatorList)
361 {
362 this->m_locators.swap(locatorList.m_locators);
363 }
364
365 // Check if there are specific transport locators associated
366 // the template parameter is the locator kind (e.g. LOCATOR_KIND_UDPv4)
367 template<int kind> bool has_kind() const
368 {
369 for (auto& loc : m_locators)
370 {
371 if ( kind == loc.kind )
372 {
373 return true;
374 }
375 }
376
377 return false;
378 }
379
380 // Copy the inner locator list to a ResourceLimitedVector locator list.
381 FASTDDS_EXPORTED_API void copy_to(
383 {
384 for (auto& locator : m_locators)
385 {
386 locator_list.emplace_back(locator);
387 }
388 }
389
390private:
391
392 std::vector<Locator> m_locators;
393};
394
405inline std::ostream& operator <<(
406 std::ostream& output,
407 const LocatorList& locList)
408{
409 output << "[";
410 if (!locList.empty())
411 {
412 output << *(locList.begin());
413 for (auto it = locList.begin() + 1; it != locList.end(); ++it)
414 {
415 output << "," << *it;
416 }
417 }
418 else
419 {
420 output << "_";
421 }
422 output << "]";
423 return output;
424}
425
436inline std::istream& operator >>(
437 std::istream& input,
438 LocatorList& locList)
439{
440 std::istream::sentry s(input);
441 locList = LocatorList();
442
443 if (s)
444 {
445 char punct;
446 Locator loc;
447 std::ios_base::iostate excp_mask = input.exceptions();
448
449 try
450 {
451 input.exceptions(excp_mask | std::ios_base::failbit | std::ios_base::badbit);
452
453 // Get [
454 input >> punct;
455
456 while (punct != ']')
457 {
458 input >> loc >> punct;
459 if (loc.kind != LOCATOR_KIND_INVALID)
460 {
461 locList.push_back(loc);
462 }
463 }
464 }
465 catch (std::ios_base::failure& )
466 {
467 locList.clear();
468 EPROSIMA_LOG_WARNING(LOCATOR_LIST, "Error deserializing LocatorList");
469 }
470
471 input.exceptions(excp_mask);
472 }
473
474 return input;
475}
476
479
480} // namespace rtps
481} // namespace fastdds
482} // namespace eprosima
483
484#endif // FASTDDS_RTPS_COMMON__LOCATORLIST_HPP
Resource limited wrapper of std::vector.
Definition ResourceLimitedVector.hpp:59
pointer emplace_back(Args &&... args)
Construct and insert element at the end.
Definition ResourceLimitedVector.hpp:217
Class Locator_t, uniquely identifies a communication channel for a particular transport.
Definition Locator.hpp:71
int32_t kind
Specifies the locator type.
Definition Locator.hpp:87
Class LocatorList, a Locator vector that doesn't allow duplicates.
Definition LocatorList.hpp:97
FASTDDS_EXPORTED_API void clear()
Erase all locators from the container.
Definition LocatorList.hpp:246
FASTDDS_EXPORTED_API LocatorList & operator=(const LocatorList &list)
Copy assignment.
Definition LocatorList.hpp:127
FASTDDS_EXPORTED_API LocatorList(LocatorList &&list)
Move constructor.
Definition LocatorList.hpp:120
bool has_kind() const
Definition LocatorList.hpp:367
FASTDDS_EXPORTED_API LocatorList & assign(const LocatorList &list)
Replace the contents of the container.
Definition LocatorList.hpp:233
FASTDDS_EXPORTED_API void push_back(const LocatorList &locList)
Add several locators to the end if not already present within the list.
Definition LocatorList.hpp:303
FASTDDS_EXPORTED_API void swap(LocatorList &locatorList)
exchange the content of the container.
Definition LocatorList.hpp:359
FASTDDS_EXPORTED_API void copy_to(eprosima::fastdds::ResourceLimitedVector< Locator > &locator_list) const
Definition LocatorList.hpp:381
FASTDDS_EXPORTED_API LocatorListConstIterator end() const
Return a constant iterator to the end.
Definition LocatorList.hpp:212
FASTDDS_EXPORTED_API LocatorList(const LocatorList &list)
Copy constructor.
Definition LocatorList.hpp:113
FASTDDS_EXPORTED_API LocatorListConstIterator begin() const
Return a constant iterator to the beginning.
Definition LocatorList.hpp:202
FASTDDS_EXPORTED_API bool operator!=(const LocatorList &locator_list) const
Not equal to operator.
Definition LocatorList.hpp:171
FASTDDS_EXPORTED_API bool isValid() const
Check that every locator contained in the list is not LOCATOR_KIND_INVALID.
Definition LocatorList.hpp:342
FASTDDS_EXPORTED_API LocatorListIterator end()
Return an iterator to the end.
Definition LocatorList.hpp:192
FASTDDS_EXPORTED_API LocatorListIterator begin()
Return an iterator to the beginning.
Definition LocatorList.hpp:182
FASTDDS_EXPORTED_API size_t size() const
Return the number of locators.
Definition LocatorList.hpp:222
FASTDDS_EXPORTED_API ~LocatorList()
Destructor.
Definition LocatorList.hpp:108
FASTDDS_EXPORTED_API void erase(const Locator &loc)
Erase the specified locator from the container.
Definition LocatorList.hpp:327
FASTDDS_EXPORTED_API void push_back(const Locator &loc)
Add locator to the end if not found within the list.
Definition LocatorList.hpp:280
FASTDDS_EXPORTED_API LocatorList()
Constructor.
Definition LocatorList.hpp:103
FASTDDS_EXPORTED_API void reserve(size_t num)
Reserve storage increasing the capacity of the vector.
Definition LocatorList.hpp:256
FASTDDS_EXPORTED_API bool empty() const
Check that the container has no locators.
Definition LocatorList.hpp:317
typename std::vector< Locator >::value_type value_type
Definition LocatorList.hpp:100
FASTDDS_EXPORTED_API void resize(size_t num)
Resize the container to contain num locators.
Definition LocatorList.hpp:269
FASTDDS_EXPORTED_API bool operator==(const LocatorList &locator_list) const
Equal to operator.
Definition LocatorList.hpp:143
Adapter class that provides a LocatorsIterator interface from a LocatorListConstIterator.
Definition LocatorList.hpp:47
LocatorsIterator & operator++()
Increment operator.
Definition LocatorList.hpp:64
bool operator!=(const LocatorsIterator &other) const
Not equal to operator.
Definition LocatorList.hpp:76
Locators(const LocatorListConstIterator &it)
Constructor.
Definition LocatorList.hpp:51
const Locator & operator*() const
Dereference operator.
Definition LocatorList.hpp:82
bool operator==(const LocatorsIterator &other) const
Equal to operator.
Definition LocatorList.hpp:70
Locators(const Locators &other)
Copy constructor.
Definition LocatorList.hpp:58
std::istream & operator>>(std::istream &input, EntityId_t &enP)
Definition EntityId_t.hpp:289
std::ostream & operator<<(std::ostream &output, BuiltinTransports transports)
Definition BuiltinTransports.hpp:117
bool IsLocatorValid(const Locator_t &loc)
Auxiliary method to check that locator kind is not LOCATOR_KIND_INVALID (-1).
Definition Locator.hpp:233
std::vector< Locator_t >::const_iterator LocatorListConstIterator
Constant iterator to iterate over a vector of locators.
Definition Locator.hpp:513
std::vector< Locator_t >::iterator LocatorListIterator
Iterator to iterate over a vector of locators.
Definition Locator.hpp:512
eProsima namespace.
Definition EntityId_t.hpp:388
Provides a Locator's iterator interface that can be used by different Locator's containers.
Definition LocatorsIterator.hpp:33