io
C++ I/O scheduling library with asynchronous socket operations
Loading...
Searching...
No Matches
Public Member Functions | Related Symbols | List of all members
io::socket::socket_address Class Reference

A platform-independent representation of a socket address. More...

#include <socket/socket_address.hpp>

Public Member Functions

 socket_address ()=default
 Constructs an empty socket address.
 
 socket_address (const socket_address &other)=default
 Default copy constructor.
 
 socket_address (socket_address &&other) noexcept=default
 Default move constructor.
 
auto operator= (const socket_address &other) -> socket_address &=default
 Default copy assignment operator.
 
auto operator= (socket_address &&other) noexcept -> socket_address &=default
 Default move assignment operator.
 
 socket_address (socklen_type size) noexcept
 Constructs a socket address with a specific size.
 
 socket_address (const sockaddr_type *addr, socklen_type size) noexcept
 Constructs a socket_address from a native socket address structure.
 
auto data () noexcept -> sockaddr_type *
 Returns a mutable pointer to the underlying socket address data.
 
auto data () const noexcept -> const sockaddr_type *
 Returns a constant pointer to the underlying socket address data.
 
auto size () noexcept -> socklen_type *
 Returns a mutable pointer to the size of the socket address.
 
auto size () const noexcept -> const socklen_type *
 Returns a constant pointer to the size of the socket address.
 
auto operator== (const socket_address &other) const noexcept -> bool
 Compares two socket_address objects for equality.
 
 ~socket_address ()=default
 Default destructor.
 

Related Symbols

(Note that these are not member symbols.)

template<typename SockAddr >
auto make_address (const SockAddr *addr=nullptr) noexcept -> socket_address
 Creates a socket_address object, optionally initialized from a native address.
 

Detailed Description

A platform-independent representation of a socket address.

This class provides a wrapper around the native socket address structures, offering a safe and convenient way to manage address information for socket operations. It uses sockaddr_storage_type to ensure sufficient space for any address family and socklen_type for the address size, which are platform-specific aliases.

Definition at line 42 of file socket_address.hpp.

Constructor & Destructor Documentation

◆ socket_address() [1/3]

io::socket::socket_address::socket_address ( )
default

Constructs an empty socket address.

The internal storage is zero-initialized, and the size is set to the full capacity of the address storage. This prepares the object to be populated by functions that require a buffer for the address, such as accept() or recvfrom().

◆ socket_address() [2/3]

io::socket::socket_address::socket_address ( socklen_type  size)
explicitnoexcept

Constructs a socket address with a specific size.

This constructor initializes the socket address to be ready to hold a socket address of the specified size. The internal storage is zero-initialized, and the size is set to the provided value. This is useful when you know the exact size needed for a particular address family.

Parameters
sizeThe size in bytes for the socket address structure.

Definition at line 23 of file socket_address.cpp.

◆ socket_address() [3/3]

io::socket::socket_address::socket_address ( const sockaddr_type addr,
socklen_type  size 
)
explicitnoexcept

Constructs a socket_address from a native socket address structure.

This constructor copies the provided native socket address data into its internal storage, making it suitable for passing to socket functions that require an address, such as connect() or bind().

Parameters
addrA pointer to the native socket address (e.g., sockaddr_in). The data pointed to is copied into the object.
sizeThe size of the address structure in bytes.

Definition at line 27 of file socket_address.cpp.

Member Function Documentation

◆ data() [1/2]

auto io::socket::socket_address::data ( ) const -> const sockaddr_type *
noexcept

Returns a constant pointer to the underlying socket address data.

This provides read-only access to the address data, suitable for passing to functions like connect() or bind() that do not modify the address. The lifetime of the returned pointer is tied to this socket_address object.

Returns
A non-owning constant pointer to the sockaddr_type data.

Definition at line 41 of file socket_address.cpp.

◆ data() [2/2]

auto io::socket::socket_address::data ( ) -> sockaddr_type *
noexcept

Returns a mutable pointer to the underlying socket address data.

This allows the raw address data to be modified by functions like accept() or getpeername() that populate a socket address structure provided by the caller. The lifetime of the returned pointer is tied to this socket_address object.

Returns
A non-owning pointer to the sockaddr_type data.

Definition at line 36 of file socket_address.cpp.

◆ operator==()

auto io::socket::socket_address::operator== ( const socket_address other) const -> bool
noexcept

Compares two socket_address objects for equality.

Two addresses are considered equal if they have the same size and their underlying address data is bit-for-bit identical.

Parameters
otherThe socket_address to compare against.
Returns
true if the addresses are equal, false otherwise.

Definition at line 52 of file socket_address.cpp.

◆ size() [1/2]

auto io::socket::socket_address::size ( ) const -> const socklen_type *
noexcept

Returns a constant pointer to the size of the socket address.

This provides read-only access to the size, suitable for functions that only need to know the size of the address being passed. The lifetime of the returned pointer is tied to this socket_address object.

Returns
A non-owning constant pointer to the socklen_type size.

Definition at line 48 of file socket_address.cpp.

◆ size() [2/2]

auto io::socket::socket_address::size ( ) -> socklen_type *
noexcept

Returns a mutable pointer to the size of the socket address.

This allows the size to be modified by functions like accept() or getpeername() that update the size argument to reflect the actual size of the returned address. The lifetime of the returned pointer is tied to this socket_address object.

Returns
A non-owning pointer to the socklen_type size.

Definition at line 46 of file socket_address.cpp.

Friends And Related Symbol Documentation

◆ make_address()

template<typename SockAddr >
auto make_address ( const SockAddr *  addr = nullptr) -> socket_address
related

Creates a socket_address object, optionally initialized from a native address.

This function is a helper to construct a socket_address instance. If addr is provided, the socket_address is initialized with the given native address and its size. Otherwise, it creates a socket_address with an appropriate size for SockAddr but without initial address data. It performs a compile-time assertion to guarantee that SockAddr can fit within sockaddr_storage_type.

Template Parameters
SockAddrThe native socket address structure type (e.g., sockaddr_in, sockaddr_in6).
Parameters
addrAn optional pointer to a native socket address structure to initialize from. If nullptr, an empty socket_address of the appropriate size is created.
Returns
A socket_address object, either initialized from addr or with the size of SockAddr.

Definition at line 180 of file socket_address.hpp.


The documentation for this class was generated from the following files: