123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- #ifndef BOOST_MYSQL_ANY_ADDRESS_HPP
- #define BOOST_MYSQL_ANY_ADDRESS_HPP
- #include <boost/mysql/defaults.hpp>
- #include <boost/mysql/string_view.hpp>
- #include <boost/mysql/detail/access.hpp>
- #include <string>
- namespace boost {
- namespace mysql {
- enum class address_type
- {
-
- host_and_port,
-
- unix_path
- };
- struct host_and_port
- {
-
- std::string host;
-
- unsigned short port{default_port};
- };
- struct unix_path
- {
-
- std::string path;
- };
- class any_address
- {
- #ifndef BOOST_MYSQL_DOXYGEN
- struct
- {
- address_type type;
- std::string address;
- unsigned short port;
- } impl_;
- any_address(address_type t, std::string&& addr, unsigned short port) noexcept
- : impl_{t, std::move(addr), port}
- {
- }
- friend struct detail::access;
- #endif
- public:
-
- any_address() noexcept : any_address(address_type::host_and_port, std::string(), default_port) {}
-
- any_address(const any_address& other) = default;
-
- any_address(any_address&& other) = default;
-
- any_address& operator=(const any_address& other) = default;
-
- any_address& operator=(any_address&& other) = default;
-
- ~any_address() = default;
-
- any_address(host_and_port value) noexcept
- : impl_{address_type::host_and_port, std::move(value.host), value.port}
- {
- }
-
- any_address(unix_path value) noexcept : impl_{address_type::unix_path, std::move(value.path), 0} {}
-
- address_type type() const noexcept { return impl_.type; }
-
- string_view hostname() const noexcept
- {
- BOOST_ASSERT(type() == address_type::host_and_port);
- return impl_.address;
- }
-
- unsigned short port() const noexcept
- {
- BOOST_ASSERT(type() == address_type::host_and_port);
- return impl_.port;
- }
-
- string_view unix_socket_path() const noexcept
- {
- BOOST_ASSERT(type() == address_type::unix_path);
- return impl_.address;
- }
-
- void emplace_host_and_port(std::string hostname, unsigned short port = default_port)
- {
- impl_.type = address_type::host_and_port;
- impl_.address = std::move(hostname);
- impl_.port = port;
- }
-
- void emplace_unix_path(std::string path)
- {
- impl_.type = address_type::unix_path;
- impl_.address = std::move(path);
- impl_.port = 0;
- }
-
- bool operator==(const any_address& rhs) const noexcept
- {
- return impl_.type == rhs.impl_.type && impl_.address == rhs.impl_.address &&
- impl_.port == rhs.impl_.port;
- }
-
- bool operator!=(const any_address& rhs) const noexcept { return !(*this == rhs); }
- };
- }
- }
- #endif
|