Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed IPNetwork copy assignment implementation to copy-and-swap. #1681

Merged
merged 3 commits into from
Jan 9, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 6 additions & 24 deletions Common++/header/IpAddress.h
Original file line number Diff line number Diff line change
Expand Up @@ -707,18 +707,9 @@ namespace pcpp
/// @return A reference to the assignee
IPNetwork& operator=(const IPv4Network& other)
{
if (m_IPv4Network)
{
m_IPv4Network = nullptr;
}

if (m_IPv6Network)
{
m_IPv6Network = nullptr;
}

m_IPv4Network = std::unique_ptr<IPv4Network>(new IPv4Network(other));

auto newNetwork = std::unique_ptr<IPv4Network>(new IPv4Network(other));
m_IPv4Network = std::move(newNetwork);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's necessary to use std::move here since a smart pointer is already cheap.
I would just use m_IPv4Network = std::unique_ptr<IPv4Network>(new IPv4Network(other)); for clearance.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the operations can be merged. Both should compile down to the same tho.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Dimi1010 I also think it will be the same after the compilation optimization. Then could you just merge the lines, so it looks cleaner?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be good now.

m_IPv6Network = nullptr;
return *this;
}

Expand All @@ -727,18 +718,9 @@ namespace pcpp
/// @return A reference to the assignee
IPNetwork& operator=(const IPv6Network& other)
{
if (m_IPv4Network)
{
m_IPv4Network = nullptr;
}

if (m_IPv6Network)
{
m_IPv6Network = nullptr;
}

m_IPv6Network = std::unique_ptr<IPv6Network>(new IPv6Network(other));

auto newNetwork = std::unique_ptr<IPv6Network>(new IPv6Network(other));
m_IPv4Network = nullptr;
m_IPv6Network = std::move(newNetwork);
return *this;
}

Expand Down
Loading