Skip to content

Commit

Permalink
using xtd::array
Browse files Browse the repository at this point in the history
  • Loading branch information
gammasoft71 committed Feb 6, 2025
1 parent 3543d1d commit f63cae0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
11 changes: 6 additions & 5 deletions src/xtd.core/include/xtd/net/ip_address.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -78,15 +79,15 @@ 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<xtd::byte>& address);
explicit ip_address(const xtd::array<xtd::byte>& 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.
/// @exception xtd::argument_out_of_range_exception scope_id < 0 or scope_id > 0x00000000FFFFFFFF.
/// @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<xtd::byte>& address, uint32 scope_id);
ip_address(const xtd::array<xtd::byte>& 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.
Expand Down Expand Up @@ -154,7 +155,7 @@ namespace xtd {

/// @brief Provides a copy of the IPAddress as an array of bytes.
/// @return A byte array.
std::vector<xtd::byte> get_address_bytes() const;
xtd::array<xtd::byte> get_address_bytes() const;

/// @brief Serves as a hash function for a particular type.
/// @return A hash code for the current object.
Expand Down Expand Up @@ -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<uint16>& numbers, uint32 scope_id);
ip_address(const xtd::array<uint16>& numbers, uint32 scope_id);
uint32 address_or_scope_id_ = 0;
std::vector<uint16> numbers_ = std::vector<uint16>(number_of_numbers_);
xtd::array<uint16> numbers_ = xtd::array<uint16>(number_of_numbers_);
sockets::address_family address_family_ = sockets::address_family::inter_network;
};
}
Expand Down
18 changes: 9 additions & 9 deletions src/xtd.core/src/xtd/net/ip_address.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<xtd::byte> {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<xtd::byte> {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<xtd::byte> {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<xtd::byte> {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<xtd::byte> {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<xtd::byte> {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};

Expand All @@ -25,7 +25,7 @@ ip_address::ip_address(uint32 address) {
address_or_scope_id_ = static_cast<int32>(address);
}

ip_address::ip_address(const std::vector<xtd::byte>& address) {
ip_address::ip_address(const xtd::array<xtd::byte>& address) {
if (address.size() != 4 && address.size() != 16) throw argument_exception {};

if (address.size() == 4) {
Expand All @@ -40,7 +40,7 @@ ip_address::ip_address(const std::vector<xtd::byte>& address) {
}
}

ip_address::ip_address(const std::vector<xtd::byte>& address, uint32 scope_id) : address_family_(sockets::address_family::inter_network_v6) {
ip_address::ip_address(const xtd::array<xtd::byte>& 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;
Expand All @@ -51,7 +51,7 @@ ip_address::ip_address(const std::vector<xtd::byte>& 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<uint16>& 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<uint16>& 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 {
Expand Down Expand Up @@ -118,7 +118,7 @@ size ip_address::get_hash_code() const noexcept {
return result.to_hash_code();
}

std::vector<xtd::byte> ip_address::get_address_bytes() const {
xtd::array<xtd::byte> ip_address::get_address_bytes() const {
auto bytes = std::vector<xtd::byte> {};
if (address_family_ == sockets::address_family::inter_network) {
bytes.push_back(static_cast<xtd::byte>(address_or_scope_id_));
Expand Down Expand Up @@ -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<uint16>(number_of_numbers_);
auto numbers = xtd::array<uint16>(number_of_numbers_);
numbers[5] = 0xFFFF;
numbers[6] = static_cast<uint16>(((address_or_scope_id_ & 0x0000FF00) >> 8) | ((address_or_scope_id_ & 0x000000FF) << 8));
numbers[7] = static_cast<uint16>(((address_or_scope_id_ & 0xFF000000) >> 24) | ((address_or_scope_id_ & 0x00FF0000) >> 8));
Expand Down Expand Up @@ -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<xtd::byte>(4);
auto addresses = xtd::array<xtd::byte>(4);
for (auto index = 0_z; index < address_parts.size(); ++index)
addresses[index] = xtd::parse<xtd::byte>(address_parts[index]);
return ip_address(addresses);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ namespace xtd::net::tests {
}

void test_method_(constructors_with_invalid_bytes_array) {
assert::throws<argument_exception>([] {ip_address({172, 16});});
assert::throws<argument_exception>([] {ip_address(array<byte >{172, 16});});
assert::throws<argument_exception>([] {ip_address({172, 16, 10, 30, 25, 42});});
}

Expand Down

0 comments on commit f63cae0

Please sign in to comment.