ipaddress 1.1.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 const auto& network = *static_cast<const ip_network_base<ipv6_network_base>*>(this); // NOLINT(cppcoreguidelines-pro-type-static-cast-downcast)
43 return network.network_address().is_site_local() && network.broadcast_address().is_site_local();
44 }
45
46protected:
47 IPADDRESS_NODISCARD static IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE ip_address_type remove_scope_id(const ip_address_type& address) IPADDRESS_NOEXCEPT {
48 auto result = address;
49 result.set_scope_id("");
50 return result;
51 }
52
54 return ip_network_base<ipv6_network_base>::from_address(remove_scope_id(network.network_address()), network.prefixlen());
55 }
56}; // ipv6_network_base
57
58/**
59 * Alias for the specialized ip_network_base class for IPv6.
60 *
61 * The ipv6_network is a convenient alias for the ip_network_base class
62 * specialized with ipv6_network_base. It allows users to work with IPv6
63 * network addresses using a type that is specifically designed for IPv6,
64 * simplifying the interface and usage in code that deals with IPv6 networks.
65 */
67
68#ifdef IPADDRESS_NONTYPE_TEMPLATE_PARAMETER
69
70 /**
71 * User-defined literal operator for creating an ipv6_network object from a string literal.
72 *
73 * This operator allows the creation of ipv6_network objects using a string literal with the
74 * `_ipv6_net` suffix.
75 *
76 * @tparam FixedString A string literal representing the IPv6 network.
77 * @return An ipv6_network object representing the network specified by the string literal.
78 */
79 IPADDRESS_EXPORT template <fixed_string FixedString>
80 IPADDRESS_NODISCARD IPADDRESS_CONSTEVAL IPADDRESS_FORCE_INLINE ipv6_network operator""_ipv6_net() IPADDRESS_NOEXCEPT {
81 return ipv6_network::parse<FixedString>();
82 }
83
84#else // IPADDRESS_NONTYPE_TEMPLATE_PARAMETER
85
86 /**
87 * User-defined literal operator for creating an ipv6_network object from a string literal.
88 *
89 * This operator allows the creation of ipv6_network objects using a string literal with the
90 * `_ipv6_net` suffix.
91 *
92 * @param[in] address The string literal representing the IPv6 network.
93 * @param[in] size The size of the string literal.
94 * @return An ipv6_network object representing the network specified by the string literal.
95 */
96 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 {
97 return internal::parse_net_from_literal<ipv6_network_base, char, ipv6_network::base_max_string_len + 4>(address, size);
98 }
99
100 /**
101 * User-defined literal operator for creating an ipv6_network object from a wide string literal.
102 *
103 * This operator allows the creation of ipv6_network objects using a string literal with the
104 * `_ipv6_net` suffix.
105 *
106 * @param[in] address The string literal representing the IPv6 network.
107 * @param[in] size The size of the string literal.
108 * @return An ipv6_network object representing the network specified by the string literal.
109 */
110 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 {
111 return internal::parse_net_from_literal<ipv6_network_base, wchar_t, ipv6_network::base_max_string_len + 4>(address, size);
112 }
113
114 /**
115 * User-defined literal operator for creating an ipv6_network object from UTF-16 string literal.
116 *
117 * This operator allows the creation of ipv6_network objects using a string literal with the
118 * `_ipv6_net` suffix.
119 *
120 * @param[in] address The string literal representing the IPv6 network.
121 * @param[in] size The size of the string literal.
122 * @return An ipv6_network object representing the network specified by the string literal.
123 */
124 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 {
125 return internal::parse_net_from_literal<ipv6_network_base, char16_t, ipv6_network::base_max_string_len + 4>(address, size);
126 }
127
128 /**
129 * User-defined literal operator for creating an ipv6_network object from UTF-32 string literal.
130 *
131 * This operator allows the creation of ipv6_network objects using a string literal with the
132 * `_ipv6_net` suffix.
133 *
134 * @param[in] address The string literal representing the IPv6 network.
135 * @param[in] size The size of the string literal.
136 * @return An ipv6_network object representing the network specified by the string literal.
137 */
138 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 {
139 return internal::parse_net_from_literal<ipv6_network_base, char32_t, ipv6_network::base_max_string_len + 4>(address, size);
140 }
141
142#endif // IPADDRESS_NONTYPE_TEMPLATE_PARAMETER
143
144} // namespace IPADDRESS_NAMESPACE
145
146#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: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