From f63cae0b24239c6326401999008740fc7b8f52de Mon Sep 17 00:00:00 2001 From: Gammasoft Date: Thu, 6 Feb 2025 23:27:51 +0100 Subject: [PATCH] using xtd::array --- src/xtd.core/include/xtd/net/ip_address.hpp | 11 ++++++----- src/xtd.core/src/xtd/net/ip_address.cpp | 18 +++++++++--------- .../src/xtd/net/tests/ip_address_tests.cpp | 2 +- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/xtd.core/include/xtd/net/ip_address.hpp b/src/xtd.core/include/xtd/net/ip_address.hpp index 37369fc4f3c..181683fa26b 100644 --- a/src/xtd.core/include/xtd/net/ip_address.hpp +++ b/src/xtd.core/include/xtd/net/ip_address.hpp @@ -3,6 +3,7 @@ /// @copyright Copyright (c) 2025 Gammasoft. All rights reserved. #pragma once #include "sockets/address_family.hpp" +#include "../array.hpp" #include "../core_export.hpp" #include "../iequatable.hpp" #include "../object.hpp" @@ -78,7 +79,7 @@ namespace xtd { explicit ip_address(uint32 address); /// @brief Initializes a new instance of the xtd::net::ip_address class with the address specified as a byte array. /// @param address The byte array value of the IP address. - explicit ip_address(const std::vector& address); + explicit ip_address(const xtd::array& address); /// @brief Initializes a new instance of the xtd::net::ip_address class with the address specified as a byte array. /// @param address The byte array value of the IP address. /// @param scope_id The long value of the scope identifier. @@ -86,7 +87,7 @@ namespace xtd { /// @remarks This constructor instantiates an IPv6 address. The scope_dd identifies a network interface in the case of a link-local address. /// @remarks The scope is valid only for link-local and site-local addresses. /// @remarks The byte array is assumed to be in network byte order with the most significant byte first in index position 0. - ip_address(const std::vector& address, uint32 scope_id); + ip_address(const xtd::array& address, uint32 scope_id); /// @brief Initializes a new instance of the xtd::net::ip_address class with the address specified as a four Bytes. /// @param quad_part_address1 The first quad part of the IP address. /// @param quad_part_address2 The second quad part of the IP address. @@ -154,7 +155,7 @@ namespace xtd { /// @brief Provides a copy of the IPAddress as an array of bytes. /// @return A byte array. - std::vector get_address_bytes() const; + xtd::array get_address_bytes() const; /// @brief Serves as a hash function for a particular type. /// @return A hash code for the current object. @@ -313,9 +314,9 @@ namespace xtd { private: friend xtd::net::sockets::socket; static constexpr size_t number_of_numbers_ = 8; - ip_address(const std::vector& numbers, uint32 scope_id); + ip_address(const xtd::array& numbers, uint32 scope_id); uint32 address_or_scope_id_ = 0; - std::vector numbers_ = std::vector(number_of_numbers_); + xtd::array numbers_ = xtd::array(number_of_numbers_); sockets::address_family address_family_ = sockets::address_family::inter_network; }; } diff --git a/src/xtd.core/src/xtd/net/ip_address.cpp b/src/xtd.core/src/xtd/net/ip_address.cpp index 6de0ce61d80..111ef13d00e 100644 --- a/src/xtd.core/src/xtd/net/ip_address.cpp +++ b/src/xtd.core/src/xtd/net/ip_address.cpp @@ -12,9 +12,9 @@ using namespace xtd::net::sockets; const ip_address ip_address::any {0x00000000LL}; const ip_address ip_address::broadcast {0xFFFFFFFFLL}; -const ip_address ip_address::ip_v6_any {std::vector {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}; -const ip_address ip_address::ip_v6_loopback {std::vector {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}}; -const ip_address ip_address::ip_v6_none {std::vector {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}; +const ip_address ip_address::ip_v6_any {xtd::array {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}; +const ip_address ip_address::ip_v6_loopback {xtd::array {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}}; +const ip_address ip_address::ip_v6_none {xtd::array {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}; const ip_address ip_address::loopback {0x0100007FLL}; const ip_address ip_address::none {0xFFFFFFFFLL}; @@ -25,7 +25,7 @@ ip_address::ip_address(uint32 address) { address_or_scope_id_ = static_cast(address); } -ip_address::ip_address(const std::vector& address) { +ip_address::ip_address(const xtd::array& address) { if (address.size() != 4 && address.size() != 16) throw argument_exception {}; if (address.size() == 4) { @@ -40,7 +40,7 @@ ip_address::ip_address(const std::vector& address) { } } -ip_address::ip_address(const std::vector& address, uint32 scope_id) : address_family_(sockets::address_family::inter_network_v6) { +ip_address::ip_address(const xtd::array& address, uint32 scope_id) : address_family_(sockets::address_family::inter_network_v6) { if (address.size() != 16) throw argument_exception {}; address_or_scope_id_ = scope_id; @@ -51,7 +51,7 @@ ip_address::ip_address(const std::vector& address, uint32 scope_id) : ip_address::ip_address(xtd::byte quad_part_address1, xtd::byte quad_part_address2, xtd::byte quad_part_address3, xtd::byte quad_part_address4) : address_or_scope_id_((quad_part_address4 << 24 | quad_part_address3 << 16 | quad_part_address2 << 8 | quad_part_address1) & 0x0FFFFFFFF) { } -ip_address::ip_address(const std::vector& numbers, uint32 scope_id) : address_or_scope_id_(scope_id), numbers_(numbers), address_family_(sockets::address_family::inter_network_v6) { +ip_address::ip_address(const xtd::array& numbers, uint32 scope_id) : address_or_scope_id_(scope_id), numbers_(numbers), address_family_(sockets::address_family::inter_network_v6) { } sockets::address_family ip_address::address_family() const noexcept { @@ -118,7 +118,7 @@ size ip_address::get_hash_code() const noexcept { return result.to_hash_code(); } -std::vector ip_address::get_address_bytes() const { +xtd::array ip_address::get_address_bytes() const { auto bytes = std::vector {}; if (address_family_ == sockets::address_family::inter_network) { bytes.push_back(static_cast(address_or_scope_id_)); @@ -185,7 +185,7 @@ ip_address ip_address::map_to_ip_v4() const noexcept { ip_address ip_address::map_to_ip_v6() const noexcept { if (address_family_ == sockets::address_family::inter_network_v6) return *this; - auto numbers = std::vector(number_of_numbers_); + auto numbers = xtd::array(number_of_numbers_); numbers[5] = 0xFFFF; numbers[6] = static_cast(((address_or_scope_id_ & 0x0000FF00) >> 8) | ((address_or_scope_id_ & 0x000000FF) << 8)); numbers[7] = static_cast(((address_or_scope_id_ & 0xFF000000) >> 24) | ((address_or_scope_id_ & 0x00FF0000) >> 8)); @@ -227,7 +227,7 @@ uint64 ip_address::network_to_host_order(uint64 network) { ip_address ip_address::parse(const string& str) { block_scope_(auto address_parts = str.split('.')) { if (address_parts.size() == 4) { - auto addresses = std::vector(4); + auto addresses = xtd::array(4); for (auto index = 0_z; index < address_parts.size(); ++index) addresses[index] = xtd::parse(address_parts[index]); return ip_address(addresses); diff --git a/tests/xtd.core.unit_tests/src/xtd/net/tests/ip_address_tests.cpp b/tests/xtd.core.unit_tests/src/xtd/net/tests/ip_address_tests.cpp index 1c728f7cf6d..cae0d45cd2b 100644 --- a/tests/xtd.core.unit_tests/src/xtd/net/tests/ip_address_tests.cpp +++ b/tests/xtd.core.unit_tests/src/xtd/net/tests/ip_address_tests.cpp @@ -74,7 +74,7 @@ namespace xtd::net::tests { } void test_method_(constructors_with_invalid_bytes_array) { - assert::throws([] {ip_address({172, 16});}); + assert::throws([] {ip_address(array{172, 16});}); assert::throws([] {ip_address({172, 16, 10, 30, 25, 42});}); }