net/addr/ether: Add basic MAC comparison and hashing
Change-Id: I8e6ddb709726326357cad4c583fec38c18d1ea83
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/include/meson.build b/include/meson.build
index 6d02351..50ecdef 100644
--- a/include/meson.build
+++ b/include/meson.build
@@ -9,6 +9,7 @@
'stdplus/hash.hpp',
'stdplus/hash/array.hpp',
'stdplus/hash/tuple.hpp',
+ 'stdplus/net/addr/ether.hpp',
'stdplus/net/addr/ip.hpp',
'stdplus/numeric/endian.hpp',
'stdplus/pinned.hpp',
diff --git a/include/stdplus/net/addr/ether.hpp b/include/stdplus/net/addr/ether.hpp
new file mode 100644
index 0000000..91cf031
--- /dev/null
+++ b/include/stdplus/net/addr/ether.hpp
@@ -0,0 +1,30 @@
+#pragma once
+#include <net/ethernet.h>
+
+#include <stdplus/hash.hpp>
+
+#include <algorithm>
+#include <variant>
+
+namespace stdplus
+{
+
+struct EtherAddr : ether_addr
+{
+ constexpr bool operator==(ether_addr rhs) const noexcept
+ {
+ return std::equal(ether_addr_octet, ether_addr_octet + 6,
+ rhs.ether_addr_octet);
+ }
+};
+
+} // namespace stdplus
+
+template <>
+struct std::hash<stdplus::EtherAddr>
+{
+ constexpr std::size_t operator()(ether_addr addr) const noexcept
+ {
+ return stdplus::hashMulti(addr.ether_addr_octet);
+ }
+};
diff --git a/src/meson.build b/src/meson.build
index e62ecbd..7367761 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -50,6 +50,7 @@
'hash.cpp',
'hash/array.cpp',
'hash/tuple.cpp',
+ 'net/addr/ether.cpp',
'net/addr/ip.cpp',
'numeric/endian.cpp',
'pinned.cpp',
diff --git a/src/net/addr/ether.cpp b/src/net/addr/ether.cpp
new file mode 100644
index 0000000..e50a2ff
--- /dev/null
+++ b/src/net/addr/ether.cpp
@@ -0,0 +1 @@
+#include <stdplus/net/addr/ether.hpp>
diff --git a/test/meson.build b/test/meson.build
index e55b18c..c9ee047 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -6,6 +6,7 @@
'hash': [stdplus_dep, gtest_main_dep],
'hash/array': [stdplus_dep, gtest_main_dep],
'hash/tuple': [stdplus_dep, gtest_main_dep],
+ 'net/addr/ether': [stdplus_dep, gtest_main_dep],
'net/addr/ip': [stdplus_dep, gtest_main_dep],
'numeric/endian': [stdplus_dep, gtest_main_dep],
'pinned': [stdplus_dep, gtest_main_dep],
diff --git a/test/net/addr/ether.cpp b/test/net/addr/ether.cpp
new file mode 100644
index 0000000..73ae5eb
--- /dev/null
+++ b/test/net/addr/ether.cpp
@@ -0,0 +1,16 @@
+#include <stdplus/net/addr/ether.hpp>
+
+#include <gtest/gtest.h>
+
+namespace stdplus
+{
+
+TEST(EqualOperator, EtherAddr)
+{
+ EXPECT_EQ((EtherAddr{1}), (ether_addr{1}));
+ EXPECT_EQ((EtherAddr{}), (EtherAddr{}));
+ EXPECT_NE((EtherAddr{1}), (EtherAddr{ether_addr{}}));
+ std::hash<EtherAddr>{}(EtherAddr{});
+}
+
+} // namespace stdplus