Jpp  master_rocky-40-g5f0272dcd
the software that should make you happy
JMACAddress.hh
Go to the documentation of this file.
1 #ifndef __JDATABASE__JMACADDRESS__
2 #define __JDATABASE__JMACADDRESS__
3 
4 #include <string>
5 #include <istream>
6 #include <ostream>
7 #include <iomanip>
8 #include <vector>
9 
10 #include <TROOT.h>
11 
12 #include "JLang/JEquals.hh"
13 
14 /**
15  * \file
16  * MAC address.
17  * \author mdejong
18  */
19 namespace JDATABASE {}
20 namespace JPP { using namespace JDATABASE; }
21 
22 namespace JDATABASE {
23 
24  using JLANG::JEquals;
25 
26 
27  /**
28  * MAC address
29  */
30  struct JMACAddress :
31  public std::vector<unsigned char>,
32  public JEquals<JMACAddress>
33  {
34  enum {
35  NUMBER_OF_BYTES = 6 //!< number of bytes
36  };
37 
38 
39  /**
40  * Get list of allowed separators.
41  *
42  * \return allowed separators
43  */
44  static const char* get_separator()
45  {
46  return ":.-";
47  }
48 
49 
50  /**
51  * Check if given character is an allowed separator.
52  *
53  * \param c character
54  * \return true if separator; else false
55  */
56  static bool is_separator(const int c)
57  {
58  const std::string buffer(JMACAddress::get_separator());
59 
60  for (std::string::const_iterator i = buffer.begin(); i != buffer.end(); ++i) {
61  if ((int) *i == c) {
62  return true;
63  }
64  }
65 
66  return false;
67  }
68 
69 
70  /**
71  * Default constructor.
72  */
74  std::vector<unsigned char>(NUMBER_OF_BYTES, 0)
75  {}
76 
77 
78  /**
79  * Equal method.
80  *
81  * \param address MAC address
82  * \return true if this MAC address and given MAC address are equal; else false
83  */
84  inline bool equal(const JMACAddress& address) const
85  {
86  for (int i = 0; i != NUMBER_OF_BYTES; ++i) {
87  if ((*this)[i] != address[i]) {
88  return false;
89  }
90  }
91 
92  return true;
93  }
94 
95 
96  /**
97  * Read MAC address from input stream.
98  *
99  * \param in input stream
100  * \param object MAC address
101  * \return input stream
102  */
103  friend inline std::istream& operator>>(std::istream& in, JMACAddress& object)
104  {
105  using namespace std;
106 
107  object = JMACAddress();
108 
109  int c;
110 
111  if (in >> hex >> c) {
112 
113  object[0] = (unsigned char) c;
114 
115  for (int i = 1; i != NUMBER_OF_BYTES; ++i) {
116  if (is_separator(in.get()) && in >> hex >> c)
117  object[i] = (unsigned char) c;
118  else
119  in.setstate(ios::failbit);
120  }
121  }
122 
123  return in >> dec;
124  }
125 
126 
127 
128  /**
129  * Write MAC address to output stream.
130  *
131  * \param out output stream
132  * \param object MAC address
133  * \return output stream
134  */
135  friend inline std::ostream& operator<<(std::ostream& out, const JMACAddress& object)
136  {
137  using namespace std;
138 
139  out << setw(2) << setfill('0') << hex << (int) object[0];
140 
141  for (int i = 1; i != NUMBER_OF_BYTES; ++i) {
142  out << setw(1) << JMACAddress::get_separator()[0]
143  << setw(2) << setfill('0') << hex << (int) object[i];
144  }
145 
146  return out << setfill(' ') << dec;
147  }
148 
150  };
151 }
152 
153 #endif
Auxiliary classes and methods for database I/O.
Definition: JAHRS.hh:14
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JSTDTypes.hh:14
static bool is_separator(const int c)
Check if given character is an allowed separator.
Definition: JMACAddress.hh:56
static const char * get_separator()
Get list of allowed separators.
Definition: JMACAddress.hh:44
JMACAddress()
Default constructor.
Definition: JMACAddress.hh:73
@ NUMBER_OF_BYTES
number of bytes
Definition: JMACAddress.hh:35
friend std::ostream & operator<<(std::ostream &out, const JMACAddress &object)
Write MAC address to output stream.
Definition: JMACAddress.hh:135
friend std::istream & operator>>(std::istream &in, JMACAddress &object)
Read MAC address from input stream.
Definition: JMACAddress.hh:103
bool equal(const JMACAddress &address) const
Equal method.
Definition: JMACAddress.hh:84
ClassDefNV(JMACAddress, 1)
Template definition of auxiliary base class for comparison of data structures.
Definition: JEquals.hh:84