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 
13 
14 class ipv4_Range {
15 public:
16  class ipv4_Address {
17  public:
18 
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  convertU8toU32();
26  }
27 
28  std::string toString() const {
29  return std::to_string(static_cast<int>(ipv4_part1)) + "." +
30  std::to_string(static_cast<int>(ipv4_part2)) + "." +
31  std::to_string(static_cast<int>(ipv4_part3)) + "." +
32  std::to_string(static_cast<int>(ipv4_part4));
33  }
34 
35  bool operator==(const ipv4_Address &rhs) const {
36  return ipv4 == rhs.ipv4;
37  }
38 
39  bool operator>(const ipv4_Address &rhs) const {
40  return ipv4 > rhs.ipv4;
41  }
42 
43  bool operator<(const ipv4_Address &rhs) const {
44  return ipv4 < rhs.ipv4;
45  }
46 
48  ipv4_Address newAdr(adr.ipv4_part1,
49  adr.ipv4_part2,
50  adr.ipv4_part3,
51  adr.ipv4_part4
52  );
53  return newAdr;
54  }
55 
57  ipv4_Address i = *this;
58  ipv4 += 1;
59  convertU32toU8();
60  return i;
61  }
62 
63  private:
64  uint8_t ipv4_part1;
65  uint8_t ipv4_part2;
66  uint8_t ipv4_part3;
67  uint8_t ipv4_part4;
68  uint32_t ipv4;
69 
70  void convertU32toU8() {
71  ipv4_part1 = static_cast<uint8_t>(ipv4 >> 24);
72  ipv4_part2 = static_cast<uint8_t>(ipv4 >> 16);
73  ipv4_part3 = static_cast<uint8_t>(ipv4 >> 8);
74  ipv4_part4 = static_cast<uint8_t>(ipv4);
75 
76  }
77 
78  void convertU8toU32() {
79  ipv4 = (static_cast<uint32_t>(ipv4_part1) << 24) +
80  (static_cast<uint32_t>(ipv4_part2) << 16) +
81  (static_cast<uint32_t>(ipv4_part3) << 8) +
82  static_cast<uint32_t>(ipv4_part4);
83  }
84 
85  };
86 
87 
88  class iterator : public std::iterator<
89  std::input_iterator_tag,
90  ipv4_Address,
91  ipv4_Address,
92  const ipv4_Address *,
93  ipv4_Address> {
94 
95  ipv4_Address start;
96  public:
97  explicit iterator(ipv4_Address startAdr) : start(startAdr) {}
98 
100  start++;
101  return *this;
102  }
103 
104  bool operator!=(iterator other) const {
105  return !(start == other.start);
106  }
107 
108  bool operator==(iterator other) const {
109  return start == other.start;
110  }
111 
112  reference operator*() const { return start; }
113  };
114 
115 
116  ipv4_Range(ipv4_Address start, ipv4_Address end) : startAddress(start), endAddress(end) {
117  validate();
118  }
119 
121  return iterator(startAddress);
122  }
123 
125  return iterator(endAddress++);
126  }
127 
128 private:
129  void validate() {
130  if (startAddress == endAddress) {
131  throw std::invalid_argument("start and end are equal");
132  }
133  if (startAddress > endAddress) {
134  // ok
135  } else {
136  // switch
137  ipv4_Address temp = startAddress;
138  startAddress = endAddress;
139  endAddress = temp;
140  }
141  }
142 
143 
144  ipv4_Address startAddress;
145  ipv4_Address endAddress;
146 
147 
148 };
ipv4_Range::iterator::operator!=
bool operator!=(iterator other) const
Definition: ip_iterator.hpp:104
ipv4_Range::iterator::operator++
iterator & operator++()
Definition: ip_iterator.hpp:99
ipv4_Range::ipv4_Address::ipv4_Address
ipv4_Address()
Definition: ip_iterator.hpp:19
ipv4_Range::ipv4_Range
ipv4_Range(ipv4_Address start, ipv4_Address end)
Definition: ip_iterator.hpp:116
ipv4_Range::begin
iterator begin()
Definition: ip_iterator.hpp:120
ipv4_Range::ipv4_Address::operator=
ipv4_Address operator=(ipv4_Address &adr)
Definition: ip_iterator.hpp:47
ipv4_Range::ipv4_Address::operator>
bool operator>(const ipv4_Address &rhs) const
Definition: ip_iterator.hpp:39
ipv4_Range::iterator::operator*
reference operator*() const
Definition: ip_iterator.hpp:112
ipv4_Range::ipv4_Address::operator==
bool operator==(const ipv4_Address &rhs) const
Definition: ip_iterator.hpp:35
ipv4_Range::iterator::iterator
iterator(ipv4_Address startAdr)
Definition: ip_iterator.hpp:97
ipv4_Range::end
iterator end()
Definition: ip_iterator.hpp:124
ipv4_Range::ipv4_Address::toString
std::string toString() const
Definition: ip_iterator.hpp:28
ipv4_Range::iterator::operator==
bool operator==(iterator other) const
Definition: ip_iterator.hpp:108
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:40
ipv4_Range::iterator
Definition: ip_iterator.hpp:88
ipv4_Range::ipv4_Address::operator++
ipv4_Address operator++(int)
Definition: ip_iterator.hpp:56
ipv4_Range::ipv4_Address::ipv4_Address
ipv4_Address(uint8_t p1, uint8_t p2, uint8_t p3, uint8_t p4)
Definition: ip_iterator.hpp:23
ipv4_Range::ipv4_Address
Definition: ip_iterator.hpp:16
ipv4_Range
Definition: ip_iterator.hpp:14
ipv4_Range::ipv4_Address::operator<
bool operator<(const ipv4_Address &rhs) const
Definition: ip_iterator.hpp:43