add move and copy constructors/assigment operators to ip_address

pull/1272/head
Jeff Becker 4 years ago
parent 3eb006f78c
commit 114bdd5ce5
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

@ -9,6 +9,11 @@ namespace llarp
setAddress(str);
}
IpAddress::IpAddress(const IpAddress& other)
: m_empty(other.m_empty), m_ipAddress(other.m_ipAddress), m_port(other.m_port)
{
}
IpAddress::IpAddress(std::string_view str, std::optional<uint16_t> port)
{
setAddress(str, port);
@ -24,6 +29,16 @@ namespace llarp
m_empty = addr.isEmpty();
}
IpAddress&
IpAddress::operator=(IpAddress&& other)
{
m_ipAddress = std::move(other.m_ipAddress);
m_port = std::move(other.m_port);
m_empty = other.m_empty;
other.m_empty = false;
return *this;
}
IpAddress&
IpAddress::operator=(const sockaddr& other)
{
@ -38,6 +53,14 @@ namespace llarp
return *this;
}
IpAddress&
IpAddress::operator=(const IpAddress& other)
{
m_empty = other.m_empty;
m_ipAddress = other.m_ipAddress;
m_port = other.m_port;
return *this;
}
std::optional<uint16_t>
IpAddress::getPort() const

@ -21,6 +21,10 @@ namespace llarp
{
/// Empty constructor.
IpAddress() = default;
/// move construtor
IpAddress(IpAddress&&) = default;
/// copy construct
IpAddress(const IpAddress&);
/// Constructor. Takes a string which can be an IPv4 or IPv6 address optionally followed by
/// a colon and a port.
@ -51,6 +55,14 @@ namespace llarp
IpAddress&
operator=(const sockaddr& other);
/// move assignment
IpAddress&
operator=(IpAddress&& other);
/// copy assignment
IpAddress&
operator=(const IpAddress& other);
/// Return the port. Returns -1 if no port has been provided.
///
/// @return the port, if present
@ -119,19 +131,7 @@ namespace llarp
std::size_t
operator()(const IpAddress& address) const noexcept
{
(void)address;
// throw std::runtime_error("FIXME: IpAddress::Hash"); // can't do this in operator(),
// apparently, so hopefully it's me that stumbles upon this (if not, sorry!)
return 0;
/*
if(a.af() == AF_INET)
{
return a.port() ^ a.addr4()->s_addr;
}
static const uint8_t empty[16] = {0};
return (a.af() + memcmp(a.addr6(), empty, 16)) ^ a.port();
*/
return std::hash<std::string>{}(address.toString());
}
};

Loading…
Cancel
Save