Browse Source

move definition of HardwareAddress to hardware_address.h

Fabian Peter Hammerle 7 years ago
parent
commit
5c8654b358
2 changed files with 45 additions and 30 deletions
  1. 44 0
      hardware_address.h
  2. 1 30
      main.cpp

+ 44 - 0
hardware_address.h

@@ -0,0 +1,44 @@
+#ifndef _IPYML_HARDWARE_ADDRESS_H
+#define _IPYML_HARDWARE_ADDRESS_H
+
+#include "yaml.h"
+
+#include <cassert>
+#include <cstring>
+#include <iomanip>
+#include <libmnl/libmnl.h>
+#include <linux/if.h>
+#include <linux/netlink.h>
+#include <ostream>
+#include <sstream>
+#include <string>
+
+class HardwareAddress : public YamlObject {
+  uint8_t bytes[IFHWADDRLEN];
+
+public:
+  HardwareAddress &operator=(const nlattr *attr) {
+    assert(mnl_attr_get_payload_len(attr) == IFHWADDRLEN);
+    memcpy(this->bytes, mnl_attr_get_payload(attr), IFHWADDRLEN);
+    return *this;
+  }
+
+  std::string format() const {
+    std::stringstream ss;
+    ss << std::hex << std::setfill('0');
+    for (int i = 0; i < IFHWADDRLEN; i++) {
+      if (i != 0) {
+        ss << ':';
+      }
+      ss << std::setw(2) << (int)bytes[i];
+    }
+    return ss.str();
+  }
+
+  void write_yaml(std::ostream &stream,
+                  const yaml_indent_level_t indent_level = 0) const {
+    stream << format();
+  }
+};
+
+#endif

+ 1 - 30
main.cpp

@@ -1,45 +1,16 @@
+#include "hardware_address.h"
 #include "vector.h"
 #include "yaml.h"
 
 #include <assert.h>
-#include <cstring>
-#include <iomanip>
 #include <iostream>
 #include <libmnl/libmnl.h>
 #include <linux/if.h>
 #include <linux/rtnetlink.h>
-#include <sstream>
 #include <string>
 
 // https://netfilter.org/projects/libmnl/doxygen/html/modules.html
 
-struct HardwareAddress : public YamlObject {
-  uint8_t bytes[IFHWADDRLEN];
-
-  HardwareAddress &operator=(const nlattr *attr) {
-    assert(mnl_attr_get_payload_len(attr) == IFHWADDRLEN);
-    memcpy(this->bytes, mnl_attr_get_payload(attr), IFHWADDRLEN);
-    return *this;
-  }
-
-  std::string format() const {
-    std::stringstream ss;
-    ss << std::hex << std::setfill('0');
-    for (int i = 0; i < IFHWADDRLEN; i++) {
-      if (i != 0) {
-        ss << ':';
-      }
-      ss << std::setw(2) << (int)bytes[i];
-    }
-    return ss.str();
-  }
-
-  void write_yaml(std::ostream &stream,
-                  const yaml_indent_level_t indent_level = 0) const {
-    stream << format();
-  }
-};
-
 struct Link : public YamlObject {
   std::string ifname;
   HardwareAddress hwaddr, broadcast;