Skip to content

Commit

Permalink
clang-tidy suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
ksmit799 committed Jan 8, 2024
1 parent ea1c39e commit 4e3a162
Show file tree
Hide file tree
Showing 16 changed files with 72 additions and 70 deletions.
4 changes: 2 additions & 2 deletions src/clientagent/client_participant.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ struct Interest {
std::unordered_set<uint32_t> zones;
};

class ClientParticipant : public NetworkClient, public ChannelSubscriber {
class ClientParticipant final : public NetworkClient, public ChannelSubscriber {
public:
ClientParticipant(ClientAgent *clientAgent,
const std::shared_ptr<uvw::tcp_handle> &socket);
~ClientParticipant();
~ClientParticipant() override;

friend class InterestOperation;

Expand Down
2 changes: 1 addition & 1 deletion src/clientagent/interest_operation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ void InterestOperation::Finish(const bool &isTimeout) {
delete this;
}

bool InterestOperation::IsReady() {
bool InterestOperation::IsReady() const {
return _hasTotal && _pendingGenerates.size() >= _total;
}

Expand Down
2 changes: 1 addition & 1 deletion src/clientagent/interest_operation.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class InterestOperation {
private:
void HandleInterestTimeout();

bool IsReady();
bool IsReady() const;
void SetExpected(const uint32_t &total);
void QueueExpected(const std::shared_ptr<Datagram> &dg);
void QueueDatagram(const std::shared_ptr<Datagram> &dg);
Expand Down
2 changes: 1 addition & 1 deletion src/database/database_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Ardos {

class DatabaseServer : public ChannelSubscriber {
class DatabaseServer final : public ChannelSubscriber {
public:
DatabaseServer();

Expand Down
7 changes: 4 additions & 3 deletions src/database/database_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ namespace Ardos {
/**
* Exception thrown by many database utility functions.
*/
class ConversionException : public std::exception {
class ConversionException final : public std::exception {
public:
explicit ConversionException(const char *msg) : _message(msg), _what(msg) {}

[[nodiscard]] inline const char *what() const noexcept override {
[[nodiscard]] const char *what() const noexcept override {
return _what.c_str();
}

inline void PushName(const std::string &name) {
void PushName(const std::string &name) {
_names.push_front(name);

_what = "";
Expand Down Expand Up @@ -100,6 +100,7 @@ class DatabaseUtils {
* Specifically for handling unsigned integers.
* @tparam T
* @param value
* @param divisor
* @return
*/
template <typename T>
Expand Down
4 changes: 2 additions & 2 deletions src/net/address_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ std::string AddressUtils::resolve_host(const std::shared_ptr<uvw::loop> &loop,
const std::string &host,
unsigned int port) {
// First, test if we have a valid IPv4 address.
sockaddr_in sockaddr;
sockaddr_in sockaddr{};
if (uv_ip4_addr(host.c_str(), port, &sockaddr) == 0) {
return host;
}

// Next, test if we have a valid IPv6 address.
sockaddr_in6 sockaddr6;
sockaddr_in6 sockaddr6{};
if (uv_ip6_addr(host.c_str(), port, &sockaddr6) == 0) {
return host;
}
Expand Down
16 changes: 8 additions & 8 deletions src/net/datagram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
namespace Ardos {

Datagram::Datagram()
: _buf(new uint8_t[kMinDgSize]), _bufLength(kMinDgSize), _bufOffset(0) {}
: _buf(new uint8_t[kMinDgSize]), _bufOffset(0), _bufLength(kMinDgSize) {}

Datagram::Datagram(const uint8_t *data, size_t size)
: _buf(new uint8_t[size]), _bufLength(size), _bufOffset(size) {
Datagram::Datagram(const uint8_t *data, const size_t &size)
: _buf(new uint8_t[size]), _bufOffset(size), _bufLength(size) {
memcpy(_buf, data, size);
}

Datagram::Datagram(const uint64_t &toChannel, const uint64_t &fromChannel,
const uint16_t &msgType)
: _buf(new uint8_t[kMinDgSize]), _bufLength(kMinDgSize), _bufOffset(0) {
: _buf(new uint8_t[kMinDgSize]), _bufOffset(0), _bufLength(kMinDgSize) {
AddUint8(1);
AddUint64(toChannel);
AddUint64(fromChannel);
Expand All @@ -24,7 +24,7 @@ Datagram::Datagram(const uint64_t &toChannel, const uint64_t &fromChannel,

Datagram::Datagram(const std::unordered_set<uint64_t> &toChannels,
const uint64_t &fromChannel, const uint16_t &msgType)
: _buf(new uint8_t[kMinDgSize]), _bufLength(kMinDgSize), _bufOffset(0) {
: _buf(new uint8_t[kMinDgSize]), _bufOffset(0), _bufLength(kMinDgSize) {
AddUint8(toChannels.size());
for (const auto &channel : toChannels) {
AddUint64(channel);
Expand Down Expand Up @@ -55,13 +55,13 @@ uint16_t Datagram::Size() const { return _bufOffset; }
* Returns the underlying data pointer for this datagram.
* @return
*/
const uint8_t *Datagram::GetData() { return _buf; }
const uint8_t *Datagram::GetData() const { return _buf; }

/**
* Returns the bytes packed into this datagram.
* @return
*/
std::vector<uint8_t> Datagram::GetBytes() {
std::vector<uint8_t> Datagram::GetBytes() const {
std::vector<uint8_t> data(GetData(), GetData() + Size());
return data;
}
Expand Down Expand Up @@ -258,7 +258,7 @@ void Datagram::EnsureLength(const size_t &length) {

// Do we need to resize the buffer?
if (newOffset > _bufLength) {
size_t newLength = _bufLength + kMinDgSize + length;
const size_t newLength = _bufLength + kMinDgSize + length;

// Copy our old buffer into a new one.
auto *tempBuf = new uint8_t[newLength];
Expand Down
12 changes: 6 additions & 6 deletions src/net/datagram.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ namespace Ardos {

// Max amount of data we can have is an uint16 (65k bytes)
// -2 for the required prepended length tag.
const size_t kMaxDgSize = 0xffff - 2;
constexpr size_t kMaxDgSize = 0xffff - 2;
// 128 bytes seems like a good minimum datagram size.
const size_t kMinDgSize = 0x80;
constexpr size_t kMinDgSize = 0x80;

/**
* A DatagramOverflow is an exception which occurs when an Add<value> method is
* called which would increase the size of the datagram past kMaxDgSize
* (preventing integer and buffer overflow).
*/
class DatagramOverflow : public std::runtime_error {
class DatagramOverflow final : public std::runtime_error {
public:
explicit DatagramOverflow(const std::string &what)
: std::runtime_error(what) {}
Expand All @@ -43,7 +43,7 @@ class DatagramOverflow : public std::runtime_error {
class Datagram {
public:
Datagram();
Datagram(const uint8_t *data, size_t size);
Datagram(const uint8_t *data, const size_t &size);
Datagram(const uint64_t &toChannel, const uint64_t &fromChannel,
const uint16_t &msgType);
Datagram(const std::unordered_set<uint64_t> &toChannels,
Expand All @@ -53,8 +53,8 @@ class Datagram {
void Clear();

[[nodiscard]] uint16_t Size() const;
const uint8_t *GetData();
std::vector<uint8_t> GetBytes();
[[nodiscard]] const uint8_t *GetData() const;
[[nodiscard]] std::vector<uint8_t> GetBytes() const;

void AddBool(const bool &v);
void AddInt8(const int8_t &v);
Expand Down
46 changes: 23 additions & 23 deletions src/net/datagram_iterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace Ardos {

DatagramIterator::DatagramIterator(const std::shared_ptr<Datagram> &dg,
size_t offset)
const size_t &offset)
: _dg(dg), _offset(offset) {}

/**
Expand All @@ -22,7 +22,7 @@ bool DatagramIterator::GetBool() { return GetUint8(); }
*/
int8_t DatagramIterator::GetInt8() {
EnsureLength(1);
int8_t v = *(int8_t *)(_dg->GetData() + _offset);
const int8_t v = *(int8_t *)(_dg->GetData() + _offset);
_offset += 1;
return v;
}
Expand All @@ -33,7 +33,7 @@ int8_t DatagramIterator::GetInt8() {
*/
uint8_t DatagramIterator::GetUint8() {
EnsureLength(1);
uint8_t v = *(uint8_t *)(_dg->GetData() + _offset);
const uint8_t v = *(uint8_t *)(_dg->GetData() + _offset);
_offset += 1;
return v;
}
Expand All @@ -44,7 +44,7 @@ uint8_t DatagramIterator::GetUint8() {
*/
int16_t DatagramIterator::GetInt16() {
EnsureLength(2);
int16_t v = *(int16_t *)(_dg->GetData() + _offset);
const int16_t v = *(int16_t *)(_dg->GetData() + _offset);
_offset += 2;
return v;
}
Expand All @@ -55,7 +55,7 @@ int16_t DatagramIterator::GetInt16() {
*/
uint16_t DatagramIterator::GetUint16() {
EnsureLength(2);
uint16_t v = *(uint16_t *)(_dg->GetData() + _offset);
const uint16_t v = *(uint16_t *)(_dg->GetData() + _offset);
_offset += 2;
return v;
}
Expand All @@ -66,7 +66,7 @@ uint16_t DatagramIterator::GetUint16() {
*/
int32_t DatagramIterator::GetInt32() {
EnsureLength(4);
int32_t v = *(int32_t *)(_dg->GetData() + _offset);
const int32_t v = *(int32_t *)(_dg->GetData() + _offset);
_offset += 4;
return v;
}
Expand All @@ -77,7 +77,7 @@ int32_t DatagramIterator::GetInt32() {
*/
uint32_t DatagramIterator::GetUint32() {
EnsureLength(4);
uint32_t v = *(uint32_t *)(_dg->GetData() + _offset);
const uint32_t v = *(uint32_t *)(_dg->GetData() + _offset);
_offset += 4;
return v;
}
Expand All @@ -88,7 +88,7 @@ uint32_t DatagramIterator::GetUint32() {
*/
int64_t DatagramIterator::GetInt64() {
EnsureLength(8);
int64_t v = *(int64_t *)(_dg->GetData() + _offset);
const int64_t v = *(int64_t *)(_dg->GetData() + _offset);
_offset += 8;
return v;
}
Expand All @@ -99,7 +99,7 @@ int64_t DatagramIterator::GetInt64() {
*/
uint64_t DatagramIterator::GetUint64() {
EnsureLength(8);
uint64_t v = *(uint64_t *)(_dg->GetData() + _offset);
const uint64_t v = *(uint64_t *)(_dg->GetData() + _offset);
_offset += 8;
return v;
}
Expand All @@ -110,7 +110,7 @@ uint64_t DatagramIterator::GetUint64() {
*/
float DatagramIterator::GetFloat32() {
EnsureLength(4);
float v = *(float *)(_dg->GetData() + _offset);
const float v = *(float *)(_dg->GetData() + _offset);
_offset += 4;
return v;
}
Expand All @@ -121,7 +121,7 @@ float DatagramIterator::GetFloat32() {
*/
double DatagramIterator::GetFloat64() {
EnsureLength(8);
double v = *(double *)(_dg->GetData() + _offset);
const double v = *(double *)(_dg->GetData() + _offset);
_offset += 8;
return v;
}
Expand All @@ -131,7 +131,7 @@ double DatagramIterator::GetFloat64() {
* @return
*/
std::string DatagramIterator::GetString() {
uint16_t length = GetUint16();
const uint16_t length = GetUint16();
EnsureLength(length);
std::string str((char *)(_dg->GetData() + _offset), length);
_offset += length;
Expand All @@ -143,7 +143,7 @@ std::string DatagramIterator::GetString() {
* @return
*/
std::vector<uint8_t> DatagramIterator::GetBlob() {
uint16_t length = GetUint16();
const uint16_t length = GetUint16();
EnsureLength(length);
std::vector<uint8_t> blob(_dg->GetData() + _offset,
_dg->GetData() + _offset + length);
Expand Down Expand Up @@ -188,7 +188,7 @@ std::shared_ptr<Datagram> DatagramIterator::GetUnderlyingDatagram() {
* @param field
* @param buffer
*/
void DatagramIterator::UnpackField(DCPackerInterface *field,
void DatagramIterator::UnpackField(const DCPackerInterface *field,
std::vector<uint8_t> &buffer) {
// If the field has a fixed size in bytes (int, uint, float, etc.)
// we can unpack data directly using that size.
Expand Down Expand Up @@ -229,7 +229,7 @@ void DatagramIterator::UnpackField(DCPackerInterface *field,
}

// Otherwise, if the field is non-atomic, process each nested field.
int numNested = field->get_num_nested_fields();
const int numNested = field->get_num_nested_fields();
for (int i = 0; i < numNested; ++i) {
UnpackField(field->get_nested_field(i), buffer);
}
Expand Down Expand Up @@ -259,7 +259,7 @@ void DatagramIterator::Seek(const size_t &offset) { _offset = offset; }
void DatagramIterator::SeekPayload() {
_offset = 0;

uint8_t channels = GetUint8();
const uint8_t channels = GetUint8();
for (int i = 0; i < channels; ++i) {
GetUint64();
}
Expand All @@ -269,11 +269,11 @@ void DatagramIterator::SeekPayload() {
* Skips reading past a packed field.
* @param field
*/
void DatagramIterator::SkipField(DCPackerInterface *field) {
void DatagramIterator::SkipField(const DCPackerInterface *field) {
// If the field has a fixed size in bytes (int, uint, float, etc.)
// we can use that as our offset.
if (field->has_fixed_byte_size()) {
auto length = field->get_fixed_byte_size();
const size_t length = field->get_fixed_byte_size();
EnsureLength(length);
_offset += length;
return;
Expand Down Expand Up @@ -304,7 +304,7 @@ void DatagramIterator::SkipField(DCPackerInterface *field) {
}

// Otherwise, if the field is non-atomic, skip each nested field.
int numNested = field->get_num_nested_fields();
const int numNested = field->get_num_nested_fields();
for (int i = 0; i < numNested; ++i) {
SkipField(field->get_nested_field(i));
}
Expand All @@ -314,23 +314,23 @@ void DatagramIterator::SkipField(DCPackerInterface *field) {
* Returns the remaining read size in bytes.
* @return
*/
size_t DatagramIterator::GetRemainingSize() { return _dg->Size() - _offset; }
size_t DatagramIterator::GetRemainingSize() const { return _dg->Size() - _offset; }

/**
* Returns the remaining bytes to be read.
* @return
*/
std::vector<uint8_t> DatagramIterator::GetRemainingBytes() {
size_t length = GetRemainingSize();
const size_t length = GetRemainingSize();
std::vector<uint8_t> data(_dg->GetData() + _offset,
_dg->GetData() + _offset + length);
_offset += length;
return data;
}

void DatagramIterator::EnsureLength(const size_t &length) {
void DatagramIterator::EnsureLength(const size_t &length) const {
// Make sure we don't overflow reading.
size_t newOffset = _offset + length;
const size_t newOffset = _offset + length;
if (newOffset > _dg->Size()) {
throw DatagramIteratorEOF(
std::format("DatagramIterator tried to read past Datagram length! "
Expand Down
Loading

0 comments on commit 4e3a162

Please sign in to comment.