Skip to content

Commit 716a579

Browse files
committed
Add copy/move support for Endpoint
1 parent 9ffb4b9 commit 716a579

3 files changed

Lines changed: 41 additions & 6 deletions

File tree

‎include/CppSockets/Address.hpp‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
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 Fri Jun 12 03:54:40 2026 Francois Michaut
88
**
99
** Address.hpp : Interface to represent network addresses
1010
*/
@@ -35,21 +35,29 @@ namespace CppSockets {
3535
[[nodiscard]] virtual auto get_addr() const -> const IAddress & = 0;
3636
[[nodiscard]] virtual auto to_string() const -> const std::string & = 0;
3737

38+
static auto from_string(std::string endpoint) -> std::shared_ptr<CppSockets::IEndpoint>;
39+
static auto from_string(std::string_view endpoint) -> std::shared_ptr<CppSockets::IEndpoint>;
40+
3841
protected:
3942
[[nodiscard]] auto make_string() const -> std::string;
4043
};
4144

4245
template <class T>
4346
class Endpoint : public IEndpoint {
4447
// TODO: Replace with new C++ requires
45-
static_assert(std::is_base_of<IAddress, T>::value,
48+
static_assert(std::is_base_of_v<IAddress, T>,
4649
"Endpoint address must derive from IAddress"
4750
);
4851
public:
4952
Endpoint(T addr, std::uint16_t port) :
5053
addr(std::move(addr)), port(port), str(make_string())
5154
{};
52-
~Endpoint() override = default;
55+
Endpoint(const Endpoint &other) = default;
56+
Endpoint(Endpoint &&other) noexcept = default;
57+
~Endpoint() override = default;
58+
59+
auto operator=(const Endpoint &other) -> Endpoint & = default;
60+
auto operator=(Endpoint &&other) noexcept -> Endpoint & = default;
5361

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

‎source/Address.cpp‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,42 @@
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 Fri Jun 12 03:55:02 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+
if (ss) {
40+
ss >> port;
41+
}
42+
43+
return std::make_shared<EndpointV4>(ipv4, port);
44+
}
1845
}

‎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)