AsyncBerkeley
Asynchronous Berkeley sockets. Simple.
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 "detail/socket.hpp"
26#include "socket_option.hpp"
27
28namespace io::socket {
35template <SocketAddress Addr = sockaddr_storage_type>
36struct socket_address : public socket_option<Addr> {
44 using Base::Base;
45
53 template <socklen_type Size = sizeof(Addr)>
54 requires(Size <= sizeof(Addr))
55 socket_address(const sockaddr_type *addr, socklen_type size = Size) noexcept
56 : Base(std::span<const std::byte, Size>(
57 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
58 reinterpret_cast<const std::byte *>(addr), size))
59 {}
60
67 template <SocketAddress OtherAddr>
69 : Base(std::span<const std::byte, sizeof(OtherAddr)>(other))
70 {}
71};
72
79template <SocketAddress Addr = sockaddr_storage_type>
80auto make_address(const Addr *addr = nullptr) -> socket_address<Addr>
81{
82 if (!addr)
83 return {};
84
85 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
86 return {reinterpret_cast<const sockaddr_type *>(addr), sizeof(Addr)};
87}
88
89} // namespace io::socket
90#endif // IO_SOCKET_ADDRESS_HPP
A generic wrapper for socket options.
Definition socket_option.hpp:36
This file defines concepts for the execution components.
The io::socket namespace provides a cross-platform abstraction for socket-level I/O operations.
Definition poll_multiplexer.hpp:36
auto make_address(const Addr *addr=nullptr) -> socket_address< Addr >
Creates a socket_address from a socket address structure.
Definition socket_address.hpp:80
Defines the socket_option wrapper for native socket options.
Represents a platform-independent socket address.
Definition socket_address.hpp:36
socket_address(const sockaddr_type *addr, socklen_type size=Size) noexcept
Constructs a socket_address from a raw socket address structure.
Definition socket_address.hpp:55
socket_option< Addr > Base
The base class for the socket address.
Definition socket_address.hpp:40
socket_address(const socket_address< OtherAddr > &other) noexcept
Constructs a socket_address from another socket_address.
Definition socket_address.hpp:68