ipaddress 1.2.0
Loading...
Searching...
No Matches
ipv6-network.hpp
Go to the documentation of this file.
1/**
2 * @file ipv6-network.hpp
3 * @brief Provides a set of functions and classes for handling IPv6 networks
4 * @author Vladimir Shaleev
5 * @copyright MIT License
6 *
7 * This header file defines the ipv6_network_base class and ipv6_network type, which are
8 * part of a library for working with IPv6 network addresses. The ipv6_network_base class
9 * is derived from the base_v6 class and includes methods for manipulating network addresses.
10 * The ipv6_network is a typedef for the ip_network_base class specialized for IPv6,
11 * providing a convenient alias for users of the library.
12 */
13
14#ifndef IPADDRESS_IPV6_NETWORK_HPP
15#define IPADDRESS_IPV6_NETWORK_HPP
16
17#include "ipv6-address.hpp"
19
20namespace IPADDRESS_NAMESPACE {
21
22/**
23 * Base class for IPv6 network address manipulation.
24 *
25 * The ipv6_network_base class provides foundational functionality for IPv6 network
26 * address manipulation by extending the base_v6 class.
27 */
29public:
30 using ip_address_type = ipv6_address; /**< Alias for the IPv6 address type used within the class. */
31
32 /**
33 * Checks if the IPv6 network is site-local.
34 *
35 * This method determines if both the network address and the broadcast address
36 * of the IPv6 network are site-local, which are addresses used within a particular
37 * organization's intranet and are not routable on the global internet.
38 *
39 * @return A boolean value indicating whether the network is site-local.
40 */
42 /**
43 * Safe downcast to ip_network_base<ipv6_network_base>*.
44 *
45 * This is guaranteed to be correct because:
46 * 1. ipv6_network_base is exclusively used as a CRTP base class
47 * 2. All instances are created through ipv6_network (the ip_network_base<...> alias)
48 * 3. The inheritance relationship is strictly enforced by the template
49 *
50 * Why static_cast is used:
51 * - Maintains constexpr compatibility (no RTTI needed)
52 * - Zero runtime overhead
53 * - Safety is verified at compile-time through template instantiation
54 *
55 * The NOLINT suppression is justified because:
56 * - This is an intentional design pattern
57 * - Type safety is architecturally guaranteed
58 * - Alternatives (like dynamic_cast) would hurt performance
59 */
60 const auto& network = *static_cast<const ip_network_base<ipv6_network_base>*>(this); // NOLINT(cppcoreguidelines-pro-type-static-cast-downcast)
61 return network.network_address().is_site_local() && network.broadcast_address().is_site_local();
62 }
63
64protected:
65 IPADDRESS_NODISCARD static IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE ip_address_type remove_scope_id(const ip_address_type& address) IPADDRESS_NOEXCEPT {
66 auto result = address;
67 result.set_scope_id("");
68 return result;
69 }
70
72 return ip_network_base<ipv6_network_base>::from_address(remove_scope_id(network.network_address()), network.prefixlen());
73 }
74}; // ipv6_network_base
75
76/**
77 * Alias for the specialized ip_network_base class for IPv6.
78 *
79 * The ipv6_network is a convenient alias for the ip_network_base class
80 * specialized with ipv6_network_base. It allows users to work with IPv6
81 * network addresses using a type that is specifically designed for IPv6,
82 * simplifying the interface and usage in code that deals with IPv6 networks.
83 */
85
86#ifdef IPADDRESS_NONTYPE_TEMPLATE_PARAMETER
87
88 /**
89 * User-defined literal operator for creating an ipv6_network object from a string literal.
90 *
91 * This operator allows the creation of ipv6_network objects using a string literal with the
92 * `_ipv6_net` suffix.
93 *
94 * @tparam FixedString A string literal representing the IPv6 network.
95 * @return An ipv6_network object representing the network specified by the string literal.
96 */
97 IPADDRESS_EXPORT template <fixed_string FixedString>
98 IPADDRESS_NODISCARD IPADDRESS_CONSTEVAL IPADDRESS_FORCE_INLINE ipv6_network operator""_ipv6_net() IPADDRESS_NOEXCEPT {
99 return ipv6_network::parse<FixedString>();
100 }
101
102#else // IPADDRESS_NONTYPE_TEMPLATE_PARAMETER
103
104 /**
105 * User-defined literal operator for creating an ipv6_network object from a string literal.
106 *
107 * This operator allows the creation of ipv6_network objects using a string literal with the
108 * `_ipv6_net` suffix.
109 *
110 * @param[in] address The string literal representing the IPv6 network.
111 * @param[in] size The size of the string literal.
112 * @return An ipv6_network object representing the network specified by the string literal.
113 */
114 IPADDRESS_EXPORT IPADDRESS_NODISCARD_WHEN_NO_EXCEPTIONS IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE ipv6_network operator""_ipv6_net(const char* address, size_t size) IPADDRESS_NOEXCEPT_WHEN_NO_EXCEPTIONS {
115 return internal::parse_net_from_literal<ipv6_network_base, char, ipv6_network::base_max_string_len + 4>(address, size);
116 }
117
118 /**
119 * User-defined literal operator for creating an ipv6_network object from a wide string literal.
120 *
121 * This operator allows the creation of ipv6_network objects using a string literal with the
122 * `_ipv6_net` suffix.
123 *
124 * @param[in] address The string literal representing the IPv6 network.
125 * @param[in] size The size of the string literal.
126 * @return An ipv6_network object representing the network specified by the string literal.
127 */
128 IPADDRESS_EXPORT IPADDRESS_NODISCARD_WHEN_NO_EXCEPTIONS IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE ipv6_network operator""_ipv6_net(const wchar_t* address, size_t size) IPADDRESS_NOEXCEPT_WHEN_NO_EXCEPTIONS {
129 return internal::parse_net_from_literal<ipv6_network_base, wchar_t, ipv6_network::base_max_string_len + 4>(address, size);
130 }
131
132 /**
133 * User-defined literal operator for creating an ipv6_network object from UTF-16 string literal.
134 *
135 * This operator allows the creation of ipv6_network objects using a string literal with the
136 * `_ipv6_net` suffix.
137 *
138 * @param[in] address The string literal representing the IPv6 network.
139 * @param[in] size The size of the string literal.
140 * @return An ipv6_network object representing the network specified by the string literal.
141 */
142 IPADDRESS_EXPORT IPADDRESS_NODISCARD_WHEN_NO_EXCEPTIONS IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE ipv6_network operator""_ipv6_net(const char16_t* address, size_t size) IPADDRESS_NOEXCEPT_WHEN_NO_EXCEPTIONS {
143 return internal::parse_net_from_literal<ipv6_network_base, char16_t, ipv6_network::base_max_string_len + 4>(address, size);
144 }
145
146 /**
147 * User-defined literal operator for creating an ipv6_network object from UTF-32 string literal.
148 *
149 * This operator allows the creation of ipv6_network objects using a string literal with the
150 * `_ipv6_net` suffix.
151 *
152 * @param[in] address The string literal representing the IPv6 network.
153 * @param[in] size The size of the string literal.
154 * @return An ipv6_network object representing the network specified by the string literal.
155 */
156 IPADDRESS_EXPORT IPADDRESS_NODISCARD_WHEN_NO_EXCEPTIONS IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE ipv6_network operator""_ipv6_net(const char32_t* address, size_t size) IPADDRESS_NOEXCEPT_WHEN_NO_EXCEPTIONS {
157 return internal::parse_net_from_literal<ipv6_network_base, char32_t, ipv6_network::base_max_string_len + 4>(address, size);
158 }
159
160#endif // IPADDRESS_NONTYPE_TEMPLATE_PARAMETER
161
162} // namespace IPADDRESS_NAMESPACE
163
164#endif
Template base class for representing a network of IP addresses.
Definition ip-network-base.hpp:32
Base class for IPv6 network address manipulation.
Definition ipv6-network.hpp:28
constexpr inline bool is_site_local() const noexcept
Checks if the IPv6 network is site-local.
Definition ipv6-network.hpp:41
#define IPADDRESS_NOEXCEPT_WHEN_NO_EXCEPTIONS
Definition config.hpp:96
#define IPADDRESS_EXPORT
Definition config.hpp:45
#define IPADDRESS_NODISCARD
Definition config.hpp:101
#define IPADDRESS_FORCE_INLINE
Definition config.hpp:115
#define IPADDRESS_NAMESPACE
Definition config.hpp:41
#define IPADDRESS_NOEXCEPT
Definition config.hpp:92
#define IPADDRESS_NODISCARD_WHEN_NO_EXCEPTIONS
Definition config.hpp:105