io
C++ I/O scheduling library with asynchronous socket operations
Loading...
Searching...
No Matches
socket_address.hpp
Go to the documentation of this file.
1/* Copyright 2025 Kevin Exton
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
21#pragma once
22#ifndef IO_SOCKET_ADDRESS_HPP
23#define IO_SOCKET_ADDRESS_HPP
24#include <boost/predef.h>
25
26#if BOOST_OS_WINDOWS
28#else
30#endif
31
32namespace io::socket {
43
44public:
53 socket_address() = default;
54
56 socket_address(const socket_address &other) = default;
57
59 socket_address(socket_address &&other) noexcept = default;
60
62 auto operator=(const socket_address &other) -> socket_address & = default;
63
65 auto operator=(socket_address &&other) noexcept -> socket_address & = default;
66
77 explicit socket_address(socklen_type size) noexcept;
78
91 explicit socket_address(const sockaddr_type *addr,
92 socklen_type size) noexcept;
93
104 [[nodiscard]] auto data() noexcept -> sockaddr_type *;
105
115 [[nodiscard]] auto data() const noexcept -> const sockaddr_type *;
116
127 [[nodiscard]] auto size() noexcept -> socklen_type *;
128
138 [[nodiscard]] auto size() const noexcept -> const socklen_type *;
139
149 auto operator==(const socket_address &other) const noexcept -> bool;
150
152 ~socket_address() = default;
153
154private:
155 sockaddr_storage_type storage_{};
156 socklen_type size_{sizeof(storage_)};
157};
158
179template <typename SockAddr>
180auto make_address(const SockAddr *addr = nullptr) noexcept -> socket_address {
181 static_assert(sizeof(SockAddr) <= sizeof(sockaddr_storage_type),
182 "SockAddr must fit into sockaddr_storage_type.");
183
184 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
185 return addr ? socket_address{reinterpret_cast<const sockaddr_type *>(addr),
186 sizeof(SockAddr)}
187 : socket_address{sizeof(SockAddr)};
188}
189
190} // namespace io::socket
191
192#endif // IO_SOCKET_ADDRESS_HPP
A platform-independent representation of a socket address.
auto data() noexcept -> sockaddr_type *
Returns a mutable pointer to the underlying socket address data.
auto size() noexcept -> socklen_type *
Returns a mutable pointer to the size of the socket address.
socket_address(const socket_address &other)=default
Default copy constructor.
socket_address()=default
Constructs an empty socket address.
auto operator=(socket_address &&other) noexcept -> socket_address &=default
Default move assignment operator.
auto make_address(const SockAddr *addr=nullptr) noexcept -> socket_address
Creates a socket_address object, optionally initialized from a native address.
auto operator=(const socket_address &other) -> socket_address &=default
Default copy assignment operator.
socket_address(socket_address &&other) noexcept=default
Default move constructor.
The io::socket namespace provides a cross-platform abstraction for socket-level I/O operations.
Definition socket.hpp:31
sockaddr sockaddr_type
The generic socket address structure for POSIX systems.
Definition socket.hpp:137
socklen_t socklen_type
The type used to represent socket-related sizes on POSIX systems.
Definition socket.hpp:155
sockaddr_storage sockaddr_storage_type
The socket address storage structure for POSIX systems.
Definition socket.hpp:146
This file contains the POSIX-specific socket definitions and functions.
This file contains the Windows-specific socket definitions and functions.