00001
00002
00003
00004
00005
00006
00007
00008
00009 static const char rcsid[] __attribute__((used)) = "$Id: IPAddress.cpp 1197 2014-10-14 22:26:11Z sella $";
00010
00011 #include "IPAddress.h"
00012 #include "../net/IPAddress.h"
00013
00014 using namespace sella::variant;
00015 namespace sn = sella::net;
00016
00017
00018 const boost::regex IPAddress::Pattern("^((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])|((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(\\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(\\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(\\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(\\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(\\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(\\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(\\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}))|:)))(%.+)?)$");
00019
00020 IPAddress::IPAddress() throw () : Nullable() { }
00021
00022 IPAddress::IPAddress(const std::string &value) throw (TypeCastException) : Nullable() {
00023 if (boost::regex_match(value, IPAddress::Pattern)) {
00024 this->value = std::make_shared<sn::IPAddress>(value);
00025 } else {
00026 THROW(TypeCastException, "unable to cast '%s' to a ipaddress value", value.c_str());
00027 }
00028 }
00029
00030 IPAddress::IPAddress(sn::IPAddress &value) throw () : Nullable(std::make_shared<sn::IPAddress>(value)) { }
00031
00032 IPAddress::IPAddress(const IPAddress &other) throw () : Nullable(other) {
00033 if (!other.isNull()) {
00034 this->value = std::make_shared<sn::IPAddress>(*other.getIPAddress());
00035 }
00036 }
00037
00038 bool IPAddress::operator==(const IPAddress &other) const throw () {
00039 return (*std::static_pointer_cast<sn::IPAddress>(this->value) == *std::static_pointer_cast<sn::IPAddress>(other.value));
00040 }
00041
00042 bool IPAddress::operator!=(const IPAddress &other) const throw () {
00043 return !(*this == other);
00044 }
00045
00046 bool IPAddress::operator>(const IPAddress &other) const throw () {
00047 return (*std::static_pointer_cast<sn::IPAddress>(this->value) > *std::static_pointer_cast<sn::IPAddress>(other.value));
00048 }
00049
00050 bool IPAddress::operator<(const IPAddress &other) const throw () {
00051 return (*std::static_pointer_cast<sn::IPAddress>(this->value) < *std::static_pointer_cast<sn::IPAddress>(other.value));
00052 }
00053
00054 IPAddress& IPAddress::operator=(const IPAddress &other) throw () {
00055 if (this != &other) {
00056 Nullable::operator=(other);
00057 if (!other.isNull()) {
00058 this->value = std::make_shared<sn::IPAddress>(*other.getIPAddress());
00059 }
00060 }
00061
00062 return *this;
00063 }
00064
00065 IPAddress::~IPAddress() throw () { }
00066
00067 const sn::IPAddress* IPAddress::getIPAddress() const throw (UnsupportedOperatorException) {
00068 if (isNull()) {
00069 THROW(UnsupportedOperatorException, "unable to take reference of null value");
00070 }
00071
00072 return &(*std::static_pointer_cast<sn::IPAddress>(this->value));
00073 }
00074
00075 const std::string& IPAddress::str() const throw () {
00076 if (cache.empty()) {
00077 if (this->isNull()) {
00078 cache = "null";
00079 } else {
00080 cache = this->getIPAddress()->str();
00081 }
00082 }
00083
00084 return cache;
00085 }
00086
00087
00088
00089