any_address.hpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. //
  2. // Copyright (c) 2019-2025 Ruben Perez Hidalgo (rubenperez038 at gmail dot com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. #ifndef BOOST_MYSQL_ANY_ADDRESS_HPP
  8. #define BOOST_MYSQL_ANY_ADDRESS_HPP
  9. #include <boost/mysql/defaults.hpp>
  10. #include <boost/mysql/string_view.hpp>
  11. #include <boost/mysql/detail/access.hpp>
  12. #include <string>
  13. namespace boost {
  14. namespace mysql {
  15. /// The type of an address identifying a MySQL server.
  16. enum class address_type
  17. {
  18. /// An Internet hostname and a TCP port.
  19. host_and_port,
  20. /// A UNIX domain socket path.
  21. unix_path
  22. };
  23. /**
  24. * \brief A host and port identifying how to connect to a MySQL server.
  25. * \details
  26. * This is an owning type with value semantics.
  27. */
  28. struct host_and_port
  29. {
  30. /**
  31. * \brief The hostname where the MySQL server is expected to be listening.
  32. * \details
  33. * An empty string is equivalent to `localhost`. This is the default.
  34. * This is an owning field
  35. */
  36. std::string host;
  37. /// The port where the MySQL server is expected to be listening.
  38. unsigned short port{default_port};
  39. };
  40. /**
  41. * \brief Contains a UNIX-socket domain path.
  42. * \details
  43. * This type is defined in all systems, regardless of their UNIX socket support.
  44. * \n
  45. * This is an owning type with value semantics.
  46. */
  47. struct unix_path
  48. {
  49. /**
  50. * \brief The UNIX domain socket path where the MySQL server is listening.
  51. * \details Defaults to the empty string. This is an owning field.
  52. */
  53. std::string path;
  54. };
  55. /**
  56. * \brief A server address, identifying how to physically connect to a MySQL server.
  57. * \details
  58. * A variant-like type that can represent the network address of a MySQL server,
  59. * regardless of the transport type being used. It can contain either a host
  60. * and port (to connect using TCP) or a UNIX path (to connect using UNIX domain sockets).
  61. * \n
  62. * This class may be extended in the future to accommodate Windows named pipes.
  63. * \n
  64. * This type has value semantics: it is owning and regular.
  65. */
  66. class any_address
  67. {
  68. #ifndef BOOST_MYSQL_DOXYGEN
  69. struct
  70. {
  71. address_type type;
  72. std::string address;
  73. unsigned short port;
  74. } impl_;
  75. any_address(address_type t, std::string&& addr, unsigned short port) noexcept
  76. : impl_{t, std::move(addr), port}
  77. {
  78. }
  79. friend struct detail::access;
  80. #endif
  81. public:
  82. /**
  83. * \brief Constructs an empty address.
  84. * \details Results in an address with `this->type() == address_type::host_and_port`,
  85. * `this->hostname() == ""` and `this->port() == default_port`, which identifies
  86. * a server running on `localhost` using the default port.
  87. * \par Exception safety
  88. * No-throw guarantee.
  89. */
  90. any_address() noexcept : any_address(address_type::host_and_port, std::string(), default_port) {}
  91. /**
  92. * \brief Copy constructor.
  93. * \par Exception safety
  94. * Strong guarantee. Exceptions may be thrown by memory allocations.
  95. * \par Object lifetimes
  96. * `*this` and `other` will have independent lifetimes (regular value semantics).
  97. */
  98. any_address(const any_address& other) = default;
  99. /**
  100. * \brief Move constructor.
  101. * \details Leaves `other` in a valid but unspecified state.
  102. * \par Exception safety
  103. * No-throw guarantee.
  104. */
  105. any_address(any_address&& other) = default;
  106. /**
  107. * \brief Copy assignment.
  108. * \par Exception safety
  109. * Basic guarantee. Exceptions may be thrown by memory allocations.
  110. * \par Object lifetimes
  111. * `*this` and `other` will have independent lifetimes (regular value semantics).
  112. */
  113. any_address& operator=(const any_address& other) = default;
  114. /**
  115. * \brief Move assignment.
  116. * \details Leaves `other` in a valid but unspecified state.
  117. * \par Exception safety
  118. * No-throw guarantee.
  119. */
  120. any_address& operator=(any_address&& other) = default;
  121. /// Destructor.
  122. ~any_address() = default;
  123. /**
  124. * \brief Constructs an address containing a host and a port.
  125. * \details Results in an address with `this->type() == address_type::host_and_port`,
  126. * `this->hostname() == value.hostname()` and `this->port() == value.port()`.
  127. *
  128. * \par Object lifetimes
  129. * `*this` and `value` will have independent lifetimes (regular value semantics).
  130. *
  131. * \par Exception safety
  132. * No-throw guarantee.
  133. */
  134. any_address(host_and_port value) noexcept
  135. : impl_{address_type::host_and_port, std::move(value.host), value.port}
  136. {
  137. }
  138. /**
  139. * \brief Constructs an address containing a UNIX socket path.
  140. * \details Results in an address with `this->type() == address_type::unix_path`,
  141. * `this->unix_socket_path() == value.path()`.
  142. *
  143. * \par Object lifetimes
  144. * `*this` and `value` will have independent lifetimes (regular value semantics).
  145. *
  146. * \par Exception safety
  147. * No-throw guarantee.
  148. */
  149. any_address(unix_path value) noexcept : impl_{address_type::unix_path, std::move(value.path), 0} {}
  150. /**
  151. * \brief Retrieves the type of address that this object contains.
  152. * \par Exception safety
  153. * No-throw guarantee.
  154. */
  155. address_type type() const noexcept { return impl_.type; }
  156. /**
  157. * \brief Retrieves the hostname that this object contains.
  158. * \par Preconditions
  159. * `this->type() == address_type::host_and_port`
  160. *
  161. * \par Object lifetimes
  162. * The returned view points into `*this`, and is valid as long as `*this`
  163. * is alive and hasn't been assigned to or moved from.
  164. *
  165. * \par Exception safety
  166. * No-throw guarantee.
  167. */
  168. string_view hostname() const noexcept
  169. {
  170. BOOST_ASSERT(type() == address_type::host_and_port);
  171. return impl_.address;
  172. }
  173. /**
  174. * \brief Retrieves the port that this object contains.
  175. * \par Preconditions
  176. * `this->type() == address_type::host_and_port`
  177. *
  178. * \par Exception safety
  179. * No-throw guarantee.
  180. */
  181. unsigned short port() const noexcept
  182. {
  183. BOOST_ASSERT(type() == address_type::host_and_port);
  184. return impl_.port;
  185. }
  186. /**
  187. * \brief Retrieves the UNIX socket path that this object contains.
  188. * \par Preconditions
  189. * `this->type() == address_type::unix_path`
  190. *
  191. * \par Object lifetimes
  192. * The returned view points into `*this`, and is valid as long as `*this`
  193. * is alive and hasn't been assigned to or moved from.
  194. *
  195. * \par Exception safety
  196. * No-throw guarantee.
  197. */
  198. string_view unix_socket_path() const noexcept
  199. {
  200. BOOST_ASSERT(type() == address_type::unix_path);
  201. return impl_.address;
  202. }
  203. /**
  204. * \brief Replaces the current object with a host and port.
  205. * \details
  206. * Destroys the current contained object and constructs a new
  207. * host and port from the passed components. This function can
  208. * change the underlying type of object held by `*this`.
  209. * \n
  210. * The constructed object has `this->type() == address_type::host_and_port`,
  211. * `this->hostname() == hostname` and `this->port() == port`.
  212. * \n
  213. * An empty hostname is equivalent to `localhost`.
  214. * \n
  215. * \par Exception safety
  216. * Basic guarantee. Memory allocations may throw.
  217. * \par Object lifetimes
  218. * Invalidates views pointing into `*this`.
  219. */
  220. void emplace_host_and_port(std::string hostname, unsigned short port = default_port)
  221. {
  222. impl_.type = address_type::host_and_port;
  223. impl_.address = std::move(hostname);
  224. impl_.port = port;
  225. }
  226. /**
  227. * \brief Replaces the current object with a UNIX socket path.
  228. * \details
  229. * Destroys the current contained object and constructs a new
  230. * UNIX socket path from the passed value. This function can
  231. * change the underlying type of object held by `*this`.
  232. * \n
  233. * The constructed object has `this->type() == address_type::unix_path` and
  234. * `this->unix_socket_path() == path`.
  235. * \n
  236. * \par Exception safety
  237. * Basic guarantee. Memory allocations may throw.
  238. * \par Object lifetimes
  239. * Invalidates views pointing into `*this`.
  240. */
  241. void emplace_unix_path(std::string path)
  242. {
  243. impl_.type = address_type::unix_path;
  244. impl_.address = std::move(path);
  245. impl_.port = 0;
  246. }
  247. /**
  248. * \brief Tests for equality.
  249. * \details Two addresses are equal if they have the same type and individual components.
  250. * \par Exception safety
  251. * No-throw guarantee.
  252. */
  253. bool operator==(const any_address& rhs) const noexcept
  254. {
  255. return impl_.type == rhs.impl_.type && impl_.address == rhs.impl_.address &&
  256. impl_.port == rhs.impl_.port;
  257. }
  258. /**
  259. * \brief Tests for inequality.
  260. * \par Exception safety
  261. * No-throw guarantee.
  262. */
  263. bool operator!=(const any_address& rhs) const noexcept { return !(*this == rhs); }
  264. };
  265. } // namespace mysql
  266. } // namespace boost
  267. #endif