ip_iterator.hpp
Go to the documentation of this file.
1/**
2 * @author: Daniel Fuchs
3 * @contact: fuxeysolutions@gmail.com
4 *
5 * distributed under the MIT License (MIT).
6 * Copyright (c) Daniel Fuchs
7 *
8 */
9#pragma once
10
11#include <algorithm>
12
14{
15public:
17 {
18 public:
20 {
21 }
22
23 ipv4_Address(uint8_t p1, uint8_t p2, uint8_t p3, uint8_t p4) :
24 ipv4_part1(p1), ipv4_part2(p2), ipv4_part3(p3), ipv4_part4(p4)
25 {
26 convertU8toU32();
27 }
28
29 std::string
30 toString() const
31 {
32 return std::to_string(static_cast<int>(ipv4_part1)) + "." +
33 std::to_string(static_cast<int>(ipv4_part2)) + "." +
34 std::to_string(static_cast<int>(ipv4_part3)) + "." +
35 std::to_string(static_cast<int>(ipv4_part4));
36 }
37
38 bool
39 operator==(const ipv4_Address& rhs) const
40 {
41 return ipv4 == rhs.ipv4;
42 }
43
44 bool
45 operator>(const ipv4_Address& rhs) const
46 {
47 return ipv4 > rhs.ipv4;
48 }
49
50 bool
51 operator<(const ipv4_Address& rhs) const
52 {
53 return ipv4 < rhs.ipv4;
54 }
55
58 {
59 ipv4_Address newAdr(adr.ipv4_part1, adr.ipv4_part2, adr.ipv4_part3, adr.ipv4_part4);
60 return newAdr;
61 }
62
65 {
66 ipv4_Address i = *this;
67 ipv4 += 1;
68 convertU32toU8();
69 return i;
70 }
71
72 private:
73 uint8_t ipv4_part1;
74 uint8_t ipv4_part2;
75 uint8_t ipv4_part3;
76 uint8_t ipv4_part4;
77 uint32_t ipv4;
78
79 void
80 convertU32toU8()
81 {
82 ipv4_part1 = static_cast<uint8_t>(ipv4 >> 24);
83 ipv4_part2 = static_cast<uint8_t>(ipv4 >> 16);
84 ipv4_part3 = static_cast<uint8_t>(ipv4 >> 8);
85 ipv4_part4 = static_cast<uint8_t>(ipv4);
86 }
87
88 void
89 convertU8toU32()
90 {
91 ipv4 = (static_cast<uint32_t>(ipv4_part1) << 24) +
92 (static_cast<uint32_t>(ipv4_part2) << 16) +
93 (static_cast<uint32_t>(ipv4_part3) << 8) + static_cast<uint32_t>(ipv4_part4);
94 }
95 };
96
97 class iterator :
98 public std::iterator<std::input_iterator_tag,
99 ipv4_Address,
100 ipv4_Address,
101 const ipv4_Address*,
102 ipv4_Address>
103 {
104
105 ipv4_Address start;
106
107 public:
108 explicit iterator(ipv4_Address startAdr) : start(startAdr)
109 {
110 }
111
112 iterator&
114 {
115 start++;
116 return *this;
117 }
118
119 bool
120 operator!=(iterator other) const
121 {
122 return !(start == other.start);
123 }
124
125 bool
126 operator==(iterator other) const
127 {
128 return start == other.start;
129 }
130
131 reference
132 operator*() const
133 {
134 return start;
135 }
136 };
137
138 ipv4_Range(ipv4_Address start, ipv4_Address end) : startAddress(start), endAddress(end)
139 {
140 validate();
141 }
142
143 iterator
145 {
146 return iterator(startAddress);
147 }
148
149 iterator
151 {
152 return iterator(endAddress++);
153 }
154
155private:
156 void
157 validate()
158 {
159 if (startAddress == endAddress)
160 {
161 throw std::invalid_argument("start and end are equal");
162 }
163 if (startAddress > endAddress)
164 {
165 // ok
166 }
167 else
168 {
169 // switch
170 ipv4_Address temp = startAddress;
171 startAddress = endAddress;
172 endAddress = temp;
173 }
174 }
175
176 ipv4_Address startAddress;
177 ipv4_Address endAddress;
178};
bool operator>(const ipv4_Address &rhs) const
std::string toString() const
ipv4_Address(uint8_t p1, uint8_t p2, uint8_t p3, uint8_t p4)
bool operator<(const ipv4_Address &rhs) const
ipv4_Address operator++(int)
ipv4_Address operator=(ipv4_Address &adr)
bool operator==(const ipv4_Address &rhs) const
iterator(ipv4_Address startAdr)
reference operator*() const
bool operator!=(iterator other) const
iterator & operator++()
bool operator==(iterator other) const
iterator end()
ipv4_Range(ipv4_Address start, ipv4_Address end)
iterator begin()