Skip to content

Commit

Permalink
Fixed compiler and clang-tidy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianReimold committed Feb 22, 2024
1 parent c603577 commit 200d367
Show file tree
Hide file tree
Showing 13 changed files with 110 additions and 76 deletions.
4 changes: 4 additions & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,25 @@ Checks: "-*,
-cppcoreguidelines-pro-bounds-pointer-arithmetic,
-cppcoreguidelines-pro-type-vararg,
-cppcoreguidelines-pro-type-reinterpret-cast,
-cppcoreguidelines-use-default-member-init,
misc-*,
-misc-non-private-member-variables-in-classes,
-misc-no-recursion,
-misc-include-cleaner,
modernize-*,
-modernize-pass-by-value,
-modernize-use-trailing-return-type,
-modernize-use-auto,
-modernize-use-default-member-init,
-modernize-concat-nested-namespaces,
-modernize-return-braced-init-list,
-modernize-use-nodiscard,
-modernize-avoid-bind,
performance-*,
-performance-avoid-endl,
readability-*,
-readability-braces-around-statements,
Expand Down
34 changes: 17 additions & 17 deletions tests/udpcap_test/src/atomic_signalable.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,79 +27,79 @@ class atomic_signalable

atomic_signalable<T>& operator=(const T new_value)
{
std::lock_guard<std::mutex> lock(mutex);
const std::lock_guard<std::mutex> lock(mutex);
value = new_value;
cv.notify_all();
return *this;
}

T operator++()
{
std::lock_guard<std::mutex> lock(mutex);
const std::lock_guard<std::mutex> lock(mutex);
T newValue = ++value;
cv.notify_all();
return newValue;
}

T operator++(T)
{
std::lock_guard<std::mutex> lock(mutex);
const std::lock_guard<std::mutex> lock(mutex);
T oldValue = value++;
cv.notify_all();
return oldValue;
}

T operator--()
{
std::lock_guard<std::mutex> lock(mutex);
const std::lock_guard<std::mutex> lock(mutex);
T newValue = --value;
cv.notify_all();
return newValue;
}

T operator--(T)
{
std::lock_guard<std::mutex> lock(mutex);
const std::lock_guard<std::mutex> lock(mutex);
T oldValue = value--;
cv.notify_all();
return oldValue;
}

T operator+=(const T& other)
{
std::lock_guard<std::mutex> lock(mutex);
const std::lock_guard<std::mutex> lock(mutex);
value += other;
cv.notify_all();
return value;
}

T operator-=(const T& other)
{
std::lock_guard<std::mutex> lock(mutex);
const std::lock_guard<std::mutex> lock(mutex);
value -= other;
cv.notify_all();
return value;
}

T operator*=(const T& other)
{
std::lock_guard<std::mutex> lock(mutex);
const std::lock_guard<std::mutex> lock(mutex);
value *= other;
cv.notify_all();
return value;
}

T operator/=(const T& other)
{
std::lock_guard<std::mutex> lock(mutex);
const std::lock_guard<std::mutex> lock(mutex);
value /= other;
cv.notify_all();
return value;
}

