ipaddress 1.1.0
Loading...
Searching...
No Matches
ipv4-address.hpp
Go to the documentation of this file.
1/**
2 * @file ipv4-address.hpp
3 * @brief Provides a set of functions and classes for handling IPv4 addresses
4 * @author Vladimir Shaleev
5 * @copyright MIT License
6 *
7 * Includes functionalities to convert IPv4 addresses to and from various formats,
8 * perform comparisons, and query specific properties of the addresses.
9 * It serves as a foundational component for network applications that require manipulation
10 * and analysis of IPv4 address data.
11 */
12
13#ifndef IPADDRESS_IPV4_ADDRESS_HPP
14#define IPADDRESS_IPV4_ADDRESS_HPP
15
16#include "base-v4.hpp"
17
18namespace IPADDRESS_NAMESPACE {
19
20/**
21 * Represents the base class for IPv4 address manipulation.
22 *
23 * This class provides the basic functionalities required for handling IPv4 addresses,
24 * including conversion to and from numeric representations, access to the underlying bytes,
25 * and utility functions that are common across different representations of IPv4 addresses.
26 */
28public:
29 using base_type = typename base_v4<ipv4_address_base>::base_type; /**< The base type for the IPv4 address. */
30 using uint_type = typename base_v4<ipv4_address_base>::uint_type; /**< The unsigned integer type for the IPv4 address. */
31
32 /**
33 * Creates an IPv4 address from an unsigned integer using a template parameter.
34 *
35 * @tparam Ip The unsigned integer representing the IPv4 address.
36 * @return An instance of ip address representing the IPv4 address.
37 */
38 template <uint_type Ip>
40 return from_uint(Ip);
41 }
42
43 /**
44 * Creates an IPv4 address from an unsigned integer.
45 *
46 * @param[in] ip The unsigned integer representing the IPv4 address.
47 * @return An instance of ip address representing the IPv4 address.
48 * @remark Bytes in integer \a ip must be presented in **host byte order**.
49 */
51 return ip_from_uint32(ip);
52 }
53
54 /**
55 * Converts the IPv4 address to an unsigned integer.
56 *
57 * @return The unsigned integer representation of the IPv4 address.
58 * @remark Bytes in integer are presented in **host byte order**.
59 */
61 return ip_to_uint32(_bytes);
62 }
63
64 /**
65 * Provides access to the underlying bytes of the IPv4 address.
66 *
67 * @return A reference to the base type containing the bytes of the IPv4 address.
68 * @remark Retrieves the data representing the IP address in **network byte order** (big-endian).
69 */
71 return _bytes;
72 }
73
74protected:
75 IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE ipv4_address_base() IPADDRESS_NOEXCEPT = default;
76
77 IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE explicit ipv4_address_base(const base_type& bytes) IPADDRESS_NOEXCEPT : _bytes(bytes) {
78 }
79
81 lhs._bytes.swap(rhs._bytes);
82 }
83
84 IPADDRESS_NODISCARD static IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE size_t hash(const base_type& bytes) IPADDRESS_NOEXCEPT {
85 return internal::calc_hash(0, size_t(bytes[0]), size_t(bytes[1]), size_t(bytes[2]), size_t(bytes[3]));
86 }
87
89 return lhs._bytes == rhs._bytes;
90 }
91
93 return lhs._bytes < rhs._bytes;
94 }
95
96#ifdef IPADDRESS_HAS_SPACESHIP_OPERATOR
97
99 return lhs._bytes <=> rhs._bytes;
100 }
101
102#endif // IPADDRESS_HAS_SPACESHIP_OPERATOR
103
104private:
105 template <typename>
106 friend class ip_network_base;
107
108 base_type _bytes{};
109}; // ipv4_address_base
110
111/**
112 * Alias for the base class specialized for IPv4 address manipulation.
113 *
114 * This alias provides a convenient shorthand for the specialized `ip_address_base` class
115 * tailored for IPv4 address handling. It inherits all functionalities from the `ipv4_address_base`
116 * class, allowing for operations such as conversion, comparison, and property querying
117 * specific to IPv4 addresses.
118 */
120
121#ifdef IPADDRESS_NONTYPE_TEMPLATE_PARAMETER
122
123 /**
124 * User-defined literal for creating an ipv4_address from a fixed string at compile time.
125 *
126 * @tparam FixedString A compile-time fixed string representing the IPv4 address.
127 * @return An ipv4_address object parsed from the fixed string.
128 */
129 IPADDRESS_EXPORT template <fixed_string FixedString>
130 IPADDRESS_NODISCARD IPADDRESS_CONSTEVAL IPADDRESS_FORCE_INLINE ipv4_address operator""_ipv4() IPADDRESS_NOEXCEPT {
131 return ipv4_address::parse<FixedString>();
132 }
133
134 /**
135 * User-defined literal for creating an ipv4_address from an unsigned long long integer at compile time.
136 *
137 * @param[in] value An unsigned long long integer representing the IPv4 address in *host byte order*.
138 * @return An ipv4_address object created from the integer.
139 */
140 IPADDRESS_EXPORT IPADDRESS_NODISCARD IPADDRESS_CONSTEVAL IPADDRESS_FORCE_INLINE ipv4_address operator""_ipv4(unsigned long long value) IPADDRESS_NOEXCEPT {
141 assert(value <= ipv4_address::base_all_ones && "literal integer is too long");
142 return ipv4_address::from_uint(uint32_t(value));
143 }
144
145#else // IPADDRESS_NONTYPE_TEMPLATE_PARAMETER
146
147 /**
148 * User-defined literal for creating an ipv4_address from a string literal.
149 *
150 * @param[in] address A pointer to a character array representing the IPv4 address.
151 * @param[in] size The size of the character array.
152 * @return An ipv4_address object parsed from the string literal.
153 */
154 IPADDRESS_EXPORT IPADDRESS_NODISCARD_WHEN_NO_EXCEPTIONS IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE ipv4_address operator""_ipv4(const char* address, size_t size) IPADDRESS_NOEXCEPT_WHEN_NO_EXCEPTIONS {
155 return internal::parse_ip_from_literal<ipv4_address_base, char, ipv4_address::base_max_string_len>(address, size);
156 }
157
158 /**
159 * User-defined literal for creating an ipv4_address from a wide string literal.
160 *
161 * @param[in] address A pointer to a character array representing the IPv4 address.
162 * @param[in] size The size of the character array.
163 * @return An ipv4_address object parsed from the string literal.
164 */
165 IPADDRESS_EXPORT IPADDRESS_NODISCARD_WHEN_NO_EXCEPTIONS IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE ipv4_address operator""_ipv4(const wchar_t* address, size_t size) IPADDRESS_NOEXCEPT_WHEN_NO_EXCEPTIONS {
166 return internal::parse_ip_from_literal<ipv4_address_base, wchar_t, ipv4_address::base_max_string_len>(address, size);
167 }
168
169 /**
170 * User-defined literal for creating an ipv4_address from a UTF-16 string literal.
171 *
172 * @param[in] address A pointer to a character array representing the IPv4 address.
173 * @param[in] size The size of the character array.
174 * @return An ipv4_address object parsed from the string literal.
175 */
176 IPADDRESS_EXPORT IPADDRESS_NODISCARD_WHEN_NO_EXCEPTIONS IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE ipv4_address operator""_ipv4(const char16_t* address, size_t size) IPADDRESS_NOEXCEPT_WHEN_NO_EXCEPTIONS {
177 return internal::parse_ip_from_literal<ipv4_address_base, char16_t, ipv4_address::base_max_string_len>(address, size);
178 }
179
180 /**
181 * User-defined literal for creating an ipv4_address from a UTF-32 string literal.
182 *
183 * @param[in] address A pointer to a character array representing the IPv4 address.
184 * @param[in] size The size of the character array.
185 * @return An ipv4_address object parsed from the string literal.
186 */
187 IPADDRESS_EXPORT IPADDRESS_NODISCARD_WHEN_NO_EXCEPTIONS IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE ipv4_address operator""_ipv4(const char32_t* address, size_t size) IPADDRESS_NOEXCEPT_WHEN_NO_EXCEPTIONS {
188 return internal::parse_ip_from_literal<ipv4_address_base, char32_t, ipv4_address::base_max_string_len>(address, size);
189 }
190
191 /**
192 * User-defined literal for creating an ipv4_address from an unsigned long long integer.
193 *
194 * @param[in] value An unsigned long long integer representing the IPv4 address in host byte order.
195 * @return An ipv4_address object created from the integer.
196 */
197 IPADDRESS_EXPORT IPADDRESS_NODISCARD IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE ipv4_address operator""_ipv4(unsigned long long value) IPADDRESS_NOEXCEPT {
198 return ipv4_address::from_uint(uint32_t(value));
199 }
200
201#endif // IPADDRESS_NONTYPE_TEMPLATE_PARAMETER
202
203} // namespace IPADDRESS_NAMESPACE
204
205#endif // IPADDRESS_IPV4_ADDRESS_HPP
A template base class for IP address representations.
Definition ip-address-base.hpp:56
Template base class for representing a network of IP addresses.
Definition ip-network-base.hpp:32
Represents the base class for IPv4 address manipulation.
Definition ipv4-address.hpp:27
constexpr inline uint_type to_uint() const noexcept
Converts the IPv4 address to an unsigned integer.
Definition ipv4-address.hpp:60
constexpr inline const base_type & bytes() const noexcept
Provides access to the underlying bytes of the IPv4 address.
Definition ipv4-address.hpp:70
static constexpr inline ip_address_base< ipv4_address_base > from_uint() noexcept
Creates an IPv4 address from an unsigned integer using a template parameter.
Definition ipv4-address.hpp:39
static constexpr inline ip_address_base< ipv4_address_base > from_uint(uint_type ip) noexcept
Creates an IPv4 address from an unsigned integer.
Definition ipv4-address.hpp:50
#define IPADDRESS_NOEXCEPT_WHEN_NO_EXCEPTIONS
Definition config.hpp:93
#define IPADDRESS_EXPORT
Definition config.hpp:42
#define IPADDRESS_NODISCARD
Definition config.hpp:98
#define IPADDRESS_FORCE_INLINE
Definition config.hpp:112
#define IPADDRESS_NAMESPACE
Definition config.hpp:38
#define IPADDRESS_NOEXCEPT
Definition config.hpp:89
#define IPADDRESS_NODISCARD_WHEN_NO_EXCEPTIONS
Definition config.hpp:102