src/corosio/src/ip_address.cpp

100.0% Lines (14 / 14) 100.0% Functions (3 / 3)
ip_address.cpp
f(x) Functions (3)
Line TLA Hits Source Code
1 //
2 // Copyright (c) 2026 Vinnie Falco (vinnie.falco@gmail.com)
3 // Copyright (c) 2026 Steve Gerbino
4 //
5 // Distributed under the Boost Software License, Version 1.0. (See accompanying
6 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 //
8 // Official repository: https://github.com/cppalliance/corosio
9 //
10
11 #include <boost/corosio/ip_address.hpp>
12
13 #include <ostream>
14 #include <stdexcept>
15
16 namespace boost::corosio {
17
18 std::string_view
19 3x ip_address::to_buffer(char* dest, std::size_t dest_size) const
20 {
21 // Uniform capacity requirement: a v4 value in an ip_address
22 // still requires the family-independent max_str_len.
23 3x if (dest_size < max_str_len)
24 1x throw std::length_error("buffer too small for IP address");
25 2x return is_v4() ? v4_.to_buffer(dest, dest_size)
26 2x : v6_.to_buffer(dest, dest_size);
27 }
28
29 std::ostream&
30 2x operator<<(std::ostream& os, ip_address const& addr)
31 {
32 2x return addr.is_v4() ? os << addr.v4_ : os << addr.v6_;
33 }
34
35 capy::io_result<ip_address>
36 45x make_ip_address(std::string_view s) noexcept
37 {
38 45x if (auto [ec, v4] = make_ipv4_address(s); !ec)
39 13x return {std::error_code{}, ip_address(v4)};
40 32x auto [ec, v6] = make_ipv6_address(s);
41 32x if (ec)
42 11x return {ec, ip_address{}};
43 21x return {std::error_code{}, ip_address(v6)};
44 }
45
46 } // namespace boost::corosio
47