refactor: more constexpr (#3744)
Make more simple functions constexpr where possible.
This commit is contained in:
parent
f8203148f0
commit
6ca0ce683a
|
@ -136,6 +136,25 @@ struct tr_handshake
|
|||
return io->isIncoming();
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr uint32_t cryptoProvide() const
|
||||
{
|
||||
uint32_t provide = 0;
|
||||
|
||||
switch (encryption_mode)
|
||||
{
|
||||
case TR_ENCRYPTION_REQUIRED:
|
||||
case TR_ENCRYPTION_PREFERRED:
|
||||
provide |= CRYPTO_PROVIDE_CRYPTO;
|
||||
break;
|
||||
|
||||
case TR_CLEAR_PREFERRED:
|
||||
provide |= CRYPTO_PROVIDE_CRYPTO | CRYPTO_PROVIDE_PLAINTEXT;
|
||||
break;
|
||||
}
|
||||
|
||||
return provide;
|
||||
}
|
||||
|
||||
std::unique_ptr<tr_handshake_mediator> const mediator;
|
||||
|
||||
bool haveReadAnythingFromPeer = false;
|
||||
|
@ -317,25 +336,6 @@ static void sendYa(tr_handshake* handshake)
|
|||
setReadState(handshake, AWAITING_YB);
|
||||
}
|
||||
|
||||
static uint32_t getCryptoProvide(tr_handshake const* handshake)
|
||||
{
|
||||
uint32_t provide = 0;
|
||||
|
||||
switch (handshake->encryption_mode)
|
||||
{
|
||||
case TR_ENCRYPTION_REQUIRED:
|
||||
case TR_ENCRYPTION_PREFERRED:
|
||||
provide |= CRYPTO_PROVIDE_CRYPTO;
|
||||
break;
|
||||
|
||||
case TR_CLEAR_PREFERRED:
|
||||
provide |= CRYPTO_PROVIDE_CRYPTO | CRYPTO_PROVIDE_PLAINTEXT;
|
||||
break;
|
||||
}
|
||||
|
||||
return provide;
|
||||
}
|
||||
|
||||
static constexpr uint32_t getCryptoSelect(tr_encryption_mode encryption_mode, uint32_t crypto_provide)
|
||||
{
|
||||
auto choices = std::array<uint32_t, 2>{};
|
||||
|
@ -439,7 +439,7 @@ static ReadState readYb(tr_handshake* handshake, struct evbuffer* inbuf)
|
|||
handshake->io->writeBuf(outbuf, false);
|
||||
handshake->io->encryptInit(handshake->io->isIncoming(), handshake->dh, *info_hash);
|
||||
evbuffer_add(outbuf, std::data(VC), std::size(VC));
|
||||
evbuffer_add_uint32(outbuf, getCryptoProvide(handshake));
|
||||
evbuffer_add_uint32(outbuf, handshake->cryptoProvide());
|
||||
evbuffer_add_uint16(outbuf, 0);
|
||||
|
||||
/* ENCRYPT len(IA)), ENCRYPT(IA) */
|
||||
|
@ -514,7 +514,7 @@ static ReadState readCryptoSelect(tr_handshake* handshake, struct evbuffer* inbu
|
|||
handshake->crypto_select = crypto_select;
|
||||
tr_logAddTraceHand(handshake, fmt::format("crypto select is {}", crypto_select));
|
||||
|
||||
if ((crypto_select & getCryptoProvide(handshake)) == 0)
|
||||
if ((crypto_select & handshake->cryptoProvide()) == 0)
|
||||
{
|
||||
tr_logAddTraceHand(handshake, "peer selected an encryption option we didn't offer");
|
||||
return tr_handshakeDone(handshake, false);
|
||||
|
|
|
@ -232,7 +232,7 @@ public:
|
|||
scripts_enabled_[i] = enabled;
|
||||
}
|
||||
|
||||
[[nodiscard]] auto useScript(TrScript i) const
|
||||
[[nodiscard]] constexpr auto useScript(TrScript i) const
|
||||
{
|
||||
return scripts_enabled_[i];
|
||||
}
|
||||
|
@ -274,7 +274,7 @@ public:
|
|||
|
||||
[[nodiscard]] bool useRpcWhitelist() const;
|
||||
|
||||
[[nodiscard]] auto externalIP() const noexcept
|
||||
[[nodiscard]] constexpr auto externalIP() const noexcept
|
||||
{
|
||||
return external_ip_;
|
||||
}
|
||||
|
@ -432,12 +432,12 @@ public:
|
|||
|
||||
/// stats
|
||||
|
||||
[[nodiscard]] auto& stats() noexcept
|
||||
[[nodiscard]] constexpr auto& stats() noexcept
|
||||
{
|
||||
return session_stats_;
|
||||
}
|
||||
|
||||
[[nodiscard]] auto const& stats() const noexcept
|
||||
[[nodiscard]] constexpr auto const& stats() const noexcept
|
||||
{
|
||||
return session_stats_;
|
||||
}
|
||||
|
@ -518,109 +518,109 @@ public:
|
|||
tr_bindinfo bind_ipv4 = tr_bindinfo{ tr_inaddr_any };
|
||||
tr_bindinfo bind_ipv6 = tr_bindinfo{ tr_in6addr_any };
|
||||
|
||||
[[nodiscard]] auto constexpr queueEnabled(tr_direction dir) const noexcept
|
||||
[[nodiscard]] constexpr auto queueEnabled(tr_direction dir) const noexcept
|
||||
{
|
||||
return queue_enabled_[dir];
|
||||
}
|
||||
|
||||
[[nodiscard]] auto constexpr queueSize(tr_direction dir) const noexcept
|
||||
[[nodiscard]] constexpr auto queueSize(tr_direction dir) const noexcept
|
||||
{
|
||||
return queue_size_[dir];
|
||||
}
|
||||
|
||||
[[nodiscard]] auto constexpr queueStalledEnabled() const noexcept
|
||||
[[nodiscard]] constexpr auto queueStalledEnabled() const noexcept
|
||||
{
|
||||
return queue_stalled_enabled_;
|
||||
}
|
||||
|
||||
[[nodiscard]] auto constexpr queueStalledMinutes() const noexcept
|
||||
[[nodiscard]] constexpr auto queueStalledMinutes() const noexcept
|
||||
{
|
||||
return queue_stalled_minutes_;
|
||||
}
|
||||
|
||||
[[nodiscard]] auto constexpr peerLimit() const noexcept
|
||||
[[nodiscard]] constexpr auto peerLimit() const noexcept
|
||||
{
|
||||
return peer_limit_;
|
||||
}
|
||||
|
||||
[[nodiscard]] auto constexpr peerLimitPerTorrent() const noexcept
|
||||
[[nodiscard]] constexpr auto peerLimitPerTorrent() const noexcept
|
||||
{
|
||||
return peer_limit_per_torrent_;
|
||||
}
|
||||
|
||||
[[nodiscard]] auto constexpr uploadSlotsPerTorrent() const noexcept
|
||||
[[nodiscard]] constexpr auto uploadSlotsPerTorrent() const noexcept
|
||||
{
|
||||
return upload_slots_per_torrent_;
|
||||
}
|
||||
|
||||
[[nodiscard]] auto constexpr isClosing() const noexcept
|
||||
[[nodiscard]] constexpr auto isClosing() const noexcept
|
||||
{
|
||||
return is_closing_;
|
||||
}
|
||||
|
||||
[[nodiscard]] auto constexpr isClosed() const noexcept
|
||||
[[nodiscard]] constexpr auto isClosed() const noexcept
|
||||
{
|
||||
return is_closed_;
|
||||
}
|
||||
|
||||
[[nodiscard]] auto constexpr encryptionMode() const noexcept
|
||||
[[nodiscard]] constexpr auto encryptionMode() const noexcept
|
||||
{
|
||||
return encryption_mode_;
|
||||
}
|
||||
|
||||
[[nodiscard]] auto constexpr preallocationMode() const noexcept
|
||||
[[nodiscard]] constexpr auto preallocationMode() const noexcept
|
||||
{
|
||||
return preallocation_mode_;
|
||||
}
|
||||
|
||||
[[nodiscard]] auto constexpr shouldScrapePausedTorrents() const noexcept
|
||||
[[nodiscard]] constexpr auto shouldScrapePausedTorrents() const noexcept
|
||||
{
|
||||
return should_scrape_paused_torrents_;
|
||||
}
|
||||
|
||||
[[nodiscard]] auto constexpr shouldPauseAddedTorrents() const noexcept
|
||||
[[nodiscard]] constexpr auto shouldPauseAddedTorrents() const noexcept
|
||||
{
|
||||
return should_pause_added_torrents_;
|
||||
}
|
||||
|
||||
[[nodiscard]] auto constexpr shouldDeleteSource() const noexcept
|
||||
[[nodiscard]] constexpr auto shouldDeleteSource() const noexcept
|
||||
{
|
||||
return should_pause_added_torrents_;
|
||||
}
|
||||
|
||||
[[nodiscard]] auto constexpr allowsDHT() const noexcept
|
||||
[[nodiscard]] constexpr auto allowsDHT() const noexcept
|
||||
{
|
||||
return is_dht_enabled_;
|
||||
}
|
||||
|
||||
[[nodiscard]] auto constexpr allowsLPD() const noexcept
|
||||
[[nodiscard]] constexpr auto allowsLPD() const noexcept
|
||||
{
|
||||
return is_lpd_enabled_;
|
||||
}
|
||||
|
||||
[[nodiscard]] auto constexpr allowsPEX() const noexcept
|
||||
[[nodiscard]] constexpr auto allowsPEX() const noexcept
|
||||
{
|
||||
return is_pex_enabled_;
|
||||
}
|
||||
|
||||
[[nodiscard]] auto constexpr allowsTCP() const noexcept
|
||||
[[nodiscard]] constexpr auto allowsTCP() const noexcept
|
||||
{
|
||||
return is_tcp_enabled_;
|
||||
}
|
||||
|
||||
[[nodiscard]] bool allowsUTP() const noexcept;
|
||||
|
||||
[[nodiscard]] auto constexpr allowsPrefetch() const noexcept
|
||||
[[nodiscard]] constexpr auto allowsPrefetch() const noexcept
|
||||
{
|
||||
return is_prefetch_enabled_;
|
||||
}
|
||||
|
||||
[[nodiscard]] auto constexpr isIdleLimited() const noexcept
|
||||
[[nodiscard]] constexpr auto isIdleLimited() const noexcept
|
||||
{
|
||||
return is_idle_limited_;
|
||||
}
|
||||
|
||||
[[nodiscard]] auto constexpr idleLimitMinutes() const noexcept
|
||||
[[nodiscard]] constexpr auto idleLimitMinutes() const noexcept
|
||||
{
|
||||
return idle_limit_minutes_;
|
||||
}
|
||||
|
@ -673,7 +673,7 @@ public:
|
|||
|
||||
[[nodiscard]] std::optional<unsigned int> activeSpeedLimitBps(tr_direction dir) const noexcept;
|
||||
|
||||
[[nodiscard]] auto isIncompleteFileNamingEnabled() const noexcept
|
||||
[[nodiscard]] constexpr auto isIncompleteFileNamingEnabled() const noexcept
|
||||
{
|
||||
return is_incomplete_file_naming_enabled_;
|
||||
}
|
||||
|
@ -683,7 +683,7 @@ public:
|
|||
return is_port_random_;
|
||||
}
|
||||
|
||||
[[nodiscard]] auto constexpr isRatioLimited() const noexcept
|
||||
[[nodiscard]] constexpr auto isRatioLimited() const noexcept
|
||||
{
|
||||
return is_ratio_limited_;
|
||||
}
|
||||
|
|
|
@ -42,19 +42,19 @@ public:
|
|||
return add(current(), old_);
|
||||
}
|
||||
|
||||
void addUploaded(uint32_t n_bytes) noexcept
|
||||
constexpr void addUploaded(uint32_t n_bytes) noexcept
|
||||
{
|
||||
single_.uploadedBytes += n_bytes;
|
||||
is_dirty_ = true;
|
||||
}
|
||||
|
||||
void addDownloaded(uint32_t n_bytes) noexcept
|
||||
constexpr void addDownloaded(uint32_t n_bytes) noexcept
|
||||
{
|
||||
single_.downloadedBytes += n_bytes;
|
||||
is_dirty_ = true;
|
||||
}
|
||||
|
||||
void addFileCreated() noexcept
|
||||
constexpr void addFileCreated() noexcept
|
||||
{
|
||||
++single_.filesAdded;
|
||||
is_dirty_ = true;
|
||||
|
|
|
@ -87,7 +87,7 @@ public:
|
|||
{
|
||||
return blockInfo().blockSize(block);
|
||||
}
|
||||
[[nodiscard]] auto blockSpanForPiece(tr_piece_index_t piece) const
|
||||
[[nodiscard]] constexpr auto blockSpanForPiece(tr_piece_index_t piece) const
|
||||
{
|
||||
return blockInfo().blockSpanForPiece(piece);
|
||||
}
|
||||
|
|
|
@ -2732,12 +2732,6 @@ void tr_torrent::markChanged()
|
|||
this->anyDate = tr_time();
|
||||
}
|
||||
|
||||
void tr_torrent::setDateActive(time_t t)
|
||||
{
|
||||
this->activityDate = t;
|
||||
this->anyDate = std::max(this->anyDate, this->activityDate);
|
||||
}
|
||||
|
||||
void tr_torrent::setBlocks(tr_bitfield blocks)
|
||||
{
|
||||
this->completion.setBlocks(std::move(blocks));
|
||||
|
|
|
@ -154,7 +154,7 @@ public:
|
|||
{
|
||||
return metainfo_.blockSize(block);
|
||||
}
|
||||
[[nodiscard]] auto blockSpanForPiece(tr_piece_index_t piece) const
|
||||
[[nodiscard]] constexpr auto blockSpanForPiece(tr_piece_index_t piece) const
|
||||
{
|
||||
return metainfo_.blockSpanForPiece(piece);
|
||||
}
|
||||
|
@ -415,32 +415,32 @@ public:
|
|||
metainfo_.setName(name);
|
||||
}
|
||||
|
||||
[[nodiscard]] auto const& name() const noexcept
|
||||
[[nodiscard]] constexpr auto const& name() const noexcept
|
||||
{
|
||||
return metainfo_.name();
|
||||
}
|
||||
|
||||
[[nodiscard]] auto const& infoHash() const noexcept
|
||||
[[nodiscard]] constexpr auto const& infoHash() const noexcept
|
||||
{
|
||||
return metainfo_.infoHash();
|
||||
}
|
||||
|
||||
[[nodiscard]] auto isPrivate() const noexcept
|
||||
[[nodiscard]] constexpr auto isPrivate() const noexcept
|
||||
{
|
||||
return metainfo_.isPrivate();
|
||||
}
|
||||
|
||||
[[nodiscard]] auto isPublic() const noexcept
|
||||
[[nodiscard]] constexpr auto isPublic() const noexcept
|
||||
{
|
||||
return !this->isPrivate();
|
||||
}
|
||||
|
||||
[[nodiscard]] auto const& infoHashString() const noexcept
|
||||
[[nodiscard]] constexpr auto const& infoHashString() const noexcept
|
||||
{
|
||||
return metainfo_.infoHashString();
|
||||
}
|
||||
|
||||
[[nodiscard]] auto dateCreated() const noexcept
|
||||
[[nodiscard]] constexpr auto dateCreated() const noexcept
|
||||
{
|
||||
return metainfo_.dateCreated();
|
||||
}
|
||||
|
@ -465,27 +465,27 @@ public:
|
|||
return metainfo_.magnet();
|
||||
}
|
||||
|
||||
[[nodiscard]] auto const& comment() const noexcept
|
||||
[[nodiscard]] constexpr auto const& comment() const noexcept
|
||||
{
|
||||
return metainfo_.comment();
|
||||
}
|
||||
|
||||
[[nodiscard]] auto const& creator() const noexcept
|
||||
[[nodiscard]] constexpr auto const& creator() const noexcept
|
||||
{
|
||||
return metainfo_.creator();
|
||||
}
|
||||
|
||||
[[nodiscard]] auto const& source() const noexcept
|
||||
[[nodiscard]] constexpr auto const& source() const noexcept
|
||||
{
|
||||
return metainfo_.source();
|
||||
}
|
||||
|
||||
[[nodiscard]] auto infoDictSize() const noexcept
|
||||
[[nodiscard]] constexpr auto infoDictSize() const noexcept
|
||||
{
|
||||
return metainfo_.infoDictSize();
|
||||
}
|
||||
|
||||
[[nodiscard]] auto infoDictOffset() const noexcept
|
||||
[[nodiscard]] constexpr auto infoDictOffset() const noexcept
|
||||
{
|
||||
return metainfo_.infoDictOffset();
|
||||
}
|
||||
|
@ -515,17 +515,17 @@ public:
|
|||
return this->isDone() ? TR_UP : TR_DOWN;
|
||||
}
|
||||
|
||||
[[nodiscard]] auto allowsPex() const noexcept
|
||||
[[nodiscard]] constexpr auto allowsPex() const noexcept
|
||||
{
|
||||
return this->isPublic() && this->session->allowsPEX();
|
||||
}
|
||||
|
||||
[[nodiscard]] auto allowsDht() const
|
||||
[[nodiscard]] constexpr auto allowsDht() const noexcept
|
||||
{
|
||||
return this->isPublic() && this->session->allowsDHT();
|
||||
}
|
||||
|
||||
[[nodiscard]] auto allowsLpd() const // local peer discovery
|
||||
[[nodiscard]] constexpr auto allowsLpd() const noexcept // local peer discovery
|
||||
{
|
||||
return this->isPublic() && this->session->allowsLPD();
|
||||
}
|
||||
|
@ -586,7 +586,15 @@ public:
|
|||
return unique_id_;
|
||||
}
|
||||
|
||||
void setDateActive(time_t t);
|
||||
constexpr void setDateActive(time_t t) noexcept
|
||||
{
|
||||
this->activityDate = t;
|
||||
|
||||
if (this->anyDate < t)
|
||||
{
|
||||
this->anyDate = t;
|
||||
}
|
||||
}
|
||||
|
||||
void setLabels(std::vector<tr_quark> const& new_labels);
|
||||
|
||||
|
@ -594,7 +602,7 @@ public:
|
|||
torrent's content than any other mime-type. */
|
||||
[[nodiscard]] std::string_view primaryMimeType() const;
|
||||
|
||||
void setDirty()
|
||||
constexpr void setDirty() noexcept
|
||||
{
|
||||
this->isDirty = true;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue