ipaddress 1.1.0
Loading...
Searching...
No Matches
ip_address_base< Base > Class Template Reference

A template base class for IP address representations. More...

#include <ip-address-base.hpp>

Inheritance diagram for ip_address_base< Base >:

Public Types

using base_type = typename Base::base_type
 Type alias for the base storage type.
 
using uint_type = typename Base::uint_type
 Type alias for the underlying unsigned integer type.
 

Public Member Functions

constexpr inline ip_address_base () noexcept
 Default constructor.
 
constexpr inline ip_address_base (const base_type &bytes) noexcept
 Constructs an IP address object from a byte array.
 
constexpr inline const uint8_t * data () const noexcept
 Retrieves the raw data representing the IP address in network byte order (big-endian).
 
constexpr inline bool is_multicast () const noexcept
 Checks if the IP address is a multicast address.
 
constexpr inline bool is_private () const noexcept
 Checks if the IP address is a private address.
 
constexpr inline bool is_global () const noexcept
 Checks if the IP address is a global address.
 
constexpr inline bool is_reserved () const noexcept
 Checks if the IP address is a reserved address.
 
constexpr inline bool is_loopback () const noexcept
 Checks if the IP address is a loopback address.
 
constexpr inline bool is_link_local () const noexcept
 Checks if the IP address is a link-local address.
 
constexpr inline bool is_unspecified () const noexcept
 Checks if the IP address is unspecified.
 
inline std::string to_string (format fmt=format::compressed) const
 Converts the IP address to a string representation.
 
inline std::wstring to_wstring (format fmt=format::compressed) const
 Converts the IP address to a string representation.
 
inline std::u16string to_u16string (format fmt=format::compressed) const
 Converts the IP address to a string representation.
 
inline std::u32string to_u32string (format fmt=format::compressed) const
 Converts the IP address to a string representation.
 
inline std::u8string to_u8string (format fmt=format::compressed) const
 Converts the IP address to a string representation.
 
constexpr inline void swap (ip_address_base &ip) noexcept
 Swaps the contents of this IP address with another IP address.
 
constexpr inline size_t hash () const noexcept
 Computes a hash value for the IP address.
 
inline std::string reverse_pointer () const
 Generates a reverse DNS lookup pointer for the IP address.
 
constexpr inline operator uint_type () const noexcept
 Converts the IP address to an unsigned integer type.
 
template<typename T >
inline operator std::basic_string< T, std::char_traits< T >, std::allocator< T > > () const
 Converts the IP address to a string.
 
constexpr inline bool operator== (const ip_address_base &rhs) const noexcept
 Checks if two IP addresses are equal.
 
constexpr inline bool operator!= (const ip_address_base &rhs) const noexcept
 Checks if two IP addresses are not equal.
 
constexpr inline bool operator< (const ip_address_base &rhs) const noexcept
 Checks if one IP address is less than another.
 
constexpr inline bool operator> (const ip_address_base &rhs) const noexcept
 Checks if one IP address is greater than another.
 
constexpr inline bool operator<= (const ip_address_base &rhs) const noexcept
 Checks if one IP address is less than or equal to another.
 
constexpr inline bool operator>= (const ip_address_base &rhs) const noexcept
 Checks if one IP address is greater than or equal to another.
 

Static Public Member Functions

static constexpr inline ip_address_base from_bytes (const base_type &bytes) noexcept
 Static factory method to create an IP address object from a byte array.
 
static constexpr inline ip_address_base from_bytes (const uint8_t *bytes, size_t byte_count) noexcept
 Static factory method to create an IP address object from a raw byte buffer.
 
template<fixed_string FixedString>
static consteval inline ip_address_base< Base > parse () noexcept
 Static method template to parse an IP address from a fixed string at compile time.
 
static constexpr inline ip_address_base parse (std::string_view address)
 Parses an IP address from a string view.
 
static constexpr inline ip_address_base parse (std::wstring_view address)
 Parses an IP address from a wide string view.
 
static constexpr inline ip_address_base parse (std::u8string_view address)
 Parses an IP address from a UTF-8 string view.
 
static constexpr inline ip_address_base parse (std::u16string_view address)
 Parses an IP address from a UTF-16 string view.
 
static constexpr inline ip_address_base parse (std::u32string_view address)
 Parses an IP address from a UTF-32 string view.
 
static constexpr inline ip_address_base parse (std::string_view address, error_code &code) noexcept
 Parses an IP address from a string view and reports errors through an error code.
 
static constexpr inline ip_address_base parse (std::wstring_view address, error_code &code) noexcept
 Parses an IP address from a wide string view and reports errors through an error code.
 
static constexpr inline ip_address_base parse (std::u8string_view address, error_code &code) noexcept
 Parses an IP address from a UTF-8 string view and reports errors through an error code.
 
static constexpr inline ip_address_base parse (std::u16string_view address, error_code &code) noexcept
 Parses an IP address from a UTF-16 string view and reports errors through an error code.
 
static constexpr inline ip_address_base parse (std::u32string_view address, error_code &code) noexcept
 Parses an IP address from a UTF-32 string view and reports errors through an error code.
 
template<typename T , size_t N>
static constexpr inline ip_address_base parse (const T(&address)[N])
 Parses an IP address from a character array.
 
template<typename T , size_t N>
static constexpr inline ip_address_base parse (const T(&address)[N], error_code &code) noexcept
 Parses an IP address from a character array and reports errors through an error code.
 

Detailed Description

template<typename Base>
class ipaddress::ip_address_base< Base >

A template base class for IP address representations.

This class template serves as a base for creating IP address objects. It inherits from a base class that provides the necessary functionality, and it is extended by more specific IP address classes.

Template Parameters
BaseThe base class providing storage and low-level IP address functionalities.

Constructor & Destructor Documentation

◆ ip_address_base() [1/2]

template<typename Base >
constexpr inline ip_address_base ( )
inlinenoexcept

Default constructor.

Constructs an empty IP address object.

◆ ip_address_base() [2/2]

template<typename Base >
constexpr inline ip_address_base ( const base_type & bytes)
inlineexplicitnoexcept

Constructs an IP address object from a byte array.

Parameters
[in]bytesThe byte array representing an IP address.

Member Function Documentation

◆ from_bytes() [1/2]

template<typename Base >
static constexpr inline ip_address_base from_bytes ( const base_type & bytes)
inlinestaticnoexcept

Static factory method to create an IP address object from a byte array.

Parameters
[in]bytesThe byte array representing an IP address.
Returns
An instance of ip address constructed from the given byte array.

◆ from_bytes() [2/2]

template<typename Base >
static constexpr inline ip_address_base from_bytes ( const uint8_t * bytes,
size_t byte_count )
inlinestaticnoexcept

Static factory method to create an IP address object from a raw byte buffer.

Parameters
[in]bytesPointer to the raw byte buffer representing an IP address.
[in]byte_countThe number of bytes in the buffer to use.
Returns
An instance of ip address constructed from the given byte buffer.
Remarks
If the number of bytes byte_count is less than the target number . of bytes to represent the IP address, the missing bytes will be filled with zeros.
Remarks
If the number of bytes byte_count is greater than the target . number of bytes represented by the IP address, then the extra bytes will be ignored

◆ parse() [1/13]

template<typename Base >
template<fixed_string FixedString>
static consteval inline ip_address_base< Base > parse ( )
inlinestaticnoexcept

Static method template to parse an IP address from a fixed string at compile time.

Template Parameters
FixedStringA non-type template parameter that holds the string representation of the IP address.
Returns
An instance of ip address parsed from the fixed string.
Note
This method is only available when non-type template parameters for strings are supported.
Remarks
If parsing fails, an error will be raised at compile time.

◆ parse() [2/13]

template<typename Base >
static constexpr inline ip_address_base parse ( std::string_view address)
inlinestatic

Parses an IP address from a string view.

This method provides a way to parse an IP address from a string view, which is a non-owning reference to a sequence of characters.

Parameters
[in]addressThe string view containing the IP address to parse.
Returns
An instance of ip address parsed from the string view.
Exceptions
parse_errorException caused by invalid input string.
Note
This method is available for C++17 and later versions.
Remarks
For C++ versions prior to C++17, member functions with std::string and C-strings will be used instead.

◆ parse() [3/13]

template<typename Base >
static constexpr inline ip_address_base parse ( std::wstring_view address)
inlinestatic

Parses an IP address from a wide string view.

This method allows for parsing an IP address from a wide string view, which is typically used for wide character strings.

Parameters
[in]addressThe wide string view containing the IP address to parse.
Returns
An instance of ip address parsed from the string view.
Exceptions
parse_errorException caused by invalid input string.
Note
This method is available for C++17 and later versions.
Remarks
For C++ versions prior to C++17, member functions with std::wstring and C-strings will be used instead.

◆ parse() [4/13]

template<typename Base >
static constexpr inline ip_address_base parse ( std::u8string_view address)
inlinestatic

Parses an IP address from a UTF-8 string view.

This method parses an IP address from a given UTF-8 string view. It leverages the char8_t type introduced in C++20 to handle UTF-8 strings natively.

Parameters
[in]addressA UTF-8 string view containing the IP address to parse.
Returns
An instance of ip address parsed from the string view.
Exceptions
parse_errorException caused by invalid input string.
Note
This method is available for C++20 and later versions where char8_t is supported.

◆ parse() [5/13]

template<typename Base >
static constexpr inline ip_address_base parse ( std::u16string_view address)
inlinestatic

Parses an IP address from a UTF-16 string view.

This method is designed to parse an IP address from a UTF-16 string view, which is a sequence of 16-bit characters.

Parameters
[in]addressThe UTF-16 string view containing the IP address to parse.
Returns
An instance of ip address parsed from the string view.
Exceptions
parse_errorException caused by invalid input string.
Note
This method is available for C++17 and later versions.
Remarks
For C++ versions prior to C++17, member functions with std::u16string and C-strings will be used instead.

◆ parse() [6/13]

template<typename Base >
static constexpr inline ip_address_base parse ( std::u32string_view address)
inlinestatic

Parses an IP address from a UTF-32 string view.

This method allows for parsing an IP address from a UTF-32 string view, which is a sequence of 32-bit characters.

Parameters
[in]addressThe UTF-32 string view containing the IP address to parse.
Returns
An instance of ip address parsed from the string view.
Exceptions
parse_errorException caused by invalid input string.
Note
This method is available for C++17 and later versions.
Remarks
For C++ versions prior to C++17, member functions with std::u32string and C-strings will be used instead.

◆ parse() [7/13]

template<typename Base >
static constexpr inline ip_address_base parse ( std::string_view address,
error_code & code )
inlinestaticnoexcept

Parses an IP address from a string view and reports errors through an error code.

This method parses an IP address from a string view and provides an error code if the parsing fails.

Parameters
[in]addressThe string view containing the IP address to parse.
[out]codeA reference to an error_code object that will be set if an error occurs during parsing.
Returns
An instance of ip address parsed from the string view. If parsing fails, the returned object will be in an unspecified state.
Note
This method is available for C++17 and later versions.
Remarks
For C++ versions prior to C++17, member functions with std::string and C-strings will be used instead.

◆ parse() [8/13]

template<typename Base >
static constexpr inline ip_address_base parse ( std::wstring_view address,
error_code & code )
inlinestaticnoexcept

Parses an IP address from a wide string view and reports errors through an error code.

This method parses an IP address from a wide string view and provides an error code if the parsing fails.

Parameters
[in]addressThe wide string view containing the IP address to parse.
[out]codeA reference to an error_code object that will be set if an error occurs during parsing.
Returns
An instance of ip address parsed from the wide string view. If parsing fails, the returned object will be in an unspecified state.
Note
This method is available for C++17 and later versions.
Remarks
For C++ versions prior to C++17, member functions with std::wstring and C-strings will be used instead.

◆ parse() [9/13]

template<typename Base >
static constexpr inline ip_address_base parse ( std::u8string_view address,
error_code & code )
inlinestaticnoexcept

Parses an IP address from a UTF-8 string view and reports errors through an error code.

This method parses an IP address from a UTF-8 string view and provides an error code if the parsing fails.

