src/corosio/src/local_endpoint.cpp

100.0% Lines (18 / 18) 100.0% Functions (3 / 3)
local_endpoint.cpp
f(x) Functions (3)
Line TLA Hits Source Code
1 //
2 // Copyright (c) 2026 Michael Vandeberg
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 // Official repository: https://github.com/cppalliance/corosio
8 //
9
10 #include <boost/corosio/local_endpoint.hpp>
11
12 #include <cstring>
13 #include <ostream>
14 #include <system_error>
15
16 namespace boost::corosio {
17
18 508x local_endpoint::local_endpoint(from_path_t, std::string_view path) noexcept
19 {
20 // An empty view may carry a null data pointer, which memcpy rejects.
21 508x if (!path.empty())
22 506x std::memcpy(path_, path.data(), path.size());
23 508x len_ = static_cast<std::uint8_t>(path.size());
24 508x }
25
26 capy::results<local_endpoint>
27 511x make_local_endpoint(std::string_view path) noexcept
28 {
29 511x if (path.size() > local_endpoint::max_path_length)
30 {
31 return {
32 3x std::make_error_code(std::errc::filename_too_long),
33 3x local_endpoint{}};
34 }
35 return {
36 508x std::error_code{}, local_endpoint(local_endpoint::from_path_t{}, path)};
37 }
38
39 std::ostream&
40 6x operator<<(std::ostream& os, local_endpoint const& ep)
41 {
42 6x if (ep.empty())
43 2x return os;
44 4x if (ep.is_abstract())
45 {
46 // Skip the leading null byte; print the rest as the name
47 2x os << "[abstract:" << std::string_view(ep.path_ + 1, ep.len_ - 1)
48 2x << ']';
49 }
50 else
51 {
52 2x os << ep.path();
53 }
54 4x return os;
55 }
56
57 } // namespace boost::corosio
58