MacAddress.h
Go to the documentation of this file.
1 /* ---------------------------------------------------------------------------
2  commonc++ - A C++ Common Class Library
3  Copyright (C) 2005-2012 Mark A Lindner
4 
5  This file is part of commonc++.
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public
18  License along with this library; if not, write to the Free
19  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  ---------------------------------------------------------------------------
21 
22  http://www.hyperrealm.com/oss_commoncpp.shtml
23 */
24 
25 #pragma once
26 
27 #include "WString.h"
28 #include "Print.h"
29 #include <cstdint>
30 
39 {
40  // https://www.artima.com/cppsource/safebool.html
41  using bool_type = void (MacAddress::*)() const;
42  void Testable() const
43  {
44  }
45 
46 public:
47  using Octets = uint8_t[6];
48 
49  MacAddress() = default;
50 
51  MacAddress(const Octets& octets)
52  {
53  setOctets(octets);
54  }
55 
61  MacAddress(const String& s);
62 
66  void getOctets(Octets& octets) const
67  {
68  memcpy(octets, this->octets, 6);
69  }
70 
74  void setOctets(const Octets& octets)
75  {
76  memcpy(this->octets, octets, 6);
77  }
78 
84  const uint8_t& operator[](unsigned index) const
85  {
86  return const_cast<MacAddress*>(this)->operator[](index);
87  }
88 
94  uint8_t& operator[](unsigned index);
95 
104  String toString(char sep = ':') const;
105 
106  operator String() const
107  {
108  return toString();
109  }
110 
114  bool operator==(const MacAddress& other) const
115  {
116  return memcmp(octets, other.octets, sizeof(octets)) == 0;
117  }
118 
122  inline bool operator!=(const MacAddress& other) const
123  {
124  return !operator==(other);
125  }
126 
130  bool operator!() const;
131 
135  operator bool_type() const
136  {
137  return operator!() ? nullptr : &MacAddress::Testable;
138  }
139 
143  void clear()
144  {
145  memset(octets, 0, sizeof(octets));
146  }
147 
153  uint32_t getHash() const;
154 
155 private:
156  Octets octets = {0};
157 };
158 
159 #define MACADDR_NONE MacAddress()
A network hardware (MAC) address.
Definition: MacAddress.h:39
void clear()
Clear address to null value.
Definition: MacAddress.h:143
void setOctets(const Octets &octets)
Set the octets of the MAC address.
Definition: MacAddress.h:74
void getOctets(Octets &octets) const
Get the octets of the MAC address.
Definition: MacAddress.h:66
bool operator==(const MacAddress &other) const
Equality operator.
Definition: MacAddress.h:114
MacAddress()=default
MacAddress(const Octets &octets)
Definition: MacAddress.h:51
uint8_t & operator[](unsigned index)
Get a reference to the octet at the given index in the MAC address.
MacAddress(const String &s)
Create a MAC address from valid string. e.g. 01:02:03:04:05:06 Separators are optional.
uint8_t[6] Octets
Definition: MacAddress.h:47
bool operator!=(const MacAddress &other) const
Inequality operator.
Definition: MacAddress.h:122
String toString(char sep=':') const
Return a String representation of the MacAddress.
uint32_t getHash() const
Generate a simple 32-bit hash from the MAC address.
bool operator!() const
Test if this is a null address (00:00:00:00:00:00).
const uint8_t & operator[](unsigned index) const
Get the octet at the given index in the MAC address.
Definition: MacAddress.h:84
The String class.
Definition: WString.h:137