Parameters
[in]addressThe UTF-8 string view containing the IP address to parse.
[out]codeA reference to an error_code object that will be set if an error occurs during parsing.
Returns
An instance of ip address parsed from the UTF-8 string view. If parsing fails, the returned object will be in an unspecified state.
Note
This method is available for C++20 and later versions where char8_t is supported.

◆ parse() [10/13]

template<typename Base >
static constexpr inline ip_address_base parse ( std::u16string_view address,
error_code & code )
inlinestaticnoexcept

Parses an IP address from a UTF-16 string view and reports errors through an error code.

This method parses an IP address from a UTF-16 string view and provides an error code if the parsing fails.

Parameters
[in]addressThe UTF-16 string view containing the IP address to parse.
[out]codeA reference to an error_code object that will be set if an error occurs during parsing.
Returns
An instance of ip address parsed from the UTF-16 string view. If parsing fails, the returned object will be in an unspecified state.
Note
This method is available for C++17 and later versions.
Remarks
For C++ versions prior to C++17, member functions with std::u16string and C-strings will be used instead.

◆ parse() [11/13]

template<typename Base >
static constexpr inline ip_address_base parse ( std::u32string_view address,
error_code & code )
inlinestaticnoexcept

Parses an IP address from a UTF-32 string view and reports errors through an error code.

This method parses an IP address from a UTF-32 string view and provides an error code if the parsing fails.

Parameters
[in]addressThe UTF-32 string view containing the IP address to parse.
[out]codeA reference to an error_code object that will be set if an error occurs during parsing.
Returns
An instance of ip address parsed from the UTF-32 string view. If parsing fails, the returned object will be in an unspecified state.
Note
This method is available for C++17 and later versions.
Remarks
For C++ versions prior to C++17, member functions with std::u32string and C-strings will be used instead.

◆ parse() [12/13]

template<typename Base >
template<typename T , size_t N>
static constexpr inline ip_address_base parse ( const T(&) address[N])
inlinestatic

Parses an IP address from a character array.

This method template parses an IP address from a character array of a specified size. Can check and get the result at compile time.

Template Parameters
TThe character type of the array.
NThe size of the character array.
Parameters
[in]addressThe character array containing the IP address to parse.
Returns
An instance of ip address parsed from the character array.

◆ parse() [13/13]

template<typename Base >
template<typename T , size_t N>
static constexpr inline ip_address_base parse ( const T(&) address[N],
error_code & code )
inlinestaticnoexcept

Parses an IP address from a character array and reports errors through an error code.

This method template parses an IP address from a character array of a specified size and provides an error code if the parsing fails.

Template Parameters
TThe character type of the array.
NThe size of the character array.
Parameters
[in]addressThe character array containing the IP address to parse.
[out]codeA reference to an error_code object that will be set if an error occurs during parsing.
Returns
An instance of ip address parsed from the character array. If parsing fails, the returned object will be in an unspecified state.

◆ data()

template<typename Base >
constexpr inline const uint8_t * data ( ) const
inlinenoexcept

Retrieves the raw data representing the IP address in network byte order (big-endian).

This method returns a pointer to the underlying byte array that stores the IP address.

Returns
A pointer to the constant byte array containing the raw IP address data.

◆ is_multicast()

template<typename Base >
constexpr inline bool is_multicast ( ) const
noexcept

Checks if the IP address is a multicast address.

Returns
true if the IP address is reserved for multicast use, false otherwise.
See also
RFC 3171 for IPv4.
RFC 2373 for IPv6.

◆ is_private()

template<typename Base >
constexpr inline bool is_private ( ) const
noexcept

Checks if the IP address is a private address.

Returns
true if the IP address is allocated for private networks, false otherwise.
See also
iana-ipv4-special-registry.
iana-ipv6-special-registry.

◆ is_global()

template<typename Base >
constexpr inline bool is_global ( ) const
noexcept

Checks if the IP address is a global address.

Returns
true if the IP address is allocated for public networks, false otherwise.
See also
iana-ipv4-special-registry.
iana-ipv6-special-registry.

◆ is_reserved()

template<typename Base >
constexpr inline bool is_reserved ( ) const
noexcept

Checks if the IP address is a reserved address.

