9 static const char rcsid[] __attribute__((used)) =
"$Id: IPAddress.cpp 1702 2016-08-13 23:26:06Z sella $";
11 #include "IPAddress.h"
12 #include "../net/IPAddress.h"
13 #include "../util/CommonMacro.h"
15 using namespace sella::variant;
16 namespace sn = sella::net;
20 IPAddress::IPAddress(
const std::string &value)
throw (TypeCastException) :
Nullable() {
21 if (LIKELY(boost::regex_match(value, IPAddress::Pattern()))) {
22 this->value = std::make_shared<sn::IPAddress>(value);
24 THROW(TypeCastException,
"unable to cast '%s' to a ipaddress value", value.c_str());
28 IPAddress::IPAddress(
const sn::IPAddress &value)
throw () :
Nullable(std::make_shared<sn::IPAddress>(value)) { }
33 if (LIKELY(
this != &other)) {
34 if (LIKELY(!other.isNull())) {
35 if (UNLIKELY(isNull())) {
36 this->value = std::make_shared<sn::IPAddress>(other.get());
38 *std::static_pointer_cast<
sn::IPAddress>(this->value) = *std::static_pointer_cast<sn::IPAddress>(other.value);
47 if (UNLIKELY(!other.isNull())) {
48 this->value = std::move(other.value);
52 bool IPAddress::operator==(
const IPAddress &other)
const throw () {
53 return (*std::static_pointer_cast<sn::IPAddress>(this->value) == *std::static_pointer_cast<sn::IPAddress>(other.value));
56 bool IPAddress::operator!=(
const IPAddress &other)
const throw () {
57 return !(*
this == other);
60 bool IPAddress::operator>(
const IPAddress &other)
const throw () {
61 return (*std::static_pointer_cast<sn::IPAddress>(this->value) > *std::static_pointer_cast<sn::IPAddress>(other.value));
64 bool IPAddress::operator<(
const IPAddress &other)
const throw () {
65 return (*std::static_pointer_cast<sn::IPAddress>(this->value) < *std::static_pointer_cast<sn::IPAddress>(other.value));
69 if (LIKELY(
this != &other)) {
70 if (LIKELY(!other.isNull())) {
71 if (UNLIKELY(isNull())) {
72 this->value = std::make_shared<sn::IPAddress>(other.get());
74 *std::static_pointer_cast<
sn::IPAddress>(this->value) = *std::static_pointer_cast<sn::IPAddress>(other.value);
85 if (LIKELY(
this != &other)) {
86 if (LIKELY(!other.isNull())) {
87 this->value = std::move(other.value);
95 if (UNLIKELY(isNull())) {
96 this->value = std::make_shared<sn::IPAddress>(other);
98 *std::static_pointer_cast<
sn::IPAddress>(this->value) = other;
104 IPAddress::~IPAddress() throw () { }
106 const sn::IPAddress& IPAddress::get()
const throw (UnsupportedOperatorException) {
107 if (UNLIKELY(isNull())) {
108 THROW(UnsupportedOperatorException,
"unable to take reference of null value");
111 return *std::static_pointer_cast<
sn::IPAddress>(this->value);
114 sn::IPAddress& IPAddress::get() throw (UnsupportedOperatorException) {
115 if (UNLIKELY(isNull())) {
116 THROW(UnsupportedOperatorException,
"unable to take reference of null value");
119 return *std::static_pointer_cast<
sn::IPAddress>(this->value);
122 void IPAddress::clear() throw() {
123 if (UNLIKELY(isNull())) {
124 this->value = std::make_shared<sn::IPAddress>();
126 std::static_pointer_cast<
sn::IPAddress>(this->value)->clear();
133 if (UNLIKELY(isNull())) {
134 c = mpack.append(offset);
136 c = mpack.append(offset, std::static_pointer_cast<sella::net::IPAddress>(this->value)->str());
141 if (UNLIKELY(c == 0)) {
142 THROW(TypeCastException,
"Failed to append '%s' at offset %d (buffer length: %d)", str(JSON).c_str(), offset, mpack.length());
146 void IPAddress::msgpackBufferSize(
size_t &size)
const throw () {
147 if (UNLIKELY(isNull())) {
153 size += 5 + (std::static_pointer_cast<
sella::net::IPAddress>(this->value)->getFamily() == sn::IPAddress::IPv4Family) ? 15 : 39;
156 std::string IPAddress::str(
int type)
const throw () {
157 if (UNLIKELY(isNull())) {
161 return this->
get().str();
164 bool IPAddress::isType(
const char *value,
size_t size)
throw () {
165 unsigned char buf[
sizeof(
struct in6_addr)];
167 for (
int i = 0, len = (size > 5) ? 5 : size; i < len; i++) {
168 if (value[i] ==
'.') {
169 return (inet_pton(AF_INET, value, buf) == 1);
170 }
else if (value[i] ==
':') {
171 return (inet_pton(AF_INET6, value, buf) == 1);