Skip to content

Commit

Permalink
cleanup: Use explicit constructors where possible.
Browse files Browse the repository at this point in the history
  • Loading branch information
iphydf committed Jan 13, 2025
1 parent f905d61 commit 2330304
Show file tree
Hide file tree
Showing 27 changed files with 62 additions and 74 deletions.
1 change: 1 addition & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Checks: "-*
, google-explicit-constructor
, modernize-avoid-bind
, modernize-deprecated-headers
, modernize-loop-convert
Expand Down
2 changes: 1 addition & 1 deletion audio/include/audio/iaudiosink.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class IAudioSink
virtual void startLoop() = 0;
virtual void stopLoop() = 0;

virtual operator bool() const = 0;
virtual explicit operator bool() const = 0;

signals:
DECLARE_SIGNAL(finishedPlaying, void);
Expand Down
2 changes: 1 addition & 1 deletion audio/include/audio/iaudiosource.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class IAudioSource : public QObject
public:
~IAudioSource() override = default;

virtual operator bool() const = 0;
virtual explicit operator bool() const = 0;

signals:
void frameAvailable(const int16_t* pcm, size_t sample_count, uint8_t channels,
Expand Down
2 changes: 1 addition & 1 deletion audio/src/backend/alsink.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class AlSink : public QObject, public IAudioSink
void startLoop() override;
void stopLoop() override;

operator bool() const override;
explicit operator bool() const override;

uint getSourceId() const;
void kill();
Expand Down
4 changes: 2 additions & 2 deletions audio/src/backend/alsource.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ class AlSource : public IAudioSource
{
Q_OBJECT
public:
AlSource(OpenAL& al);
explicit AlSource(OpenAL& al);
AlSource(AlSource& src) = delete;
AlSource& operator=(const AlSource&) = delete;
AlSource(AlSource&& other) = delete;
AlSource& operator=(AlSource&& other) = delete;
~AlSource() override;

operator bool() const override;
explicit operator bool() const override;

void kill();

Expand Down
6 changes: 3 additions & 3 deletions src/core/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ ToxCorePtr Core::makeToxCore(const QByteArray& savedata, const ICoreSettings& se
}

Tox_Err_New tox_err;
core->tox = ToxPtr(tox_new(*toxOptions, &tox_err));
core->tox = ToxPtr(tox_new(toxOptions->get(), &tox_err));

switch (tox_err) {
case TOX_ERR_NEW_OK:
Expand All @@ -151,7 +151,7 @@ ToxCorePtr Core::makeToxCore(const QByteArray& savedata, const ICoreSettings& se
case TOX_ERR_NEW_PORT_ALLOC:
if (toxOptions->getIPv6Enabled()) {
toxOptions->setIPv6Enabled(false);
core->tox = ToxPtr(tox_new(*toxOptions, &tox_err));
core->tox = ToxPtr(tox_new(toxOptions->get(), &tox_err));
if (tox_err == TOX_ERR_NEW_OK) {
qWarning() << "Core failed to start with IPv6, falling back to IPv4. LAN discovery "
"may not work properly.";
Expand Down Expand Up @@ -541,7 +541,7 @@ void Core::onConferencePeerListChange(Tox* tox, uint32_t conferenceId, void* vCo
{
std::ignore = tox;
const auto core = static_cast<Core*>(vCore);
qDebug("Conference %d peerlist changed", conferenceId);
qDebug("Conference %u peerlist changed", conferenceId);
// no saveRequest, this callback is called on every connection to conference peer, not just on brand new peers
emit core->conferencePeerlistChanged(conferenceId);
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/toxfileprogress.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
class ToxFileProgress
{
public:
ToxFileProgress(uint64_t filesize_, int samplePeriodMs_ = 4000);
explicit ToxFileProgress(uint64_t filesize_, int samplePeriodMs_ = 4000);

QTime lastSampleTime() const;
bool addSample(uint64_t bytesSent, QTime now = QTime::currentTime());
Expand Down
36 changes: 18 additions & 18 deletions src/core/toxoptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const char* ToxOptions::getProxyAddrData() const
return proxyAddrData.constData();
}

ToxOptions::operator Tox_Options*()
Tox_Options* ToxOptions::get()
{
return options;
}
Expand All @@ -71,13 +71,13 @@ std::unique_ptr<ToxOptions> ToxOptions::makeToxOptions(const QByteArray& savedat

auto toxOptions = std::unique_ptr<ToxOptions>(new ToxOptions(tox_opts, proxyAddr.toUtf8()));
// register log first, to get messages as early as possible
tox_options_set_log_callback(*toxOptions, ToxLogger::onLogMessage);
tox_options_set_log_callback(toxOptions->get(), ToxLogger::onLogMessage);

// savedata
tox_options_set_savedata_type(*toxOptions, savedata.isNull() ? TOX_SAVEDATA_TYPE_NONE
: TOX_SAVEDATA_TYPE_TOX_SAVE);
tox_options_set_savedata_data(*toxOptions, reinterpret_cast<const uint8_t*>(savedata.data()),
savedata.size());
tox_options_set_savedata_type(toxOptions->get(), savedata.isNull() ? TOX_SAVEDATA_TYPE_NONE
: TOX_SAVEDATA_TYPE_TOX_SAVE);
tox_options_set_savedata_data(toxOptions->get(),
reinterpret_cast<const uint8_t*>(savedata.data()), savedata.size());

// IPv6 needed for LAN discovery, but can crash some weird routers. On by default, can be
// disabled in options.
Expand All @@ -98,9 +98,9 @@ std::unique_ptr<ToxOptions> ToxOptions::makeToxOptions(const QByteArray& savedat
}

// No proxy by default
tox_options_set_proxy_type(*toxOptions, TOX_PROXY_TYPE_NONE);
tox_options_set_proxy_host(*toxOptions, nullptr);
tox_options_set_proxy_port(*toxOptions, 0);
tox_options_set_proxy_type(toxOptions->get(), TOX_PROXY_TYPE_NONE);
tox_options_set_proxy_host(toxOptions->get(), nullptr);
tox_options_set_proxy_port(toxOptions->get(), 0);

if (proxyType != ICoreSettings::ProxyType::ptNone) {
if (static_cast<uint32_t>(proxyAddr.length()) > tox_max_hostname_length()) {
Expand All @@ -109,13 +109,13 @@ std::unique_ptr<ToxOptions> ToxOptions::makeToxOptions(const QByteArray& savedat
qDebug() << "Using proxy" << proxyAddr << ":" << proxyPort;
// protection against changes in Tox_Proxy_Type enum
if (proxyType == ICoreSettings::ProxyType::ptSOCKS5) {
tox_options_set_proxy_type(*toxOptions, TOX_PROXY_TYPE_SOCKS5);
tox_options_set_proxy_type(toxOptions->get(), TOX_PROXY_TYPE_SOCKS5);
} else if (proxyType == ICoreSettings::ProxyType::ptHTTP) {
tox_options_set_proxy_type(*toxOptions, TOX_PROXY_TYPE_HTTP);
tox_options_set_proxy_type(toxOptions->get(), TOX_PROXY_TYPE_HTTP);
}

tox_options_set_proxy_host(*toxOptions, toxOptions->getProxyAddrData());
tox_options_set_proxy_port(*toxOptions, proxyPort);
tox_options_set_proxy_host(toxOptions->get(), toxOptions->getProxyAddrData());
tox_options_set_proxy_port(toxOptions->get(), proxyPort);

if (!forceTCP) {
qDebug() << "Proxy and UDP enabled. This is a security risk. Forcing TCP only.";
Expand All @@ -125,11 +125,11 @@ std::unique_ptr<ToxOptions> ToxOptions::makeToxOptions(const QByteArray& savedat
}

// network options
tox_options_set_udp_enabled(*toxOptions, !forceTCP);
tox_options_set_ipv6_enabled(*toxOptions, enableIPv6);
tox_options_set_local_discovery_enabled(*toxOptions, enableLanDiscovery);
tox_options_set_start_port(*toxOptions, 0);
tox_options_set_end_port(*toxOptions, 0);
tox_options_set_udp_enabled(toxOptions->get(), !forceTCP);
tox_options_set_ipv6_enabled(toxOptions->get(), enableIPv6);
tox_options_set_local_discovery_enabled(toxOptions->get(), enableLanDiscovery);
tox_options_set_start_port(toxOptions->get(), 0);
tox_options_set_end_port(toxOptions->get(), 0);

return toxOptions;
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/toxoptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ToxOptions
public:
~ToxOptions();
ToxOptions(ToxOptions&& from);
operator Tox_Options*();
Tox_Options* get();
const char* getProxyAddrData() const;
static std::unique_ptr<ToxOptions> makeToxOptions(const QByteArray& savedata,
const ICoreSettings& s);
Expand Down
2 changes: 1 addition & 1 deletion src/model/chatlogitem.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ChatLogItem

ChatLogItem(ToxPk sender_, const QString& displayName_, ChatLogFile file_);
ChatLogItem(ToxPk sender_, const QString& displayName_, ChatLogMessage message_);
ChatLogItem(SystemMessage message);
explicit ChatLogItem(SystemMessage message);
const ToxPk& getSender() const;
ContentType getContentType() const;
ChatLogFile& getContentAsFile();
Expand Down
2 changes: 1 addition & 1 deletion src/model/message.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class MessageProcessor
QRegularExpression pubKeyMention;
};

MessageProcessor(const SharedParams& sharedParams_);
explicit MessageProcessor(const SharedParams& sharedParams_);

std::vector<Message> processOutgoingMessage(bool isAction, const QString& content);
Message processIncomingCoreMessage(bool isAction, const QString& message);
Expand Down
34 changes: 10 additions & 24 deletions src/model/sessionchatlog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,20 @@

#include <QDebug>
#include <QtGlobal>
#include <mutex>

namespace {

constexpr QDate invalidDate;

/**
* lower_bound needs two way comparisons. This adaptor allows us to compare
* between a Message and QDateTime in both directions
* This function allows us to compare between a Message and QDateTime.
*/
struct MessageDateAdaptor
QDate toDate(const std::pair<const ChatLogIdx, ChatLogItem>& item)
{
static const QDateTime invalidDateTime;
MessageDateAdaptor(const std::pair<const ChatLogIdx, ChatLogItem>& item)
: timestamp(item.second.getContentType() == ChatLogItem::ContentType::message
? item.second.getContentAsMessage().message.timestamp
: invalidDateTime)
{
}

MessageDateAdaptor(const QDateTime& timestamp_)
: timestamp(timestamp_)
{
}

const QDateTime& timestamp;
};

const QDateTime MessageDateAdaptor::invalidDateTime;
return item.second.getContentType() == ChatLogItem::ContentType::message
? item.second.getContentAsMessage().message.timestamp.date()
: invalidDate;
}

/**
* @brief The search types all can be represented as some regular expression. This function
Expand Down Expand Up @@ -86,9 +73,8 @@ std::map<ChatLogIdx, ChatLogItem>::const_iterator
firstItemAfterDate(QDate date, const std::map<ChatLogIdx, ChatLogItem>& items)
{
return std::lower_bound(items.begin(), items.end(), date.startOfDay(),
[](const MessageDateAdaptor& a, const MessageDateAdaptor& b) {
return a.timestamp.date() < b.timestamp.date();
});
[](const std::pair<const ChatLogIdx, ChatLogItem>& a,
const QDateTime& b) { return toDate(a) < b.date(); });
}

QString resolveToxPk(FriendList& friendList, ConferenceList& conferenceList, const ToxPk& pk)
Expand Down
2 changes: 1 addition & 1 deletion src/net/avatarbroadcaster.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class AvatarBroadcaster : public QObject
{
Q_OBJECT
public:
AvatarBroadcaster(Core& _core);
explicit AvatarBroadcaster(Core& _core);

void setAvatar(QByteArray data);
void sendAvatarTo(uint32_t friendId);
Expand Down
4 changes: 2 additions & 2 deletions src/persistence/db/rawdatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ class RawDatabase
class Query
{
public:
Query(QString query_, QVector<QByteArray> blobs_ = {},
const std::function<void(RowId)>& insertCallback_ = {})
explicit Query(QString query_, QVector<QByteArray> blobs_ = {},
const std::function<void(RowId)>& insertCallback_ = {})
: query{query_.toUtf8()}
, blobs{std::move(blobs_)}
, insertCallback{insertCallback_}
Expand Down
2 changes: 1 addition & 1 deletion src/persistence/db/rawdatabaseimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ void RawDatabaseImpl::compileAndExecute(Transaction& trans)

// Add transaction commands if necessary
if (trans.queries.size() > 1) {
trans.queries.insert(trans.queries.begin(), {"BEGIN;"});
trans.queries.insert(trans.queries.begin(), Query{"BEGIN;"});
trans.queries.emplace_back("COMMIT;");
}

Expand Down
6 changes: 3 additions & 3 deletions src/persistence/history.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ RawDatabase::Query generateHistoryTableInsertion(char type, const QDateTime& tim
.arg(time.toMSecsSinceEpoch());
addChatIdSubQuery(queryString, boundParams, chatId);
queryString += ");";
return {queryString, boundParams};
return RawDatabase::Query{queryString, boundParams};
}

/**
Expand Down Expand Up @@ -418,7 +418,7 @@ RawDatabase::Query History::generateFileFinished(RowId id, bool success, const Q
{
auto file_state = success ? ToxFile::FINISHED : ToxFile::CANCELED;
if (filePath.length()) {
return {
return RawDatabase::Query{
QStringLiteral( //
"UPDATE file_transfers "
"SET file_state = %1, file_path = ?, file_hash = ?"
Expand All @@ -428,7 +428,7 @@ RawDatabase::Query History::generateFileFinished(RowId id, bool success, const Q
QVector<QByteArray>{filePath.toUtf8(), fileHash},
};
}
return {
return RawDatabase::Query{
QStringLiteral( //
"UPDATE file_transfers "
"SET file_state = %1 "
Expand Down
6 changes: 3 additions & 3 deletions src/persistence/history.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@ enum class HistMessageContentType
class HistMessageContent
{
public:
HistMessageContent(QString message)
explicit HistMessageContent(QString message)
: data(std::make_shared<QString>(std::move(message)))
, type(HistMessageContentType::message)
{
}

HistMessageContent(ToxFile file)
explicit HistMessageContent(ToxFile file)
: data(std::make_shared<ToxFile>(std::move(file)))
, type(HistMessageContentType::file)
{
}

HistMessageContent(SystemMessage systemMessage)
explicit HistMessageContent(SystemMessage systemMessage)
: data(std::make_shared<SystemMessage>(std::move(systemMessage)))
, type(HistMessageContentType::system)
{
Expand Down
2 changes: 1 addition & 1 deletion src/persistence/paths.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Paths
NonPortable /** Force non-portable mode */
};

Paths(Portable mode = Portable::Auto);
explicit Paths(Portable mode = Portable::Auto);

bool setPortable(bool portable);
bool isPortable() const;
Expand Down
2 changes: 1 addition & 1 deletion src/persistence/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ private slots:
struct friendProp
{
friendProp() = delete;
friendProp(QString addr_)
explicit friendProp(QString addr_)
: addr(std::move(addr_))
{
}
Expand Down
2 changes: 1 addition & 1 deletion src/persistence/settingsserializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class SettingsSerializer
ArrayValue = 3,
ArrayEnd = 4,
};
SettingsSerializer(QString filePath_, const ToxEncrypt* passKey_ = nullptr);
explicit SettingsSerializer(QString filePath_, const ToxEncrypt* passKey_ = nullptr);

static bool isSerializedFormat(QString filePath);

Expand Down
2 changes: 1 addition & 1 deletion src/video/videosurface.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class VideoSurface : public QWidget
Q_OBJECT

public:
VideoSurface(QPixmap avatar_, QWidget* parent = nullptr, bool expanding_ = false);
explicit VideoSurface(QPixmap avatar_, QWidget* parent = nullptr, bool expanding_ = false);
VideoSurface(const QPixmap& avatar_, VideoSource* source_, QWidget* parent = nullptr);
~VideoSurface() override;

Expand Down
2 changes: 1 addition & 1 deletion src/widget/form/filetransferlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Model : public QAbstractTableModel
{
Q_OBJECT
public:
Model(FriendList& friendList, QObject* parent = nullptr);
explicit Model(FriendList& friendList, QObject* parent = nullptr);
~Model() override = default;

void onFileUpdated(const ToxFile& file);
Expand Down
2 changes: 1 addition & 1 deletion src/widget/imagepreviewwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
class ImagePreviewButton : public QPushButton
{
public:
ImagePreviewButton(QWidget* parent = nullptr)
explicit ImagePreviewButton(QWidget* parent = nullptr)
: QPushButton(parent)
{
}
Expand Down
2 changes: 1 addition & 1 deletion src/widget/searchform.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class LineEdit : public QLineEdit
Q_OBJECT

public:
LineEdit(QWidget* parent = nullptr);
explicit LineEdit(QWidget* parent = nullptr);

protected:
void keyPressEvent(QKeyEvent* event) final;
Expand Down
3 changes: 2 additions & 1 deletion src/widget/tool/activatedialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class ActivateDialog : public QDialog
{
Q_OBJECT
public:
ActivateDialog(Style& style, QWidget* parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags());
explicit ActivateDialog(Style& style, QWidget* parent = nullptr,
Qt::WindowFlags f = Qt::WindowFlags());
bool event(QEvent* event) override;

public slots:
Expand Down
Loading

0 comments on commit 2330304

Please sign in to comment.