libcosmos
Linux C++ System Programming Library
All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
InterfaceAddressIterator.hxx
1#pragma once
2
3// cosmos
4#include <cosmos/error/RuntimeError.hxx>
5#include <cosmos/net/InterfaceAddress.hxx>
6
7namespace cosmos {
8
10
15 friend class InterfaceAddressList;
16protected: // functions
17
18 explicit InterfaceAddressIterator(struct ifaddrs *pos) :
19 m_pos{pos} {
20 }
21
22public: // functions
23
24 auto& operator++() {
25 if (m_pos) {
26 m_pos = m_pos->ifa_next;
27 } else {
28 cosmos_throw (RuntimeError("Attempt to increment InterfaceAddressIterator past the end"));
29 }
30 return *this;
31 }
32
33 const InterfaceAddress& operator*() {
34 if (!m_pos) {
35 cosmos_throw (RuntimeError("Attempt to dereference invalid InterfaceAddressIterator"));
36 }
37
38 return *(reinterpret_cast<InterfaceAddress*>(m_pos));
39 }
40
41 bool operator==(const InterfaceAddressIterator &other) const {
42 return m_pos == other.m_pos;
43 }
44
45 bool operator!=(const InterfaceAddressIterator &other) const {
46 return !(*this == other);
47 }
48
49protected: // data
50
51 struct ifaddrs *m_pos = nullptr;
52};
53
54} // end ns
Iterator helper type for InterfaceAddressList.
Access to the list of local network interface addresses.
A single network interface address.
Exception type for generic runtime errors.