官术网_书友最值得收藏!

15. IPv4 data type

The problem requires writing a class to represent an IPv4 address. This is a 32-bit value, usually represented in decimal dotted format, such as 168.192.0.100; each part of it is an 8-bit value, ranging from 0 to 255. For easy representation and handling, we can use four unsigned char to store the address value. Such a value could be constructed either from four unsigned char or from an unsigned long. In order to be able to read a value directly from the console (or any other input stream) and be able to write the value to the console (or any other output stream), we have to overload operator>> and operator<<. The following listing shows a minimal implementation that can meet the requested functionality:

class ipv4
{
std::array<unsigned char, 4> data;
public:
constexpr ipv4() : data{ {0} } {}
constexpr ipv4(unsigned char const a, unsigned char const b,
unsigned char const c, unsigned char const d):
data{{a,b,c,d}} {}
explicit constexpr ipv4(unsigned long a) :
data{ { static_cast<unsigned char>((a >> 24) & 0xFF),
static_cast<unsigned char>((a >> 16) & 0xFF),
static_cast<unsigned char>((a >> 8) & 0xFF),
static_cast<unsigned char>(a & 0xFF) } } {}
ipv4(ipv4 const & other) noexcept : data(other.data) {}
ipv4& operator=(ipv4 const & other) noexcept
{
data = other.data;
return *this;
}

std::string to_string() const
{
std::stringstream sstr;
sstr << *this;
return sstr.str();
}

constexpr unsigned long to_ulong() const noexcept
{
return (static_cast<unsigned long>(data[0]) << 24) |
(static_cast<unsigned long>(data[1]) << 16) |
(static_cast<unsigned long>(data[2]) << 8) |
static_cast<unsigned long>(data[3]);
}

friend std::ostream& operator<<(std::ostream& os, const ipv4& a)
{
os << static_cast<int>(a.data[0]) << '.'
<< static_cast<int>(a.data[1]) << '.'
<< static_cast<int>(a.data[2]) << '.'
<< static_cast<int>(a.data[3]);
return os;
}

friend std::istream& operator>>(std::istream& is, ipv4& a)
{
char d1, d2, d3;
int b1, b2, b3, b4;
is >> b1 >> d1 >> b2 >> d2 >> b3 >> d3 >> b4;
if (d1 == '.' && d2 == '.' && d3 == '.')
a = ipv4(b1, b2, b3, b4);
else
is.setstate(std::ios_base::failbit);
return is;
}
};

The ipv4 class can be used as follows:

int main()
{
ipv4 address(168, 192, 0, 1);
std::cout << address << std::endl;

ipv4 ip;
std::cout << ip << std::endl;
std::cin >> ip;
if(!std::cin.fail())
std::cout << ip << std::endl;
}
主站蜘蛛池模板: 会理县| 溧水县| 册亨县| 凤庆县| 灌阳县| 苏州市| 策勒县| 五常市| 钟山县| 玛多县| 安国市| 宜兰县| 汨罗市| 公安县| 肃北| 闽侯县| 和政县| 鹤壁市| 巨野县| 延津县| 固安县| 时尚| 安西县| 乐至县| 新乡市| 呼和浩特市| 通河县| 辛集市| 绥棱县| 镇沅| 洛浦县| 道孚县| 临沂市| 南通市| 靖边县| 黄大仙区| 乌海市| 瓦房店市| 苗栗县| 松江区| 郴州市|