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

Port tutorial codes to C++11, fixing issue #977 partially #1358

Merged
merged 8 commits into from
Apr 18, 2024

Conversation

merttozer
Copy link
Contributor

This pull request introduces updates to the PcapPlusPlus tutorial codes, transitioning them to utilize C++11 features for improved performance and readability. The changes align with modern C++ standards and address the requirements specified in Issue #977.

Key Changes:

  1. Auto Variables: Leveraged auto for type inference to reduce verbosity and improve code clarity.
  2. Range-Based Loops: Implemented range-based loops for safer and cleaner iteration over containers.
  3. Smart Pointers: Used smart pointers (std::unique_ptr) where applicable to manage resource allocation and deallocation more safely.
  4. Nullptr: Replaced all occurrences of NULL with nullptr to enhance type safety and readability.
  5. Lambda Functions: Introduced lambda functions for compact and inline function definitions, particularly for small scopes.

Testing:

  • Ensured that all existing tests passed to confirm that the refactoring did not introduce regressions.
  • Compiled the updated tutorial examples with a C++11 compliant compiler and verified functionality.

@merttozer merttozer requested a review from seladb as a code owner April 14, 2024 13:33
@merttozer merttozer force-pushed the port-cpp11-issue-977 branch from d0b42cd to 298e60e Compare April 14, 2024 14:16
Examples/Tutorials/Tutorial-DpdkL2Fwd/main.cpp Outdated Show resolved Hide resolved
Examples/Tutorials/Tutorial-DpdkL2Fwd/main.cpp Outdated Show resolved Hide resolved
columnLengths.push_back(15);
columnLengths.push_back(15);
columnLengths.push_back(15);
std::vector<std::string> columnNames = {" ", "Total Packets", "Packets/sec", "Bytes", "Bits/sec"};
Copy link
Collaborator

Choose a reason for hiding this comment

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

good.

Examples/Tutorials/Tutorial-DpdkL2Fwd/main.cpp Outdated Show resolved Hide resolved
Examples/Tutorials/Tutorial-DpdkL2Fwd/main.cpp Outdated Show resolved Hide resolved
Examples/Tutorials/Tutorial-LiveTraffic/main.cpp Outdated Show resolved Hide resolved
@@ -74,7 +72,7 @@ struct PacketStats
static void onPacketArrives(pcpp::RawPacket* packet, pcpp::PcapLiveDevice* dev, void* cookie)
{
// extract the stats object form the cookie
PacketStats* stats = (PacketStats*)cookie;
auto* stats = static_cast<PacketStats*>(cookie);
Copy link
Collaborator

Choose a reason for hiding this comment

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

auto here make sense.

Choose a reason for hiding this comment

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

IMO, the sensible thing here is to use static casting instead of c-style casting.


if (dev->getDnsServers().size() > 0)
std::cout << " DNS server: " << dev->getDnsServers().at(0) << std::endl;
if (!dev->getDnsServers().empty())
Copy link
Collaborator

Choose a reason for hiding this comment

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

nice.

Examples/Tutorials/Tutorial-PacketParsing/main.cpp Outdated Show resolved Hide resolved
Examples/Tutorials/Tutorial-PcapFiles/main.cpp Outdated Show resolved Hide resolved
@merttozer merttozer requested a review from tigercosmos April 14, 2024 16:18
Examples/Tutorials/Tutorial-LiveTraffic/main.cpp Outdated Show resolved Hide resolved
{
workersCoreMask = workersCoreMask | (1 << i);
}
int workersCoreMask = 0x06; // Binary 110, using cores 1 and 2
Copy link
Collaborator

Choose a reason for hiding this comment

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

It is cleaner but I'm not sure it is more beginner friendly. The for-loop implementation shows that people might set mask by using bit shifting but the updated version only shows as a constant. Since this is a tutorial code, I'm not sure which one is better.

@merttozer merttozer requested a review from egecetin April 15, 2024 10:14
Copy link
Collaborator

@tigercosmos tigercosmos left a comment

Choose a reason for hiding this comment

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

LGTM

@merttozer merttozer force-pushed the port-cpp11-issue-977 branch from 92e80a7 to 41db8c4 Compare April 17, 2024 17:37
@merttozer merttozer requested a review from seladb April 17, 2024 18:07
@seladb seladb merged commit a0dac50 into seladb:dev Apr 18, 2024
39 checks passed
@seladb
Copy link
Owner

seladb commented Apr 18, 2024

@merttozer thank you so much for working on it, much appreciated! 🙏 ❤️

@merttozer merttozer deleted the port-cpp11-issue-977 branch April 18, 2024 12:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants