Skip to content

Commit 99cb1d1

Browse files
committed
Add copy/move support for Endpoint
1 parent 9ffb4b9 commit 99cb1d1

3 files changed

Lines changed: 43 additions & 6 deletions

File tree

‎include/CppSockets/Address.hpp‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
** Author Francois Michaut
55
**
66
** Started on Sun Feb 13 17:09:05 2022 Francois Michaut
7-
** Last update Wed Aug 20 12:57:17 2025 Francois Michaut
7+
** Last update Sat Jun 20 09:12:58 2026 Francois Michaut
88
**
99
** Address.hpp : Interface to represent network addresses
1010
*/
1111

1212
#pragma once
1313

1414
#include <cstdint>
15+
#include <memory>
1516
#include <string>
1617
#include <type_traits>
1718

@@ -35,21 +36,29 @@ namespace CppSockets {
3536
[[nodiscard]] virtual auto get_addr() const -> const IAddress & = 0;
3637
[[nodiscard]] virtual auto to_string() const -> const std::string & = 0;
3738

39+
static auto from_string(std::string endpoint) -> std::shared_ptr<CppSockets::IEndpoint>;
40+
static auto from_string(std::string_view endpoint) -> std::shared_ptr<CppSockets::IEndpoint>;
41+
3842
protected:
3943
[[nodiscard]] auto make_string() const -> std::string;
4044
};
4145

4246
template <class T>
4347
class Endpoint : public IEndpoint {
4448
// TODO: Replace with new C++ requires
45-
static_assert(std::is_base_of<IAddress, T>::value,
49+
static_assert(std::is_base_of_v<IAddress, T>,
4650
"Endpoint address must derive from IAddress"
4751
);
4852
public:
4953
Endpoint(T addr, std::uint16_t port) :
5054
addr(std::move(addr)), port(port), str(make_string())
5155
{};
52-
~Endpoint() override = default;
56+
Endpoint(const Endpoint &other) = default;
57+
Endpoint(Endpoint &&other) noexcept = default;
58+
~Endpoint() override = default;
59+
60+
auto operator=(const Endpoint &other) -> Endpoint & = default;
61+
auto operator=(Endpoint &&other) noexcept -> Endpoint & = default;
5362

5463
[[nodiscard]] auto get_port() const -> std::uint16_t override { return port; }
5564
[[nodiscard]] auto get_addr() const -> const T & override { return addr; }

‎source/Address.cpp‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,43 @@
44
** Author Francois Michaut
55
**
66
** Started on Sun Feb 13 22:03:32 2022 Francois Michaut
7-
** Last update Wed Aug 20 12:58:08 2025 Francois Michaut
7+
** Last update Sat Jun 20 09:13:40 2026 Francois Michaut
88
**
99
** Address.cpp : Implementation of generic Address classes & functions
1010
*/
1111

1212
#include "CppSockets/Address.hpp"
13+
#include "CppSockets/IPv4.hpp"
14+
15+
#include <sstream>
1316

1417
namespace CppSockets {
1518
auto IEndpoint::make_string() const -> std::string {
19+
// TODO: For IPv6, the address needs to be surrounded in "[]"
1620
return this->get_addr().to_string() + ":" + std::to_string(this->get_port());
1721
}
22+
23+
// TODO: Find a better way to use string_view
24+
auto IEndpoint::from_string(std::string_view endpoint) -> std::shared_ptr<CppSockets::IEndpoint> {
25+
return from_string(std::string(endpoint));
26+
}
27+
28+
auto IEndpoint::from_string(std::string endpoint) -> std::shared_ptr<CppSockets::IEndpoint> {
29+
// TODO: Support IPv6
30+
std::size_t colon_pos = endpoint.find_last_of(':');
31+
std::stringstream ss{std::move(endpoint)};
32+
std::string ip_part(colon_pos, '\0'); // 0-init str with enough size
33+
std::uint16_t port = 0;
34+
35+
ss.read(ip_part.data(), colon_pos);
36+
37+
IPv4 ipv4(ip_part.c_str());
38+
39+
ss.ignore(1, ':');
40+
if (ss) {
41+
ss >> port;
42+
}
43+
44+
return std::make_shared<EndpointV4>(ipv4, port);
45+
}
1846
}

‎source/IPv4.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
** Author Francois Michaut
55
**
66
** Started on Sun Feb 13 18:52:28 2022 Francois Michaut
7-
** Last update Wed Aug 20 12:58:26 2025 Francois Michaut
7+
** Last update Fri Jun 12 02:17:05 2026 Francois Michaut
88
**
99
** IPv4.cpp : Implementation of IPv4 class
1010
*/
@@ -23,7 +23,7 @@
2323

2424
namespace CppSockets {
2525
IPv4::IPv4(std::uint32_t addr) :
26-
addr(htonl(addr))
26+
addr(htonl(addr)) // TODO: We are assuming input is always host - how do we allow network byte order as input ?
2727
{
2828
std::array<char, 17> buff = {0};
2929

0 commit comments

Comments
 (0)