C++ Essence Library 0.1.0
A Utility Library for Modern C++ Programming
Loading...
Searching...
No Matches
ipv6_address.hpp
1/*
2 * Copyright (c) 2024 The RefValue Project
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#pragma once
24
25#include "../abi/string.hpp"
26#include "../compat.hpp"
27
28#include <algorithm>
29#include <array>
30#include <compare>
31#include <concepts>
32#include <cstddef>
33#include <cstdint>
34#include <initializer_list>
35#include <optional>
36#include <span>
37#include <string_view>
38
39namespace essence::net {
44 public:
45 static constexpr std::size_t value_size = 16;
46 static constexpr std::size_t value_group_count = value_size / 2;
53 static constexpr std::size_t max_zone_id_size = 16;
54
55 using value_type = std::array<std::uint8_t, value_size>;
56 using zone_id_type = std::array<char, max_zone_id_size>;
57
61 constexpr ipv6_address() noexcept : value_{}, zone_id_{} {}
62
63
69 constexpr ipv6_address(std::initializer_list<std::uint8_t> address, std::string_view zone_id = "") noexcept
71 std::span<const std::uint8_t, value_size>{address.begin(), std::min(address.size(), value_size)},
72 zone_id} {}
73
79 constexpr explicit ipv6_address(
80 std::span<const std::uint8_t, value_size> address, std::string_view zone_id = "") noexcept
81 : ipv6_address{} {
82 std::ranges::copy(address, value_.begin());
84 }
85
86 constexpr auto operator<=>(const ipv6_address&) const noexcept = default;
87
92 [[nodiscard]] constexpr value_type& get() noexcept {
93 return value_;
94 }
95
100 [[nodiscard]] constexpr const value_type& get() const noexcept {
101 return value_;
102 }
103
108 [[nodiscard]] constexpr const char* zone_id() const noexcept {
109 return zone_id_.data();
110 }
111
116 constexpr void set_zone_id(std::string_view zone_id) noexcept {
117 zone_id.copy(zone_id_.data(), std::min(zone_id_.size(), zone_id.size()));
118 }
119
120 private:
121 value_type value_;
122 zone_id_type zone_id_;
123 };
124
130 ES_API(CPPESSENCE) std::optional<ipv6_address> parse_ipv6_address(std::string_view str);
131
138 template <std::same_as<ipv6_address> T>
139 std::optional<T> from_string(std::string_view str) {
140 return parse_ipv6_address(str);
141 }
142
148 ES_API(CPPESSENCE) abi::string to_string(const ipv6_address& address);
149} // namespace essence::net
Represents an IPv6 address.
Definition ipv6_address.hpp:43
constexpr const char * zone_id() const noexcept
Gets the zone ID.
Definition ipv6_address.hpp:108
constexpr ipv6_address(std::span< const std::uint8_t, value_size > address, std::string_view zone_id="") noexcept
Creates an instance from a 16-byte array.
Definition ipv6_address.hpp:79
constexpr const value_type & get() const noexcept
Gets the underlying array.
Definition ipv6_address.hpp:100
constexpr value_type & get() noexcept
Gets the underlying array.
Definition ipv6_address.hpp:92
static constexpr std::size_t max_zone_id_size
On linux systems, the length of a stringified zone ID must be not longer than 16 bytes including the ...
Definition ipv6_address.hpp:53
constexpr ipv6_address(std::initializer_list< std::uint8_t > address, std::string_view zone_id="") noexcept
Creates an instance from an initializer list.
Definition ipv6_address.hpp:69
constexpr void set_zone_id(std::string_view zone_id) noexcept
Sets the zone ID.
Definition ipv6_address.hpp:116
constexpr ipv6_address() noexcept
Creates an instance.
Definition ipv6_address.hpp:61