T operator%=(const T& other)
{
std::lock_guard<std::mutex> lock(mutex);
const std::lock_guard<std::mutex> lock(mutex);
value %= other;
cv.notify_all();
return value;
Expand All @@ -114,13 +114,13 @@ class atomic_signalable

T get() const
{
std::lock_guard<std::mutex> lock(mutex);
const std::lock_guard<std::mutex> lock(mutex);
return value;
}

bool operator==(T other) const
{
std::lock_guard<std::mutex> lock(mutex);
const std::lock_guard<std::mutex> lock(mutex);
return value == other;
}

Expand All @@ -133,31 +133,31 @@ class atomic_signalable

bool operator!=(T other) const
{
std::lock_guard<std::mutex> lock(mutex);
const std::lock_guard<std::mutex> lock(mutex);
return value != other;
}

bool operator<(T other) const
{
std::lock_guard<std::mutex> lock(mutex);
const std::lock_guard<std::mutex> lock(mutex);
return value < other;
}

bool operator<=(T other) const
{
std::lock_guard<std::mutex> lock(mutex);
const std::lock_guard<std::mutex> lock(mutex);
return value <= other;
}

bool operator>(T other) const
{
std::lock_guard<std::mutex> lock(mutex);
const std::lock_guard<std::mutex> lock(mutex);
return value > other;
}

bool operator>=(T other) const
{
std::lock_guard<std::mutex> lock(mutex);
const std::lock_guard<std::mutex> lock(mutex);
return value >= other;
}

Expand Down
50 changes: 24 additions & 26 deletions tests/udpcap_test/src/udpcap_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ TEST(udpcap, RAII)
{
{
// Create a udpcap socket
Udpcap::UdpcapSocket udpcap_socket;
const Udpcap::UdpcapSocket udpcap_socket;
ASSERT_TRUE(udpcap_socket.isValid());
// Delete the socket
}
Expand All @@ -45,7 +45,7 @@ TEST(udpcap, RAIIWithClose)
ASSERT_TRUE(udpcap_socket.isValid());

// bind the socket
bool success = udpcap_socket.bind(Udpcap::HostAddress::Any(), 14000);
const bool success = udpcap_socket.bind(Udpcap::HostAddress::Any(), 14000);
ASSERT_TRUE(success);

// Close the socket
Expand All @@ -60,7 +60,7 @@ TEST(udpcap, RAIIWithSomebodyWaiting)
ASSERT_TRUE(udpcap_socket.isValid());

// bind the socket
bool success = udpcap_socket.bind(Udpcap::HostAddress::Any(), 14000);
const bool success = udpcap_socket.bind(Udpcap::HostAddress::Any(), 14000);
ASSERT_TRUE(success);

// Blocking receive a datagram
Expand All @@ -73,7 +73,7 @@ TEST(udpcap, RAIIWithSomebodyWaiting)
Udpcap::Error error = Udpcap::Error::ErrorCode::GENERIC_ERROR;

// blocking receive
size_t received_bytes = udpcap_socket.receiveDatagram(received_datagram.data(), received_datagram.size(), 0, error);
const size_t received_bytes = udpcap_socket.receiveDatagram(received_datagram.data(), received_datagram.size(), 0, error);

// Check that we didn't receive any bytes
ASSERT_EQ(received_bytes, 0);
Expand Down Expand Up @@ -107,7 +107,7 @@ TEST(udpcap, SimpleReceive)
ASSERT_TRUE(udpcap_socket.isValid());

{
bool success = udpcap_socket.bind(Udpcap::HostAddress::Any(), 14000);
const bool success = udpcap_socket.bind(Udpcap::HostAddress::Any(), 14000);
ASSERT_TRUE(success);
}

Expand All @@ -124,7 +124,7 @@ TEST(udpcap, SimpleReceive)
received_datagram.resize(65536);

// blocking receive
size_t received_bytes = udpcap_socket.receiveDatagram(received_datagram.data(), received_datagram.size(), &sender_address, &sender_port, error);
const size_t received_bytes = udpcap_socket.receiveDatagram(received_datagram.data(), received_datagram.size(), &sender_address, &sender_port, error);
received_datagram.resize(received_bytes);

// No error must have occurred
Expand Down Expand Up @@ -172,7 +172,7 @@ TEST(udpcap, MultipleSmallPackages)

// Bind the udpcap socket to all interfaces
{
bool success = udpcap_socket.bind(Udpcap::HostAddress::Any(), 14000);
const bool success = udpcap_socket.bind(Udpcap::HostAddress::Any(), 14000);
ASSERT_TRUE(success);
}

Expand All @@ -188,7 +188,7 @@ TEST(udpcap, MultipleSmallPackages)
received_datagram.resize(65536);

// blocking receive
size_t received_bytes = udpcap_socket.receiveDatagram(received_datagram.data(), received_datagram.size(), error);
const size_t received_bytes = udpcap_socket.receiveDatagram(received_datagram.data(), received_datagram.size(), error);

if (error)
{
Expand Down Expand Up @@ -249,7 +249,7 @@ TEST(udpcap, SimpleReceiveWithBuffer)
ASSERT_TRUE(udpcap_socket.isValid());

{
bool success = udpcap_socket.bind(Udpcap::HostAddress::LocalHost(), 14000);
const bool success = udpcap_socket.bind(Udpcap::HostAddress::LocalHost(), 14000);
ASSERT_TRUE(success);
}

Expand Down Expand Up @@ -317,7 +317,7 @@ TEST(udpcap, DelayedPackageReceiveMultiplePackages)

// Bind the udpcap socket to all interfaces
{
bool success = udpcap_socket.bind(Udpcap::HostAddress::Any(), 14000);
const bool success = udpcap_socket.bind(Udpcap::HostAddress::Any(), 14000);
ASSERT_TRUE(success);
}

Expand All @@ -335,7 +335,7 @@ TEST(udpcap, DelayedPackageReceiveMultiplePackages)
std::vector<char> received_datagram;
received_datagram.resize(65536);

size_t bytes_received = udpcap_socket.receiveDatagram(received_datagram.data(), received_datagram.size(), &sender_address, &sender_port, error);
const size_t bytes_received = udpcap_socket.receiveDatagram(received_datagram.data(), received_datagram.size(), &sender_address, &sender_port, error);
received_datagram.resize(bytes_received);

if (error)
Expand Down Expand Up @@ -396,14 +396,12 @@ TEST(udpcap, DelayedPackageReceiveMultiplePackages)
// Test the timeout of the receiveDatagram function
TEST(udpcap, Timeout)
{
atomic_signalable<int> received_messages(0);

// Create a udpcap socket
Udpcap::UdpcapSocket udpcap_socket;
ASSERT_TRUE(udpcap_socket.isValid());

{
bool success = udpcap_socket.bind(Udpcap::HostAddress::Any(), 14000);
const bool success = udpcap_socket.bind(Udpcap::HostAddress::Any(), 14000);
ASSERT_TRUE(success);
}

Expand All @@ -429,7 +427,7 @@ TEST(udpcap, Timeout)
auto start_time = std::chrono::steady_clock::now();

// blocking receive with a 100ms timeout
size_t received_bytes = udpcap_socket.receiveDatagram(received_datagram.data(), received_datagram.size(), 100, &sender_address, &sender_port, error);
const size_t received_bytes = udpcap_socket.receiveDatagram(received_datagram.data(), received_datagram.size(), 100, &sender_address, &sender_port, error);

// Take End time
auto end_time = std::chrono::steady_clock::now();
Expand All @@ -450,7 +448,7 @@ TEST(udpcap, Timeout)
auto start_time = std::chrono::steady_clock::now();

// blocking receive with a 500ms timeout
size_t received_bytes = udpcap_socket.receiveDatagram(received_datagram.data(), received_datagram.size(), 500, &sender_address, &sender_port, error);
const size_t received_bytes = udpcap_socket.receiveDatagram(received_datagram.data(), received_datagram.size(), 500, &sender_address, &sender_port, error);

// Take End time
auto end_time = std::chrono::steady_clock::now();
Expand All @@ -470,7 +468,7 @@ TEST(udpcap, Timeout)
auto start_time = std::chrono::steady_clock::now();

// blocking receive with a 0ms timeout
size_t received_bytes = udpcap_socket.receiveDatagram(received_datagram.data(), received_datagram.size(), 0, &sender_address, &sender_port, error);
const size_t received_bytes = udpcap_socket.receiveDatagram(received_datagram.data(), received_datagram.size(), 0, &sender_address, &sender_port, error);

// Take End time
auto end_time = std::chrono::steady_clock::now();
Expand All @@ -488,7 +486,7 @@ TEST(udpcap, Timeout)
auto start_time = std::chrono::steady_clock::now();

// blocking receive with a 0ms timeout
size_t received_bytes = udpcap_socket.receiveDatagram(received_datagram.data(), received_datagram.size(), 0, &sender_address, &sender_port, error);
const size_t received_bytes = udpcap_socket.receiveDatagram(received_datagram.data(), received_datagram.size(), 0, &sender_address, &sender_port, error);

// Take End time
auto end_time = std::chrono::steady_clock::now();
Expand Down Expand Up @@ -525,7 +523,7 @@ TEST(udpcap, ReceiveNotBound)
Udpcap::Error error = Udpcap::Error::ErrorCode::GENERIC_ERROR;

// blocking receive
size_t received_bytes = udpcap_socket.receiveDatagram(received_datagram.data(), received_datagram.size(), &sender_address, &sender_port, error);
const size_t received_bytes = udpcap_socket.receiveDatagram(received_datagram.data(), received_datagram.size(), &sender_address, &sender_port, error);

// Check if the received datagram is valid and contains "Hello World"
ASSERT_EQ(received_bytes, 0);
Expand All @@ -551,27 +549,27 @@ TEST(udpcap, MulticastReceive)

// Bind the udpcap sockets to all interfaces
{
bool success = udpcap_socket1.bind(Udpcap::HostAddress::Any(), 14000);
const bool success = udpcap_socket1.bind(Udpcap::HostAddress::Any(), 14000);
ASSERT_TRUE(success);
}
{
bool success = udpcap_socket2.bind(Udpcap::HostAddress::Any(), 14000);
const bool success = udpcap_socket2.bind(Udpcap::HostAddress::Any(), 14000);
ASSERT_TRUE(success);
}

// Join the multicast group 224.0.0.1 on both sockets
{
bool success = udpcap_socket1.joinMulticastGroup(Udpcap::HostAddress("224.0.0.1"));
const bool success = udpcap_socket1.joinMulticastGroup(Udpcap::HostAddress("224.0.0.1"));
ASSERT_TRUE(success);
}
{
bool success = udpcap_socket2.joinMulticastGroup(Udpcap::HostAddress("224.0.0.1"));
const bool success = udpcap_socket2.joinMulticastGroup(Udpcap::HostAddress("224.0.0.1"));
ASSERT_TRUE(success);
}

// Join the multicast group 224.0.0.2 on the second socket
{
bool success = udpcap_socket2.joinMulticastGroup(Udpcap::HostAddress("224.0.0.2"));
const bool success = udpcap_socket2.joinMulticastGroup(Udpcap::HostAddress("224.0.0.2"));
ASSERT_TRUE(success);
}

Expand All @@ -598,7 +596,7 @@ TEST(udpcap, MulticastReceive)
std::vector<char> received_datagram;
received_datagram.resize(65536);

size_t bytes_received = udpcap_socket1.receiveDatagram(received_datagram.data(), received_datagram.size(), &sender_address, &sender_port, error);
const size_t bytes_received = udpcap_socket1.receiveDatagram(received_datagram.data(), received_datagram.size(), &sender_address, &sender_port, error);
received_datagram.resize(bytes_received);

if (error)
Expand Down Expand Up @@ -635,7 +633,7 @@ TEST(udpcap, MulticastReceive)
std::vector<char> received_datagram;
received_datagram.resize(65536);

size_t bytes_received = udpcap_socket2.receiveDatagram(received_datagram.data(), received_datagram.size(), &sender_address, &sender_port, error);
const size_t bytes_received = udpcap_socket2.receiveDatagram(received_datagram.data(), received_datagram.size(), &sender_address, &sender_port, error);
received_datagram.resize(bytes_received);

if (error)
Expand Down
5 changes: 4 additions & 1 deletion udpcap/include/udpcap/host_address.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
#pragma once

#include <string>

// IWYU pragma: begin_exports
#include <udpcap/udpcap_export.h>
// IWYU pragma: end_exports

namespace Udpcap
{
Expand Down Expand Up @@ -57,7 +60,7 @@ namespace Udpcap
UDPCAP_EXPORT HostAddress(const std::string& address);

/** @brief Constructs a HostAddress from a 32bit integer in host byte order. */
UDPCAP_EXPORT HostAddress(const uint32_t address);
UDPCAP_EXPORT HostAddress(uint32_t address);

/** @brief Checks if the Host Address is valid.
* Invalid HostAddresses are created when providing a wrong IPv4 string,
Expand Down
Loading

0 comments on commit 200d367

Please sign in to comment.