00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef TOOL_ADDR_HXX_
00023 # define TOOL_ADDR_HXX_
00024
00025 # include <cstring>
00026 # include <cassert>
00027 # include <sstream>
00028 # include <iomanip>
00029 # include <stdexcept>
00030 # include <sstream>
00031
00032 # include "addr.hh"
00033
00034 namespace tool
00035 {
00036
00037 template <class VT, unsigned N, bool HN, char SN>
00038 addr<VT, N, HN, SN>::addr()
00039 {
00040 memset(addr_, 0, sizeof (addr_));
00041 }
00042
00043 template <class VT, unsigned N, bool HN, char SN>
00044 addr<VT, N, HN, SN>::addr(const void* a)
00045 {
00046 memcpy(addr_, a, sizeof (addr_));
00047 }
00048
00049 template <class VT, unsigned N, bool HN, char SN>
00050 addr<VT, N, HN, SN>::addr(const std::string& str)
00051 {
00052 std::istringstream is (str);
00053
00054 read(is);
00055 if (not is)
00056 throw std::invalid_argument ("string is not a valid address");
00057 }
00058
00059 template <class VT, unsigned N, bool HN, char SN>
00060 addr<VT, N, HN, SN>::addr(std::istream& is)
00061 {
00062 read(is);
00063 if (not is)
00064 throw std::invalid_argument ("string is not a valid address");
00065 }
00066
00067 template <class VT, unsigned N, bool HN, char SN>
00068 std::istream&
00069 addr<VT, N, HN, SN>::read(std::istream& is)
00070 {
00071 if (HN)
00072 is >> std::hex;
00073 for (unsigned i = 0; i < N; ++i)
00074 {
00075 unsigned t;
00076
00077 if (i and is.get() != SN)
00078 {
00079 is.setstate(is.rdstate() | std::istream::failbit);
00080 return is;
00081 }
00082 if (not (is >> t))
00083 return is;
00084 addr_[i] = VT (t);
00085 }
00086 return is;
00087 }
00088
00089 template <class VT, unsigned N, bool HN, char SN>
00090 bool
00091 addr<VT, N, HN, SN>::operator < (const addr& rhs) const
00092 {
00093 return memcmp(addr_, rhs.addr_, sizeof (addr_)) < 0;
00094 }
00095
00096 template <class VT, unsigned N, bool HN, char SN>
00097 bool
00098 addr<VT, N, HN, SN>::operator == (const addr& rhs) const
00099 {
00100 return memcmp(addr_, rhs.addr_, sizeof (addr_)) == 0;
00101 }
00102
00103 template <class VT, unsigned N, bool HN, char SN>
00104 bool
00105 addr<VT, N, HN, SN>::operator != (const addr& rhs) const
00106 {
00107 return not operator == (rhs);
00108 }
00109
00110 template <class VT, unsigned N, bool HN, char SN>
00111 VT&
00112 addr<VT, N, HN, SN>::operator [] (unsigned i)
00113 {
00114 assert(i < N);
00115
00116 return addr_[i];
00117 }
00118
00119 template <class VT, unsigned N, bool HN, char SN>
00120 const VT&
00121 addr<VT, N, HN, SN>::operator [] (unsigned i) const
00122 {
00123 assert(i < N);
00124
00125 return addr_[i];
00126 }
00127
00128 template <class VT, unsigned N, bool HN, char SN>
00129 void
00130 addr<VT, N, HN, SN>::dump(void *a) const
00131 {
00132 memcpy(a, addr_, sizeof (addr_));
00133 }
00134
00135 template <class VT, unsigned N, bool HN, char SN>
00136 const addr<VT, N, HN, SN>&
00137 addr<VT, N, HN, SN>::null()
00138 {
00139 static const addr<VT, N, HN, SN> null;
00140
00141 return null;
00142 }
00143
00144 template <class VT, unsigned N, bool HN, char SN>
00145 const addr<VT, N, HN, SN>&
00146 addr<VT, N, HN, SN>::broadcast()
00147 {
00148 static bool init = false;
00149 static addr<VT, N, HN, SN> bcast;
00150
00151 if (not init)
00152 {
00153 memset(bcast.addr_, 0xFF, sizeof (bcast.addr_));
00154 init = true;
00155 }
00156 return bcast;
00157 }
00158
00159 template <class VT, unsigned N, bool HN, char SN>
00160 std::ostream&
00161 operator << (std::ostream& os, const addr<VT, N, HN, SN>& a)
00162 {
00163 if (HN)
00164 os << std::hex << std::setfill ('0');
00165 for (unsigned i = 0; i < N - 1; ++i)
00166 {
00167 if (HN)
00168 os << std::setw (sizeof (VT) * 2);
00169 os << unsigned (a[i]) << SN;
00170 }
00171 if (HN)
00172 os << std::setw (sizeof (VT) * 2);
00173 return os << unsigned (a[N - 1])
00174 << std::dec << std::setfill (' ');
00175 }
00176
00177 template <class VT, unsigned N, bool HN, char SN>
00178 std::istream&
00179 operator >> (std::istream& is, addr<VT, N, HN, SN>& a)
00180 {
00181 return a.read(is);
00182 }
00183
00184 template <class VT, unsigned N, bool HN, char SN>
00185 std::string
00186 make_string(const addr<VT, N, HN, SN>& a)
00187 {
00188 std::ostringstream os;
00189
00190 os << a;
00191 return os.str();
00192 }
00193
00194 }
00195
00196 #endif // ! TOOL_ADDR_HXX_