7#if !defined(ASYNC_MQTT_HOST_PORT_HPP)
8#define ASYNC_MQTT_HOST_PORT_HPP
12#include <boost/lexical_cast.hpp>
13#include <boost/asio/ip/address.hpp>
15#include <async_mqtt/util/move.hpp>
16#include <async_mqtt/util/optional.hpp>
17#include <async_mqtt/log.hpp>
21namespace as = boost::asio;
24 host_port(std::string host, std::uint16_t port)
25 :host{force_move(host)},
32inline std::string to_string(host_port
const& hp) {
33 return hp.host +
':' + std::to_string(hp.port);
36inline std::ostream& operator<<(std::ostream& s, host_port
const& hp) {
41inline bool operator==(host_port
const& lhs, host_port
const& rhs) {
42 return std::tie(lhs.host, lhs.port) == std::tie(rhs.host, rhs.port);
45inline bool operator!=(host_port
const& lhs, host_port
const& rhs) {
49inline optional<host_port> host_port_from_string(std::string_view str) {
51 auto last_colon = str.find_last_of(
':');
52 if (last_colon == std::string::npos)
return nullopt;
56 auto port_size = std::size_t(str.size() - (last_colon + 1));
57 port = boost::lexical_cast<std::uint16_t>(
58 std::string_view(&str[last_colon + 1], port_size)
60 str.remove_suffix(port_size + 1);
62 catch (std::exception
const& e) {
63 ASYNC_MQTT_LOG(
"mqtt_api", error)
64 <<
"invalid host port string:" << str
69 if (str.empty())
return nullopt;
70 if (str.front() ==
'[' && str.back() ==
']') {
74 if (str.empty())
return nullopt;
77 return host_port{std::string(str), port};