|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- //
- // ip/impl/address.ipp
- // ~~~~~~~~~~~~~~~~~~~
- //
- // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
- //
- // Distributed under the Boost Software License, Version 1.0. (See accompanying
- // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
- //
-
- #ifndef ASIO_IP_IMPL_ADDRESS_IPP
- #define ASIO_IP_IMPL_ADDRESS_IPP
-
- #if defined(_MSC_VER) && (_MSC_VER >= 1200)
- # pragma once
- #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
-
- #include "asio/detail/config.hpp"
- #include <typeinfo>
- #include "asio/detail/throw_error.hpp"
- #include "asio/detail/throw_exception.hpp"
- #include "asio/error.hpp"
- #include "asio/ip/address.hpp"
- #include "asio/ip/bad_address_cast.hpp"
- #include "asio/system_error.hpp"
-
- #include "asio/detail/push_options.hpp"
-
- namespace asio {
- namespace ip {
-
- address::address()
- : type_(ipv4),
- ipv4_address_(),
- ipv6_address_()
- {
- }
-
- address::address(const asio::ip::address_v4& ipv4_address)
- : type_(ipv4),
- ipv4_address_(ipv4_address),
- ipv6_address_()
- {
- }
-
- address::address(const asio::ip::address_v6& ipv6_address)
- : type_(ipv6),
- ipv4_address_(),
- ipv6_address_(ipv6_address)
- {
- }
-
- address::address(const address& other)
- : type_(other.type_),
- ipv4_address_(other.ipv4_address_),
- ipv6_address_(other.ipv6_address_)
- {
- }
-
- #if defined(ASIO_HAS_MOVE)
- address::address(address&& other)
- : type_(other.type_),
- ipv4_address_(other.ipv4_address_),
- ipv6_address_(other.ipv6_address_)
- {
- }
- #endif // defined(ASIO_HAS_MOVE)
-
- address& address::operator=(const address& other)
- {
- type_ = other.type_;
- ipv4_address_ = other.ipv4_address_;
- ipv6_address_ = other.ipv6_address_;
- return *this;
- }
-
- #if defined(ASIO_HAS_MOVE)
- address& address::operator=(address&& other)
- {
- type_ = other.type_;
- ipv4_address_ = other.ipv4_address_;
- ipv6_address_ = other.ipv6_address_;
- return *this;
- }
- #endif // defined(ASIO_HAS_MOVE)
-
- address& address::operator=(const asio::ip::address_v4& ipv4_address)
- {
- type_ = ipv4;
- ipv4_address_ = ipv4_address;
- ipv6_address_ = asio::ip::address_v6();
- return *this;
- }
-
- address& address::operator=(const asio::ip::address_v6& ipv6_address)
- {
- type_ = ipv6;
- ipv4_address_ = asio::ip::address_v4();
- ipv6_address_ = ipv6_address;
- return *this;
- }
-
- address make_address(const char* str)
- {
- asio::error_code ec;
- address addr = make_address(str, ec);
- asio::detail::throw_error(ec);
- return addr;
- }
-
- address make_address(const char* str, asio::error_code& ec)
- {
- asio::ip::address_v6 ipv6_address =
- asio::ip::make_address_v6(str, ec);
- if (!ec)
- return address(ipv6_address);
-
- asio::ip::address_v4 ipv4_address =
- asio::ip::make_address_v4(str, ec);
- if (!ec)
- return address(ipv4_address);
-
- return address();
- }
-
- address make_address(const std::string& str)
- {
- return make_address(str.c_str());
- }
-
- address make_address(const std::string& str,
- asio::error_code& ec)
- {
- return make_address(str.c_str(), ec);
- }
-
- asio::ip::address_v4 address::to_v4() const
- {
- if (type_ != ipv4)
- {
- bad_address_cast ex;
- asio::detail::throw_exception(ex);
- }
- return ipv4_address_;
- }
-
- asio::ip::address_v6 address::to_v6() const
- {
- if (type_ != ipv6)
- {
- bad_address_cast ex;
- asio::detail::throw_exception(ex);
- }
- return ipv6_address_;
- }
-
- std::string address::to_string() const
- {
- if (type_ == ipv6)
- return ipv6_address_.to_string();
- return ipv4_address_.to_string();
- }
-
- #if !defined(ASIO_NO_DEPRECATED)
- std::string address::to_string(asio::error_code& ec) const
- {
- if (type_ == ipv6)
- return ipv6_address_.to_string(ec);
- return ipv4_address_.to_string(ec);
- }
- #endif // !defined(ASIO_NO_DEPRECATED)
-
- bool address::is_loopback() const
- {
- return (type_ == ipv4)
- ? ipv4_address_.is_loopback()
- : ipv6_address_.is_loopback();
- }
-
- bool address::is_unspecified() const
- {
- return (type_ == ipv4)
- ? ipv4_address_.is_unspecified()
- : ipv6_address_.is_unspecified();
- }
-
- bool address::is_multicast() const
- {
- return (type_ == ipv4)
- ? ipv4_address_.is_multicast()
- : ipv6_address_.is_multicast();
- }
-
- bool operator==(const address& a1, const address& a2)
- {
- if (a1.type_ != a2.type_)
- return false;
- if (a1.type_ == address::ipv6)
- return a1.ipv6_address_ == a2.ipv6_address_;
- return a1.ipv4_address_ == a2.ipv4_address_;
- }
-
- bool operator<(const address& a1, const address& a2)
- {
- if (a1.type_ < a2.type_)
- return true;
- if (a1.type_ > a2.type_)
- return false;
- if (a1.type_ == address::ipv6)
- return a1.ipv6_address_ < a2.ipv6_address_;
- return a1.ipv4_address_ < a2.ipv4_address_;
- }
-
- } // namespace ip
- } // namespace asio
-
- #include "asio/detail/pop_options.hpp"
-
- #endif // ASIO_IP_IMPL_ADDRESS_IPP
|