C++ Essence Library 0.1.0
A Utility Library for Modern C++ Programming
Loading...
Searching...
No Matches
ipv4_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 = 4;
46
47 using value_type = std::array<std::uint8_t, value_size>;
48
52 constexpr ipv4_address() noexcept : value_{} {}
53
58 constexpr ipv4_address(std::initializer_list<std::uint8_t> address) noexcept
60 std::span<const std::uint8_t, value_size>{address.begin(), std::min(address.size(), value_size)}} {}
61
66 constexpr explicit ipv4_address(std::span<const std::uint8_t, value_size> address) noexcept : ipv4_address{} {
67 std::ranges::copy(address, value_.begin());
68 }
69
74 constexpr explicit ipv4_address(std::uint32_t address) noexcept
75 : value_{static_cast<std::uint8_t>(address >> 24), static_cast<std::uint8_t>(address >> 16),
76 static_cast<std::uint8_t>(address >> 8), static_cast<std::uint8_t>(address & 0xFF)} {}
77
78 constexpr auto operator<=>(const ipv4_address&) const noexcept = default;
79
83 constexpr explicit operator std::uint32_t() const noexcept {
84 return (value_[0] << 24) + (value_[1] << 16) + (value_[2] << 8) + value_[3];
85 }
86
91 [[nodiscard]] constexpr value_type& get() noexcept {
92 return value_;
93 }
94
99 [[nodiscard]] constexpr const value_type& get() const noexcept {
100 return value_;
101 }
102
103 private:
104 value_type value_;
105 };
106
112 ES_API(CPPESSENCE) std::optional<ipv4_address> parse_ipv4_address(std::string_view str);
113
120 template <std::same_as<ipv4_address> T>
121 std::optional<T> from_string(std::string_view str) {
122 return parse_ipv4_address(str);
123 }
124
130 ES_API(CPPESSENCE) abi::string to_string(const ipv4_address& address);
131} // namespace essence::net
Represents an IPv4 address.
Definition ipv4_address.hpp:43
constexpr value_type & get() noexcept
Gets the underlying array.
Definition ipv4_address.hpp:91
constexpr ipv4_address(std::span< const std::uint8_t, value_size > address) noexcept
Creates an instance from a 4-byte array.
Definition ipv4_address.hpp:66
constexpr ipv4_address(std::uint32_t address) noexcept
Creates an instance from a 32-bit unsigned integer.
Definition ipv4_address.hpp:74
constexpr const value_type & get() const noexcept
Gets the underlying array.
Definition ipv4_address.hpp:99
constexpr ipv4_address() noexcept
Creates an instance.
Definition ipv4_address.hpp:52
constexpr ipv4_address(std::initializer_list< std::uint8_t > address) noexcept
Creates an instance from an initializer list.
Definition ipv4_address.hpp:58