ipaddress 1.1.0
Loading...
Searching...
No Matches
ip-any-iterator.hpp
Go to the documentation of this file.
1/**
2 * @file ip-any-iterator.hpp
3 * @brief Provides iterator classes for sequential unified traversal of IPv4 and IPv6
4 * @author Vladimir Shaleev
5 * @copyright MIT License
6 *
7 * This file defines the `ip_any_iterator` class template, which facilitates the
8 * unified traversal over both IPv4 and IPv6 address spaces. It abstracts the
9 * differences between the two IP versions, allowing users to write code that can
10 * operate on either IP version without modification. This is particularly useful
11 * for applications that need to be agnostic to the type of IP network they are
12 * working with, such as network utilities, security scanners, or dual-stack network
13 * implementations. The iterator provides random access capabilities, making it
14 * suitable for a wide range of algorithms that require iteration over IP addresses.
15 */
16
17#ifndef IPADDRESS_IP_ANY_ITERATOR_HPP
18#define IPADDRESS_IP_ANY_ITERATOR_HPP
19
22#include "ipv4-network.hpp"
23#include "ipv6-network.hpp"
24
25namespace IPADDRESS_NAMESPACE {
26/**
27 * An iterator for unified traversal over IPv4 and IPv6 address spaces.
28 *
29 * The `ip_any_iterator` class template provides a mechanism to iterate over
30 * both IPv4 and IPv6 addresses using a single, unified interface. It abstracts
31 * the complexities associated with handling the two different IP versions,
32 * allowing for code that is agnostic to the IP version it operates on. This
33 * is particularly useful for applications that must work with both IPv4 and
34 * IPv6 networks, such as dual-stack network configurations, without the need
35 * for separate code paths. The iterator supports random access, which enables
36 * efficient traversal and manipulation of IP address ranges in various network
37 * operations.
38 *
39 * @tparam T The type of IP address or network (IPv4 or IPv6) the iterator will handle.
40 * @tparam Ipv4Iter The type of IPv4 iterator
41 * @tparam Ipv6Iter The type of IPv6 iterator
42 */
43IPADDRESS_EXPORT template <typename T, typename Ipv4Iter, typename Ipv6Iter>
45public:
46 using iterator_category = std::random_access_iterator_tag; /**< Iterator category. */
47 using value_type = T; /**< Value type iterated over. */
48 using difference_type = int64_t; /**< Difference type between iterators. */
49 using pointer = const value_type*; /**< Pointer to value type. */
50 using reference = const value_type&; /**< Reference to value type. */
51
52 using uint_type = uint128_t; /**< Unsigned integer type for addressing. */
53
54 /**
55 * Default constructor.
56 */
58
59 /**
60 * Constructs an ip_any_iterator from an Ipv4Iter iterator.
61 *
62 * @param[in] iter The underlying iterator to reverse.
63 */
64 IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE ip_any_iterator(Ipv4Iter iter) IPADDRESS_NOEXCEPT : _iter(iter), _current(*iter) {
65 }
66
67 /**
68 * Constructs an ip_any_iterator from an Ipv6Iter iterator.
69 *
70 * @param[in] iter The underlying iterator to reverse.
71 */
72 IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE ip_any_iterator(Ipv6Iter iter) IPADDRESS_NOEXCEPT : _iter(iter), _version(ip_version::V6), _current(*iter) {
73 }
74
75 /**
76 * Calculates the difference in the number of elements between this and another ip_any_iterator.
77 *
78 * @param[in] other The ip_any_iterator to compare with.
79 * @return The number of elements between this and the other iterator.
80 * @remark This is a special function for calculate the difference between iterators,
81 * which can correctly represent all addresses using the integer number uint128_t
82 */
84 if (_version != other._version) {
85 return 0;
86 }
87 return _version == ip_version::V4 ? uint128_t(_iter.iter4.uint_diff(other._iter.iter4)) : _iter.iter6.uint_diff(other._iter.iter6);
88 }
89
90 /**
91 * Returns a reference to the current element.
92 *
93 * @return A reference to the element pointed to by the iterator.
94 */
96 return _current;
97 }
98
99 /**
100 * Returns a pointer to the current element.
101 *
102 * @return A pointer to the element pointed to by the iterator.
103 */
105 return &_current;
106 }
107
108 /**
109 * Accesses an element by index.
110 *
111 * @param[in] n The index of the element.
112 * @return The element at the specified index.
113 */
114 IPADDRESS_NODISCARD IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE value_type operator[](difference_type n) const IPADDRESS_NOEXCEPT {
115 const auto& it = *this;
116 return it[uint128_t(n)];
117 }
118
119 /**
120 * Accesses an element by index.
121 *
122 * @param[in] n The index of the element.
123 * @return The element at the specified index.
124 */
125 IPADDRESS_NODISCARD IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE value_type operator[](const uint128_t& n) const IPADDRESS_NOEXCEPT {
126 return _version == ip_version::V4 ? value_type(_iter.iter4[uint32_t(n)]) : value_type(_iter.iter6[n]);
127 }
128
129 /**
130 * Pre-increment operator.
131 *
132 * @return A reference to the incremented iterator.
133 */
135 add(1);
136 return *this;
137 }
138
139 /**
140 * Post-increment operator.
141 *
142 * @return The iterator before incrementing.
143 */
145 auto tmp = *this;
146 ++(*this);
147 return tmp;
148 }
149
150 /**
151 * Pre-decrement operator.
152 *
153 * @return A reference to the decremented iterator.
154 */
156 sub(1);
157 return *this;
158 }
159
160 /**
161 * Post-decrement operator.
162 *
163 * @return The iterator before decrementing.
164 */
166 auto tmp = *this;
167 --(*this);
168 return tmp;
169 }
170
171 /**
172 * Addition assignment operator.
173 *
174 * Moves the iterator forward by \a n positions.
175 *
176 * @param[in] n The number of positions to move the iterator forward.
177 * @return A reference to the updated iterator.
178 */
179 IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE ip_any_iterator& operator+=(difference_type n) IPADDRESS_NOEXCEPT {
180 add(n);
181 return *this;
182 }
183
184 /**
185 * Addition assignment operator.
186 *
187 * Moves the iterator forward by \a n positions.
188 *
189 * @param[in] n The number of positions to move the iterator forward.
190 * @return A reference to the updated iterator.
191 */
193 add(n);
194 return *this;
195 }
196
197 /**
198 * Subtraction assignment operator.
199 *
200 * Moves the iterator backward by \a n positions.
201 *
202 * @param[in] n The number of positions to move the iterator backward.
203 * @return A reference to the updated iterator.
204 */
205 IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE ip_any_iterator& operator-=(difference_type n) IPADDRESS_NOEXCEPT {
206 sub(n);
207 return *this;
208 }
209
210 /**
211 * Subtraction assignment operator.
212 *
213 * Moves the iterator backward by \a n positions.
214 *
215 * @param[in] n The number of positions to move the iterator backward.
216 * @return A reference to the updated iterator.
217 */
219 sub(n);
220 return *this;
221 }
222
223 /**
224 * Addition operator.
225 *
226 * Creates a new iterator that is \a n positions ahead of the current one.
227 *
228 * @param[in] n The number of positions to move ahead.
229 * @return A new iterator that is \a n positions ahead.
230 */
232 auto tmp = *this;
233 tmp += n;
234 return tmp;
235 }
236
237 /**
238 * Addition operator.
239 *
240 * Creates a new iterator that is \a n positions ahead of the current one.
241 *
242 * @param[in] n The number of positions to move ahead.
243 * @return A new iterator that is \a n positions ahead.
244 */
246 auto tmp = *this;
247 tmp += n;
248 return tmp;
249 }
250
251 /**
252 * Addition operator.
253 *
254 * Creates a new iterator that is \a n positions ahead of the specified iterator.
255 *
256 * @param[in] n The number of positions to move ahead.
257 * @param[in] it The iterator to move ahead from.
258 * @return A new iterator that is \a n positions ahead of it.
259 */
261 return it + n;
262 }
263
264 /**
265 * Addition operator.
266 *
267 * Creates a new iterator that is \a n positions ahead of the specified iterator.
268 *
269 * @param[in] n The number of positions to move ahead.
270 * @param[in] it The iterator to move ahead from.
271 * @return A new iterator that is \a n positions ahead of it.
272 */
274 return it + n;
275 }
276
277 /**
278 * Subtraction operator.
279 *
280 * Creates a new iterator that is \a n positions behind the current one.
281 *
282 * @param[in] n The number of positions to move behind.
283 * @return A new iterator that is \a n positions behind.
284 */
286 auto tmp = *this;
287 tmp -= n;
288 return tmp;
289 }
290
291 /**
292 * Subtraction operator.
293 *
294 * Creates a new iterator that is \a n positions behind the current one.
295 *
296 * @param[in] n The number of positions to move behind.
297 * @return A new iterator that is \a n positions behind.
298 */
300 auto tmp = *this;
301 tmp -= n;
302 return tmp;
303 }
304
305 /**
306 * Subtraction operator.
307 *
308 * Calculates the difference in the number of elements between this and another ip_any_iterator.
309 *
310 * @param[in] other The ip_any_iterator to compare with.
311 * @return The number of elements between this and the other iterator.
312 */
313 IPADDRESS_NODISCARD IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE difference_type operator-(const ip_any_iterator& other) const IPADDRESS_NOEXCEPT {
314 if (_version != other._version) {
315 return 0;
316 }
317 return _version == ip_version::V4 ? _iter.iter4 - other._iter.iter4 : _iter.iter6 - other._iter.iter6;
318 }
319
320 /**
321 * Equality operator.
322 *
323 * Compares two ip_any_iterator for equality.
324 *
325 * @param[in] other The ip_any_iterator to compare with.
326 * @return `true` if the iterators are equal, `false` otherwise.
327 */
329 if (_version != other._version) {
330 return false;
331 }
332 return _version == ip_version::V4 ? _iter.iter4 == other._iter.iter4 : _iter.iter6 == other._iter.iter6;
333 }
334
335 /**
336 * Inequality operator.
337 *
338 * Compares two ip_any_iterator for inequality.
339 *
340 * @param[in] other The ip_any_iterator to compare with.
341 * @return `true` if the iterators are not equal, `false` otherwise.
342 */
344 return !(*this == other);
345 }
346
347#ifdef IPADDRESS_HAS_SPACESHIP_OPERATOR
348
349 /**
350 * Three-way comparison operator (spaceship operator).
351 *
352 * Compares two ip_any_iterator for ordering.
353 *
354 * @param[in] other The ip_any_iterator to compare with.
355 * @return The result of the comparison as a std::strong_ordering value.
356 */
358 if (const auto result = _version <=> other._version; result == std::strong_ordering::equivalent) {
360 } else {
361 return result;
362 }
363 }
364
365#else // !IPADDRESS_HAS_SPACESHIP_OPERATOR
366
367 /**
368 * Less-than operator.
369 *
370 * Compares two ip_any_iterator to determine if the left one is less than the right one.
371 *
372 * @param[in] other The ip_any_iterator to compare with.
373 * @return `true` if the left iterator is less than the right iterator, `false` otherwise.
374 */
376 if (_version != other._version) {
377 return false;
378 }
379 return _version == ip_version::V4 ? _iter.iter4 < other._iter.iter4 : _iter.iter6 < other._iter.iter6;
380 }
381
382 /**
383 * Less-than-or-equal-to operator.
384 *
385 * Compares two ip_any_iterator to determine if the left one is less than or equal to the right one.
386 *
387 * @param[in] other The ip_any_iterator to compare with.
388 * @return `true` if the left iterator is less than or equal to the right iterator, `false` otherwise.
389 */
391 return !(other < *this);
392 }
393
394 /**
395 * Greater-than operator.
396 *
397 * Compares two ip_any_iterator to determine if the left one is greater than the right one.
398 *
399 * @param[in] other The ip_any_iterator to compare with.
400 * @return `true` if the left iterator is greater than the right iterator, `false` otherwise.
401 */
403 return other < *this;
404 }
405
406 /**
407 * Greater-than-or-equal-to operator.
408 *
409 * Compares two ip_any_iterator to determine if the left one is greater than or equal to the right one.
410 *
411 * @param[in] other The ip_any_iterator to compare with.
412 * @return `true` if the left iterator is greater than or equal to the right iterator, `false` otherwise.
413 */
415 return !(*this < other);
416 }
417
418#endif // !IPADDRESS_HAS_SPACESHIP_OPERATOR
419
420private:
422 return _version == ip_version::V4 ? ip_any_iterator(_iter.iter4.base()) : ip_any_iterator(_iter.iter6.base());
423 }
424
426 return _version == ip_version::V4 ? ip_any_iterator(_iter.iter4.reverse()) : ip_any_iterator(_iter.iter6.reverse());
427 }
428
429 IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE void add(const uint128_t& n) IPADDRESS_NOEXCEPT {
430 if (_version == ip_version::V4) {
431 _iter.iter4 += uint32_t(n);
432 _current = *_iter.iter4;
433 } else {
434 _iter.iter6 += n;
435 _current = *_iter.iter6;
436 }
437 }
438
439 IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE void sub(const uint128_t& n) IPADDRESS_NOEXCEPT {
440 if (_version == ip_version::V4) {
441 _iter.iter4 -= uint32_t(n);
442 _current = *_iter.iter4;
443 } else {
444 _iter.iter6 -= n;
445 _current = *_iter.iter6;
446 }
447 }
448
449 template <typename>
450 friend class ip_reverse_iterator;
451
452 union ip_any_iter {
453 IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE ip_any_iter() IPADDRESS_NOEXCEPT : iter4() {
454 }
455
456 IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE ip_any_iter(const Ipv4Iter& iter) IPADDRESS_NOEXCEPT : iter4(iter) {
457 }
458
459 IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE ip_any_iter(const Ipv6Iter& iter) IPADDRESS_NOEXCEPT : iter6(iter) {
460 }
461
462 Ipv4Iter iter4;
463 Ipv6Iter iter6;
464 } _iter {};
465 ip_version _version = ip_version::V4;
466 value_type _current {};
467}; // ip_any_iterator
468
469/**
470 * A sequence of host IP addresses.
471 *
472 * The subnets_any_sequence class template to provide a container-like interface for IP address sequences.
473 * It allows for iteration over the host addresses in a network or subnet, excluding the network and broadcast addresses when appropriate.
474 * This specialization is essential for network-related operations that require processing of individual host addresses within a given range.
475 *
476 * @tparam Base The base type from which the ip_address_base is derived, representing the underlying IP address type.
477 * @remark When iterating, obtaining addresses occurs through lazy calculations.
478 */
480public:
481 using value_type = ip_address; /**< The type of the IP addresses in the sequence. */
482 using size_type = size_t; /**< The type used for representing the size of the sequence. */
483 using difference_type = uint128_t; /**< The type used for representing differences between iterators. */
484 using pointer = value_type*; /**< The pointer type for the value_type. */
485 using const_pointer = const value_type*; /**< The const pointer type for the value_type. */
486 using reference = value_type&; /**< The reference type for the value_type. */
487 using const_reference = const value_type&; /**< The const reference type for the value_type. */
488
489 using iterator = ip_any_iterator<value_type, ip_address_iterator<ipv4_address>, ip_address_iterator<ipv6_address>>; /**< The iterator type for iterating over the sequence. */
490 using const_iterator = iterator; /**< The const iterator type for iterating over the sequence. */
491
492 using reverse_iterator = ip_reverse_iterator<iterator>; /**< The reverse iterator type for iterating over the sequence in reverse. */
493 using const_reverse_iterator = ip_reverse_iterator<const_iterator>; /**< The const reverse iterator type for iterating over the sequence in reverse. */
494
495 /**
496 * Constructs a hosts_any_sequence with specified network parameters.
497 *
498 * @param[in] begin The begin ipv4 address iterator.
499 * @param[in] end The end ipv4 address iterator.
500 */
501 IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE hosts_any_sequence(ip_address_iterator<ipv4_address> begin, ip_address_iterator<ipv4_address> end) IPADDRESS_NOEXCEPT : _begin(begin), _end(end) {
502 }
503
504 /**
505 * Constructs a hosts_any_sequence with specified network parameters.
506 *
507 * @param[in] begin The begin ipv6 address iterator.
508 * @param[in] end The end ipv6 address iterator.
509 */
510 IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE hosts_any_sequence(ip_address_iterator<ipv6_address> begin, ip_address_iterator<ipv6_address> end) IPADDRESS_NOEXCEPT : _begin(begin), _end(end) {
511 }
512
513 /**
514 * Gets the beginning iterator of the sequence.
515 *
516 * @return A const_iterator to the first element in the sequence.
517 */
518 IPADDRESS_NODISCARD IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE const_iterator begin() const IPADDRESS_NOEXCEPT {
519 return _begin;
520 }
521
522 /**
523 * Gets the end iterator of the sequence.
524 *
525 * @return A const_iterator to the element following the last element in the sequence.
526 */
527 IPADDRESS_NODISCARD IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE const_iterator end() const IPADDRESS_NOEXCEPT {
528 return _end;
529 }
530
531 /**
532 * Gets the beginning reverse iterator of the sequence.
533 *
534 * @return A const_reverse_iterator to the first element of the reversed sequence.
535 */
536 IPADDRESS_NODISCARD IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE const_reverse_iterator rbegin() const IPADDRESS_NOEXCEPT {
537 return const_reverse_iterator(end());
538 }
539
540 /**
541 * Gets the end reverse iterator of the sequence.
542 *
543 * @return A const_reverse_iterator to the element following the last element of the reversed sequence.
544 */
545 IPADDRESS_NODISCARD IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE const_reverse_iterator rend() const IPADDRESS_NOEXCEPT {
546 return const_reverse_iterator(begin());
547 }
548
549 /**
550 * Gets the beginning const iterator of the sequence.
551 *
552 * @return A const_iterator to the first element in the sequence.
553 */
554 IPADDRESS_NODISCARD IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE const_iterator cbegin() const IPADDRESS_NOEXCEPT {
555 return begin();
556 }
557
558 /**
559 * Gets the end const iterator of the sequence.
560 *
561 * @return A const_iterator to the element following the last element in the sequence.
562 */
563 IPADDRESS_NODISCARD IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE const_iterator cend() const IPADDRESS_NOEXCEPT {
564 return end();
565 }
566
567 /**
568 * Gets the beginning const reverse iterator of the sequence.
569 *
570 * @return A const_reverse_iterator to the first element of the reversed sequence.
571 */
572 IPADDRESS_NODISCARD IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE const_reverse_iterator crbegin() const IPADDRESS_NOEXCEPT {
573 return const_reverse_iterator(cend());
574 }
575
576 /**
577 * Gets the end const reverse iterator of the sequence.
578 *
579 * @return A const_reverse_iterator to the element following the last element of the reversed sequence.
580 */
581 IPADDRESS_NODISCARD IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE const_reverse_iterator crend() const IPADDRESS_NOEXCEPT {
582 return const_reverse_iterator(cbegin());
583 }
584
585 /**
586 * Checks if the sequence is empty.
587 *
588 * @return `true` if the sequence is empty, `false` otherwise.
589 */
591 return _begin == _end;
592 }
593
594 /**
595 * Gets the size of the sequence.
596 *
597 * @return The number of elements in the sequence.
598 */
599 IPADDRESS_NODISCARD IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE difference_type size() const IPADDRESS_NOEXCEPT {
600 return _end.uint_diff(_begin);
601 }
602
603 /**
604 * Accesses an element by index.
605 *
606 * @param[in] n The index of the element.
607 * @return The element at the specified index.
608 */
609 IPADDRESS_NODISCARD IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE value_type operator[](difference_type n) const IPADDRESS_NOEXCEPT {
610 return at(n);
611 }
612
613 /**
614 * Accesses an element by index with bounds checking.
615 *
616 * @param[in] n The index of the element.
617 * @return The element at the specified index.
618 */
619 IPADDRESS_NODISCARD IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE value_type at(difference_type n) const IPADDRESS_NOEXCEPT {
620 return *(_begin + n);
621 }
622
623 /**
624 * Accesses the first element in the sequence.
625 *
626 * @return A reference to the first element in the sequence.
627 */
629 return *_begin;
630 }
631
632 /**
633 * Accesses the last element in the sequence.
634 *
635 * @return A reference to the last element in the sequence.
636 */
637 IPADDRESS_NODISCARD IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE value_type back() const IPADDRESS_NOEXCEPT {
638 return *(_end - 1U);
639 }
640
641private:
642 const_iterator _begin{};
643 const_iterator _end{};
644}; // hosts_any_sequence
645
646/**
647 * A sequence container for subnet ranges within a network.
648 *
649 * This class template represents a sequence of subnets within a network.
650 * It provides forward and reverse iterators to traverse the subnets and
651 * offers insight into the structure of a network by breaking it down into
652 * smaller, manageable parts.
653 *
654 * @tparam T The type of IP network to be divided into subnets.
655 * @remark When iterating, obtaining networks occurs through lazy calculations.
656 */
657IPADDRESS_EXPORT template <typename T>
659public:
660 using value_type = T; /**< The type of subnet value. */
661 using size_type = size_t; /**< An unsigned integral type. */
662 using difference_type = uint128_t; /**< Unsigned integer type for differences. */
663 using pointer = value_type*; /**< Pointer to the subnet type. */
664 using const_pointer = const value_type*; /**< Const pointer to the subnet type. */
665 using reference = value_type&; /**< Reference to the subnet type. */
666 using const_reference = const value_type&; /**< Const reference to the subnet type. */
667
668 using iterator = ip_any_iterator<value_type, ip_network_iterator<ipv4_network>, ip_network_iterator<ipv6_network>>; /**< Forward iterator for subnet traversal. */
669 using const_iterator = iterator; /**< Const forward iterator for subnet traversal. */
670
671 using reverse_iterator = ip_reverse_iterator<iterator>; /**< Reverse iterator for subnet traversal. */
672 using const_reverse_iterator = ip_reverse_iterator<const_iterator>; /**< Const reverse iterator for subnet */
673
674 /**
675 * Constructs a subnets_any_sequence with specified network parameters.
676 *
677 * @param[in] begin The begin ipv4 network iterator.
678 * @param[in] end The end ipv4 network iterator.
679 */
681 }
682
683 /**
684 * Constructs a subnets_any_sequence with specified network parameters.
685 *
686 * @param[in] begin The begin ipv6 network iterator.
687 * @param[in] end The end ipv6 network iterator.
688 */
690 }
691
692 /**
693 * Gets the beginning iterator of the sequence.
694 *
695 * @return A const_iterator to the first element in the sequence.
696 */
697 IPADDRESS_NODISCARD IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE const_iterator begin() const IPADDRESS_NOEXCEPT {
698 return _begin;
699 }
700
701 /**
702 * Gets the end iterator of the sequence.
703 *
704 * @return A const_iterator to the element following the last element in the sequence.
705 */
706 IPADDRESS_NODISCARD IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE const_iterator end() const IPADDRESS_NOEXCEPT {
707 return _end;
708 }
709
710 /**
711 * Gets the beginning reverse iterator of the sequence.
712 *
713 * @return A const_reverse_iterator to the first element of the reversed sequence.
714 */
715 IPADDRESS_NODISCARD IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE const_reverse_iterator rbegin() const IPADDRESS_NOEXCEPT {
716 return const_reverse_iterator(end());
717 }
718
719 /**
720 * Gets the end reverse iterator of the sequence.
721 *
722 * @return A const_reverse_iterator to the element following the last element of the reversed sequence.
723 */
724 IPADDRESS_NODISCARD IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE const_reverse_iterator rend() const IPADDRESS_NOEXCEPT {
725 return const_reverse_iterator(begin());
726 }
727
728 /**
729 * Gets the beginning const iterator of the sequence.
730 *
731 * @return A const_iterator to the first element in the sequence.
732 */
733 IPADDRESS_NODISCARD IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE const_iterator cbegin() const IPADDRESS_NOEXCEPT {
734 return begin();
735 }
736
737 /**
738 * Gets the end const iterator of the sequence.
739 *
740 * @return A const_iterator to the element following the last element in the sequence.
741 */
742 IPADDRESS_NODISCARD IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE const_iterator cend() const IPADDRESS_NOEXCEPT {
743 return end();
744 }
745
746 /**
747 * Gets the beginning const reverse iterator of the sequence.
748 *
749 * @return A const_reverse_iterator to the first element of the reversed sequence.
750 */
751 IPADDRESS_NODISCARD IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE const_reverse_iterator crbegin() const IPADDRESS_NOEXCEPT {
752 return const_reverse_iterator(cend());
753 }
754
755 /**
756 * Gets the end const reverse iterator of the sequence.
757 *
758 * @return A const_reverse_iterator to the element following the last element of the reversed sequence.
759 */
760 IPADDRESS_NODISCARD IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE const_reverse_iterator crend() const IPADDRESS_NOEXCEPT {
761 return const_reverse_iterator(cbegin());
762 }
763
764 /**
765 * Checks if the sequence is empty.
766 *
767 * @return `true` if the sequence is empty, `false` otherwise.
768 */
770 return _begin == _end;
771 }
772
773 /**
774 * Gets the size of the sequence.
775 *
776 * @return The number of elements in the sequence.
777 */
778 IPADDRESS_NODISCARD IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE difference_type size() const IPADDRESS_NOEXCEPT {
779 return _end.uint_diff(_begin);
780 }
781
782 /**
783 * Accesses an element by index.
784 *
785 * @param[in] n The index of the element.
786 * @return The element at the specified index.
787 */
788 IPADDRESS_NODISCARD IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE value_type operator[](difference_type n) const IPADDRESS_NOEXCEPT {
789 return at(n);
790 }
791
792 /**
793 * Accesses an element by index with bounds checking.
794 *
795 * @param[in] n The index of the element.
796 * @return The element at the specified index.
797 */
798 IPADDRESS_NODISCARD IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE value_type at(difference_type n) const IPADDRESS_NOEXCEPT {
799 return *(_begin + n);
800 }
801
802 /**
803 * Accesses the first element in the sequence.
804 *
805 * @return A reference to the first element in the sequence.
806 */
808 return *_begin;
809 }
810
811 /**
812 * Accesses the last element in the sequence.
813 *
814 * @return A reference to the last element in the sequence.
815 */
816 IPADDRESS_NODISCARD IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE value_type back() const IPADDRESS_NOEXCEPT {
817 return *(_end - 1U);
818 }
819
820private:
821 const_iterator _begin{};
822 const_iterator _end{};
823}; // subnets_any_sequence
824
825} // namespace IPADDRESS_NAMESPACE
826
827#endif // IPADDRESS_IP_ANY_ITERATOR_HPP
A sequence of host IP addresses.
Definition ip-any-iterator.hpp:479
constexpr inline const_reverse_iterator crbegin() const noexcept
Gets the beginning const reverse iterator of the sequence.
Definition ip-any-iterator.hpp:572
constexpr inline value_type operator[](difference_type n) const noexcept
Accesses an element by index.
Definition ip-any-iterator.hpp:609
constexpr inline const_iterator begin() const noexcept
Gets the beginning iterator of the sequence.
Definition ip-any-iterator.hpp:518
constexpr inline bool empty() const noexcept
Checks if the sequence is empty.
Definition ip-any-iterator.hpp:590
constexpr inline value_type back() const noexcept
Accesses the last element in the sequence.
Definition ip-any-iterator.hpp:637
constexpr inline const_reverse_iterator crend() const noexcept
Gets the end const reverse iterator of the sequence.
Definition ip-any-iterator.hpp:581
constexpr inline value_type front() const noexcept
Accesses the first element in the sequence.
Definition ip-any-iterator.hpp:628
constexpr inline difference_type size() const noexcept
Gets the size of the sequence.
Definition ip-any-iterator.hpp:599
constexpr inline hosts_any_sequence(ip_address_iterator< ipv6_address > begin, ip_address_iterator< ipv6_address > end) noexcept
Constructs a hosts_any_sequence with specified network parameters.
Definition ip-any-iterator.hpp:510
constexpr inline const_reverse_iterator rend() const noexcept
Gets the end reverse iterator of the sequence.
Definition ip-any-iterator.hpp:545
constexpr inline const_iterator cend() const noexcept
Gets the end const iterator of the sequence.
Definition ip-any-iterator.hpp:563
constexpr inline const_iterator cbegin() const noexcept
Gets the beginning const iterator of the sequence.
Definition ip-any-iterator.hpp:554
constexpr inline const_iterator end() const noexcept
Gets the end iterator of the sequence.
Definition ip-any-iterator.hpp:527
constexpr inline value_type at(difference_type n) const noexcept
Accesses an element by index with bounds checking.
Definition ip-any-iterator.hpp:619
constexpr inline const_reverse_iterator rbegin() const noexcept
Gets the beginning reverse iterator of the sequence.
Definition ip-any-iterator.hpp:536
constexpr inline hosts_any_sequence(ip_address_iterator< ipv4_address > begin, ip_address_iterator< ipv4_address > end) noexcept
Constructs a hosts_any_sequence with specified network parameters.
Definition ip-any-iterator.hpp:501
A class that represents an IP address, supporting both IPv4 and IPv6 formats.
Definition ip-any-address.hpp:73
An iterator for unified traversal over IPv4 and IPv6 address spaces.
Definition ip-any-iterator.hpp:44
constexpr inline ip_any_iterator(Ipv6Iter iter) noexcept
Constructs an ip_any_iterator from an Ipv6Iter iterator.
Definition ip-any-iterator.hpp:72
constexpr inline ip_any_iterator() noexcept=default
Default constructor.
friend constexpr inline ip_any_iterator operator+(const uint128_t &n, const ip_any_iterator &it) noexcept
Addition operator.
Definition ip-any-iterator.hpp:273
constexpr inline bool operator<(const ip_any_iterator &other) const noexcept
Less-than operator.
Definition ip-any-iterator.hpp:375
constexpr inline value_type operator[](difference_type n) const noexcept
Accesses an element by index.
Definition ip-any-iterator.hpp:114
constexpr inline difference_type operator-(const ip_any_iterator &other) const noexcept
Subtraction operator.
Definition ip-any-iterator.hpp:313
constexpr inline ip_any_iterator & operator+=(const uint128_t &n) noexcept
Addition assignment operator.
Definition ip-any-iterator.hpp:192
constexpr inline ip_any_iterator operator+(difference_type n) const noexcept
Addition operator.
Definition ip-any-iterator.hpp:231
constexpr inline bool operator<=(const ip_any_iterator &other) const noexcept
Less-than-or-equal-to operator.
Definition ip-any-iterator.hpp:390
constexpr inline bool operator!=(const ip_any_iterator &other) const noexcept
Inequality operator.
Definition ip-any-iterator.hpp:343
constexpr inline ip_any_iterator & operator-=(const uint128_t &n) noexcept
Subtraction assignment operator.
Definition ip-any-iterator.hpp:218
constexpr inline ip_any_iterator operator-(difference_type n) const noexcept
Subtraction operator.
Definition ip-any-iterator.hpp:285
constexpr inline value_type operator[](const uint128_t &n) const noexcept
Accesses an element by index.
Definition ip-any-iterator.hpp:125
constexpr inline ip_any_iterator operator-(const uint128_t &n) const noexcept
Subtraction operator.
Definition ip-any-iterator.hpp:299
constexpr inline uint128_t uint_diff(const ip_any_iterator &other) const noexcept
Calculates the difference in the number of elements between this and another ip_any_iterator.
Definition ip-any-iterator.hpp:83
constexpr inline ip_any_iterator(Ipv4Iter iter) noexcept
Constructs an ip_any_iterator from an Ipv4Iter iterator.
Definition ip-any-iterator.hpp:64
constexpr inline bool operator>(const ip_any_iterator &other) const noexcept
Greater-than operator.
Definition ip-any-iterator.hpp:402
constexpr inline ip_any_iterator operator++(int) noexcept
Post-increment operator.
Definition ip-any-iterator.hpp:144
constexpr inline bool operator==(const ip_any_iterator &other) const noexcept
Equality operator.
Definition ip-any-iterator.hpp:328
constexpr inline bool operator>=(const ip_any_iterator &other) const noexcept
Greater-than-or-equal-to operator.
Definition ip-any-iterator.hpp:414
constexpr inline ip_any_iterator & operator--() noexcept
Pre-decrement operator.
Definition ip-any-iterator.hpp:155
friend constexpr inline ip_any_iterator operator+(difference_type n, const ip_any_iterator &it) noexcept
Addition operator.
Definition ip-any-iterator.hpp:260
constexpr inline pointer operator->() const noexcept
Returns a pointer to the current element.
Definition ip-any-iterator.hpp:104
constexpr inline ip_any_iterator operator+(const uint128_t &n) const noexcept
Addition operator.
Definition ip-any-iterator.hpp:245
constexpr inline ip_any_iterator & operator+=(difference_type n) noexcept
Addition assignment operator.
Definition ip-any-iterator.hpp:179
constexpr inline ip_any_iterator & operator++() noexcept
Pre-increment operator.
Definition ip-any-iterator.hpp:134
constexpr inline reference operator*() const noexcept
Returns a reference to the current element.
Definition ip-any-iterator.hpp:95
constexpr inline ip_any_iterator & operator-=(difference_type n) noexcept
Subtraction assignment operator.
Definition ip-any-iterator.hpp:205
constexpr inline ip_any_iterator operator--(int) noexcept
Post-decrement operator.
Definition ip-any-iterator.hpp:165
An iterator for traversing IP addresses within a network range.
Definition ip-network-iterator.hpp:33
A reverse iterator template class for IP addresses.
Definition ip-address-iterator.hpp:35
A sequence container for subnet ranges within a network.
Definition ip-any-iterator.hpp:658
constexpr inline const_reverse_iterator crbegin() const noexcept
Gets the beginning const reverse iterator of the sequence.
Definition ip-any-iterator.hpp:751
constexpr inline value_type operator[](difference_type n) const noexcept
Accesses an element by index.
Definition ip-any-iterator.hpp:788
constexpr inline const_iterator begin() const noexcept
Gets the beginning iterator of the sequence.
Definition ip-any-iterator.hpp:697
constexpr inline bool empty() const noexcept
Checks if the sequence is empty.
Definition ip-any-iterator.hpp:769
constexpr inline value_type back() const noexcept
Accesses the last element in the sequence.
Definition ip-any-iterator.hpp:816
constexpr inline const_reverse_iterator crend() const noexcept
Gets the end const reverse iterator of the sequence.
Definition ip-any-iterator.hpp:760
constexpr inline value_type front() const noexcept
Accesses the first element in the sequence.
Definition ip-any-iterator.hpp:807
constexpr inline difference_type size() const noexcept
Gets the size of the sequence.
Definition ip-any-iterator.hpp:778
constexpr inline subnets_any_sequence(ip_network_iterator< ipv4_network > begin, ip_network_iterator< ipv4_network > end) noexcept
Constructs a subnets_any_sequence with specified network parameters.
Definition ip-any-iterator.hpp:680
constexpr inline const_reverse_iterator rend() const noexcept
Gets the end reverse iterator of the sequence.
Definition ip-any-iterator.hpp:724
constexpr inline const_iterator cend() const noexcept
Gets the end const iterator of the sequence.
Definition ip-any-iterator.hpp:742
constexpr inline const_iterator cbegin() const noexcept
Gets the beginning const iterator of the sequence.
Definition ip-any-iterator.hpp:733
constexpr inline const_iterator end() const noexcept
Gets the end iterator of the sequence.
Definition ip-any-iterator.hpp:706
constexpr inline value_type at(difference_type n) const noexcept
Accesses an element by index with bounds checking.
Definition ip-any-iterator.hpp:798
constexpr inline const_reverse_iterator rbegin() const noexcept
Gets the beginning reverse iterator of the sequence.
Definition ip-any-iterator.hpp:715
constexpr inline subnets_any_sequence(ip_network_iterator< ipv6_network > begin, ip_network_iterator< ipv6_network > end) noexcept
Constructs a subnets_any_sequence with specified network parameters.
Definition ip-any-iterator.hpp:689
constexpr inline uint128_t(long lower) noexcept
Constructs a uint128_t instance from a signed integer.
Definition uint128.hpp:205
inline std::u8string to_u8string(format fmt=format::decimal) const
Converts the uint128_t value to a string representation.
Definition uint128.hpp:402
#define IPADDRESS_EXPORT
Definition config.hpp:42
#define IPADDRESS_NODISCARD
Definition config.hpp:98
#define IPADDRESS_FORCE_INLINE
Definition config.hpp:112
#define IPADDRESS_NAMESPACE
Definition config.hpp:38
#define IPADDRESS_NOEXCEPT
Definition config.hpp:89
ip_version
Enumerates the IP address versions.
Definition ip-address-base.hpp:29
@ V4
IPv4 version identifier.
@ V6
IPv6 version identifier.