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