Returns
true if the IP address is otherwise IETF reserved, false otherwise.

◆ is_loopback()

template<typename Base >
constexpr inline bool is_loopback ( ) const
noexcept

Checks if the IP address is a loopback address.

Returns
true if the IP address is a loopback address, false otherwise.
See also
RFC 3330 for IPv4.
RFC 2373 for IPv6.

◆ is_link_local()

template<typename Base >
constexpr inline bool is_link_local ( ) const
noexcept

Checks if the IP address is a link-local address.

Returns
true if the IP address is reserved for link-local usage, false otherwise.
See also
RFC 3927.

◆ is_unspecified()

template<typename Base >
constexpr inline bool is_unspecified ( ) const
inlinenoexcept

Checks if the IP address is unspecified.

An unspecified IP address is an address with all bits set to zero. In IPv4, this is represented as 0.0.0.0, and in IPv6, as ::. This type of address is used to indicate the absence of an address.

Returns
true if the IP address is unspecified (all bits are zero), false otherwise.
See also
RFC 5735 for IPv4.
RFC 2373 for IPv6.

◆ to_string()

template<typename Base >
inline std::string to_string ( format fmt = format::compressed) const
inline

Converts the IP address to a string representation.

The function converts the binary representation of the IP address to a string. The format of the output string can be adjusted by passing the desired format as an argument. The default format is 'compressed'.

Parameters
[in]fmtThe format to use for the string representation. Defaults to format::compressed.
Returns
A std::string representing the IP address in the specified format.

◆ to_wstring()

template<typename Base >
inline std::wstring to_wstring ( format fmt = format::compressed) const
inline

Converts the IP address to a string representation.

The function converts the binary representation of the IP address to a string. The format of the output string can be adjusted by passing the desired format as an argument. The default format is 'compressed'.

Parameters
[in]fmtThe format to use for the string representation. Defaults to format::compressed.
Returns
A std::wstring representing the IP address in the specified format.

◆ to_u16string()

template<typename Base >
inline std::u16string to_u16string ( format fmt = format::compressed) const
inline

Converts the IP address to a string representation.

The function converts the binary representation of the IP address to a string. The format of the output string can be adjusted by passing the desired format as an argument. The default format is 'compressed'.

Parameters
[in]fmtThe format to use for the string representation. Defaults to format::compressed.
Returns
A std::u16string representing the IP address in the specified format.

◆ to_u32string()

template<typename Base >
inline std::u32string to_u32string ( format fmt = format::compressed) const
inline

Converts the IP address to a string representation.

The function converts the binary representation of the IP address to a string. The format of the output string can be adjusted by passing the desired format as an argument. The default format is 'compressed'.

Parameters
[in]fmtThe format to use for the string representation. Defaults to format::compressed.
Returns
A std::u32string representing the IP address in the specified format.

◆ to_u8string()

template<typename Base >
inline std::u8string to_u8string ( format fmt = format::compressed) const
inline

Converts the IP address to a string representation.

The function converts the binary representation of the IP address to a string. The format of the output string can be adjusted by passing the desired format as an argument. The default format is 'compressed'.

Parameters
[in]fmtThe format to use for the string representation. Defaults to format::compressed.
Returns
A std::u8string representing the IP address in the specified format.

◆ swap()

template<typename Base >
constexpr inline void swap ( ip_address_base< Base > & ip)
inlinenoexcept

Swaps the contents of this IP address with another IP address.

This function swaps the underlying bytes representing the IP address with those of another IP address.

Parameters
[in]ipThe other IP address to swap with.

◆ hash()

template<typename Base >
constexpr inline size_t hash ( ) const
inlinenoexcept

Computes a hash value for the IP address.

This function generates a hash value that can be used to uniquely identify the IP address. It can be useful when IP addresses are used as keys in hash tables.

Returns
A size_t hash value of the IP address.

◆ reverse_pointer()

template<typename Base >
inline std::string reverse_pointer ( ) const
inline

Generates a reverse DNS lookup pointer for the IP address.

This function creates a string that is the reverse DNS lookup pointer of the IP address. It is commonly used in reverse DNS lookups, where the IP address is reversed and appended with .in-addr.arpa for IPv4, or .ip6.arpa for IPv6, to form a domain name that can be looked up in the DNS system.

