roo_io
API Documentation for roo_io
Loading...
Searching...
No Matches
mac_address.h
Go to the documentation of this file.
1#pragma once
2
3#include <stdint.h>
4
5#include <cstring>
6#include <string>
7
8#include "roo_io/base/byte.h"
9#include "roo_logging.h"
10
11namespace roo_io {
12
14 public:
15 static const MacAddress& Broadcast();
16
17 MacAddress() { memset(addr_, 0, 6); }
18
19 MacAddress(const byte* addr) { memcpy(addr_, addr, 6); }
20
21 MacAddress(unsigned char a0, unsigned char a1, unsigned char a2,
22 unsigned char a3, unsigned char a4, unsigned char a5);
23
24#if (defined UINT8_MAX && __cplusplus >= 201703L)
26#endif
27
28 int operator[](int idx) const {
29 DCHECK(idx >= 0 && idx <= 5);
30 return (int)asBytes()[idx];
31 }
32
33 const byte* asBytes() const { return addr_; }
34
35 std::string asString() const;
36
37 uint64_t asU64() const;
38
39 void writeTo(byte* buf) const;
40
41#if (defined UINT8_MAX && __cplusplus >= 201703L)
42 void writeTo(uint8_t* buf) const { writeTo((byte*)buf); }
43#endif
44
45 // Writes the address to buf, in the format "XX-XX-XX-XX-XX-XX".
46 // Buf must be 18+.
47 void writeStringTo(char* buf) const;
48
50
51 bool parseFrom(const char* rep);
52
53 private:
54 byte addr_[6];
55};
56
57bool operator==(const MacAddress& a, const MacAddress& b);
58bool operator!=(const MacAddress& a, const MacAddress& b);
59bool operator<(const MacAddress& a, const MacAddress& b);
60bool operator>(const MacAddress& a, const MacAddress& b);
61bool operator<=(const MacAddress& a, const MacAddress& b);
62bool operator<=(const MacAddress& a, const MacAddress& b);
63
64template <typename InputIterator>
66 byte addr[] = {byte{0}, byte{0}, byte{0}, byte{0}, byte{0}, byte{0}};
68 return MacAddress(addr);
69}
70
71template <typename OutputIterator>
73 WriteBytes(itr, addr.asBytes(), 6);
74}
75
76roo_logging::Stream& operator<<(roo_logging::Stream& s, const MacAddress& addr);
77
78} // namespace roo_io
79
80namespace std {
81
82template<>
83struct hash<roo_io::MacAddress> {
84 size_t operator()(const roo_io::MacAddress& addr) const {
85 return std::hash<uint64_t>()(addr.asU64());
86 }
87};
88
89}
bool parseFrom(const char *rep)
const byte * asBytes() const
Definition mac_address.h:33
int operator[](int idx) const
Definition mac_address.h:28
uint64_t asU64() const
MacAddress(const byte *addr)
Definition mac_address.h:19
MacAddress(unsigned char a0, unsigned char a1, unsigned char a2, unsigned char a3, unsigned char a4, unsigned char a5)
void writeStringTo(char *buf) const
void writeTo(byte *buf) const
std::string asString() const
bool assignFromU64(uint64_t addr)
static const MacAddress & Broadcast()
Definition byte.h:6
roo::basic_string_view< CharT, Traits > basic_string_view
Definition string_view.h:8
roo_logging::Stream & operator<<(roo_logging::Stream &s, const MacAddress &addr)
MacAddress ReadMacAddress(InputIterator &itr)
Definition mac_address.h:65
bool operator>(const MacAddress &a, const MacAddress &b)
bool operator==(const MacAddress &a, const MacAddress &b)
void WriteMacAddress(OutputIterator &itr, const MacAddress &addr)
Definition mac_address.h:72
bool operator!=(const MacAddress &a, const MacAddress &b)
bool operator<(const MacAddress &a, const MacAddress &b)
roo::byte byte
Definition byte.h:8
bool operator<=(const MacAddress &a, const MacAddress &b)