The name of the reverse DNS PTR record for the IP address, e.g.:

std::cout << ipv4_address::parse("127.0.0.1").reverse_pointer() << std::endl;
std::cout << ipv6_address::parse("2001:db8::1").reverse_pointer() << std::endl;
// out:
// 1.0.0.127.in-addr.arpa
// 1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa
inline std::string reverse_pointer() const
Generates a reverse DNS lookup pointer for the IP address.
Definition ip-address-base.hpp:634
static consteval inline ip_address_base< ipv4_address_base > parse() noexcept
Static method template to parse an IP address from a fixed string at compile time.
Definition ip-address-base.hpp:123
Returns
A std::string that is the reverse DNS lookup pointer of the IP address.

◆ operator uint_type()

template<typename Base >
constexpr inline operator uint_type ( ) const
inlineexplicitnoexcept

Converts the IP address to an unsigned integer type.

This operator allows the IP address to be used as an unsigned integer type. It can be useful when a numerical representation of the IP address is needed.

Returns
An unsigned integer representation of the IP address.
Remarks
Bytes in integer are presented in host byte order.

◆ operator std::basic_string< T, std::char_traits< T >, std::allocator< T > >()

template<typename Base >
template<typename T >
inline operator std::basic_string< T, std::char_traits< T >, std::allocator< T > > ( ) const
inlineexplicit

Converts the IP address to a string.

This operator allows the IP address to be converted to a string.

Template Parameters
TThe character type of the string.
Returns
A string representation of the IP address.

◆ operator==()

template<typename Base >
constexpr inline bool operator== ( const ip_address_base< Base > & rhs) const
inlinenoexcept

Checks if two IP addresses are equal.

This operator compares the binary representation of two IP addresses to determine if they are equal.

Parameters
[in]rhsThe right-hand side IP address for comparison.
Returns
true if both IP addresses are equal, false otherwise.

◆ operator!=()

template<typename Base >
constexpr inline bool operator!= ( const ip_address_base< Base > & rhs) const
inlinenoexcept

Checks if two IP addresses are not equal.

This operator compares the binary representation of two IP addresses to determine if they are not equal.

Parameters
[in]rhsThe right-hand side IP address for comparison.
Returns
true if both IP addresses are not equal, false otherwise.

◆ operator<()

template<typename Base >
constexpr inline bool operator< ( const ip_address_base< Base > & rhs) const
inlinenoexcept

Checks if one IP address is less than another.

This operator compares the binary representation of two IP addresses to determine if the left-hand side is less than the right-hand side.

Parameters
[in]rhsThe right-hand side IP address for comparison.
Returns
true if the left-hand side IP address is less than the right-hand side, false otherwise.

◆ operator>()

template<typename Base >
constexpr inline bool operator> ( const ip_address_base< Base > & rhs) const
inlinenoexcept

Checks if one IP address is greater than another.

This operator compares the binary representation of two IP addresses to determine if the left-hand side is greater than the right-hand side.

Parameters
[in]rhsThe right-hand side IP address for comparison.
Returns
true if the left-hand side IP address is greater than the right-hand side, false otherwise.

◆ operator<=()

template<typename Base >
constexpr inline bool operator<= ( const ip_address_base< Base > & rhs) const
inlinenoexcept

Checks if one IP address is less than or equal to another.

This operator compares the binary representation of two IP addresses to determine if the left-hand side is less than or equal to the right-hand side.

Parameters
[in]rhsThe right-hand side IP address for comparison.
Returns
true if the left-hand side IP address is less than or equal to the right-hand side, false otherwise.

◆ operator>=()

template<typename Base >
constexpr inline bool operator>= ( const ip_address_base< Base > & rhs) const
inlinenoexcept

Checks if one IP address is greater than or equal to another.

This operator compares the binary representation of two IP addresses to determine if the left-hand side is greater than or equal to the right-hand side.

Parameters
[in]rhsThe right-hand side IP address for comparison.
Returns
true if the left-hand side IP address is greater than or equal to the right-hand side, false otherwise.

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