refactor: more constexpr (#3744)

Make more simple functions constexpr where possible.
This commit is contained in:
Charles Kerr 2022-08-31 17:33:51 -05:00 committed by GitHub
parent f8203148f0
commit 6ca0ce683a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 77 additions and 75 deletions

View File

@ -136,6 +136,25 @@ struct tr_handshake
return io->isIncoming(); 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; std::unique_ptr<tr_handshake_mediator> const mediator;
bool haveReadAnythingFromPeer = false; bool haveReadAnythingFromPeer = false;
@ -317,25 +336,6 @@ static void sendYa(tr_handshake* handshake)
setReadState(handshake, AWAITING_YB); 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) static constexpr uint32_t getCryptoSelect(tr_encryption_mode encryption_mode, uint32_t crypto_provide)
{ {
auto choices = std::array<uint32_t, 2>{}; 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->writeBuf(outbuf, false);
handshake->io->encryptInit(handshake->io->isIncoming(), handshake->dh, *info_hash); handshake->io->encryptInit(handshake->io->isIncoming(), handshake->dh, *info_hash);
evbuffer_add(outbuf, std::data(VC), std::size(VC)); 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); evbuffer_add_uint16(outbuf, 0);
/* ENCRYPT len(IA)), ENCRYPT(IA) */ /* ENCRYPT len(IA)), ENCRYPT(IA) */
@ -514,7 +514,7 @@ static ReadState readCryptoSelect(tr_handshake* handshake, struct evbuffer* inbu
handshake->crypto_select = crypto_select; handshake->crypto_select = crypto_select;
tr_logAddTraceHand(handshake, fmt::format("crypto select is {}", 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"); tr_logAddTraceHand(handshake, "peer selected an encryption option we didn't offer");
return tr_handshakeDone(handshake, false); return tr_handshakeDone(handshake, false);

View File

@ -232,7 +232,7 @@ public:
scripts_enabled_[i] = enabled; scripts_enabled_[i] = enabled;
} }
[[nodiscard]] auto useScript(TrScript i) const [[nodiscard]] constexpr auto useScript(TrScript i) const
{ {
return scripts_enabled_[i]; return scripts_enabled_[i];
} }
@ -274,7 +274,7 @@ public:
[[nodiscard]] bool useRpcWhitelist() const; [[nodiscard]] bool useRpcWhitelist() const;
[[nodiscard]] auto externalIP() const noexcept [[nodiscard]] constexpr auto externalIP() const noexcept
{ {
return external_ip_; return external_ip_;
} }
@ -432,12 +432,12 @@ public:
/// stats /// stats
[[nodiscard]] auto& stats() noexcept [[nodiscard]] constexpr auto& stats() noexcept
{ {
return session_stats_; return session_stats_;
} }
[[nodiscard]] auto const& stats() const noexcept [[nodiscard]] constexpr auto const& stats() const noexcept
{ {
return session_stats_; return session_stats_;
} }
@ -518,109 +518,109 @@ public:
tr_bindinfo bind_ipv4 = tr_bindinfo{ tr_inaddr_any }; tr_bindinfo bind_ipv4 = tr_bindinfo{ tr_inaddr_any };
tr_bindinfo bind_ipv6 = tr_bindinfo{ tr_in6addr_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]; 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]; return queue_size_[dir];
} }
[[nodiscard]] auto constexpr queueStalledEnabled() const noexcept [[nodiscard]] constexpr auto queueStalledEnabled() const noexcept
{ {
return queue_stalled_enabled_; return queue_stalled_enabled_;
} }
[[nodiscard]] auto constexpr queueStalledMinutes() const noexcept [[nodiscard]] constexpr auto queueStalledMinutes() const noexcept
{ {
return queue_stalled_minutes_; return queue_stalled_minutes_;
} }
[[nodiscard]] auto constexpr peerLimit() const noexcept [[nodiscard]] constexpr auto peerLimit() const noexcept
{ {
return peer_limit_; return peer_limit_;
} }
[[nodiscard]] auto constexpr peerLimitPerTorrent() const noexcept [[nodiscard]] constexpr auto peerLimitPerTorrent() const noexcept
{ {
return peer_limit_per_torrent_; return peer_limit_per_torrent_;
} }
[[nodiscard]] auto constexpr uploadSlotsPerTorrent() const noexcept [[nodiscard]] constexpr auto uploadSlotsPerTorrent() const noexcept
{ {
return upload_slots_per_torrent_; return upload_slots_per_torrent_;
} }
[[nodiscard]] auto constexpr isClosing() const noexcept [[nodiscard]] constexpr auto isClosing() const noexcept
{ {
return is_closing_; return is_closing_;
} }
[[nodiscard]] auto constexpr isClosed() const noexcept [[nodiscard]] constexpr auto isClosed() const noexcept
{ {
return is_closed_; return is_closed_;
} }
[[nodiscard]] auto constexpr encryptionMode() const noexcept [[nodiscard]] constexpr auto encryptionMode() const noexcept
{ {
return encryption_mode_; return encryption_mode_;
} }
[[nodiscard]] auto constexpr preallocationMode() const noexcept [[nodiscard]] constexpr auto preallocationMode() const noexcept
{ {
return preallocation_mode_; return preallocation_mode_;
} }
[[nodiscard]] auto constexpr shouldScrapePausedTorrents() const noexcept [[nodiscard]] constexpr auto shouldScrapePausedTorrents() const noexcept
{ {
return should_scrape_paused_torrents_; return should_scrape_paused_torrents_;
} }
[[nodiscard]] auto constexpr shouldPauseAddedTorrents() const noexcept [[nodiscard]] constexpr auto shouldPauseAddedTorrents() const noexcept
{ {
return should_pause_added_torrents_; return should_pause_added_torrents_;
} }
[[nodiscard]] auto constexpr shouldDeleteSource() const noexcept [[nodiscard]] constexpr auto shouldDeleteSource() const noexcept
{ {
return should_pause_added_torrents_; return should_pause_added_torrents_;
} }
[[nodiscard]] auto constexpr allowsDHT() const noexcept [[nodiscard]] constexpr auto allowsDHT() const noexcept
{ {
return is_dht_enabled_; return is_dht_enabled_;
} }
[[nodiscard]] auto constexpr allowsLPD() const noexcept [[nodiscard]] constexpr auto allowsLPD() const noexcept
{ {
return is_lpd_enabled_; return is_lpd_enabled_;
} }
[[nodiscard]] auto constexpr allowsPEX() const noexcept [[nodiscard]] constexpr auto allowsPEX() const noexcept
{ {
return is_pex_enabled_; return is_pex_enabled_;
} }
[[nodiscard]] auto constexpr allowsTCP() const noexcept [[nodiscard]] constexpr auto allowsTCP() const noexcept
{ {
return is_tcp_enabled_; return is_tcp_enabled_;
} }
[[nodiscard]] bool allowsUTP() const noexcept; [[nodiscard]] bool allowsUTP() const noexcept;
[[nodiscard]] auto constexpr allowsPrefetch() const noexcept [[nodiscard]] constexpr auto allowsPrefetch() const noexcept
{ {
return is_prefetch_enabled_; return is_prefetch_enabled_;
} }
[[nodiscard]] auto constexpr isIdleLimited() const noexcept [[nodiscard]] constexpr auto isIdleLimited() const noexcept
{ {
return is_idle_limited_; return is_idle_limited_;
} }
[[nodiscard]] auto constexpr idleLimitMinutes() const noexcept [[nodiscard]] constexpr auto idleLimitMinutes() const noexcept
{ {
return idle_limit_minutes_; return idle_limit_minutes_;
} }
@ -673,7 +673,7 @@ public:
[[nodiscard]] std::optional<unsigned int> activeSpeedLimitBps(tr_direction dir) const noexcept; [[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_; return is_incomplete_file_naming_enabled_;
} }
@ -683,7 +683,7 @@ public:
return is_port_random_; return is_port_random_;
} }
[[nodiscard]] auto constexpr isRatioLimited() const noexcept [[nodiscard]] constexpr auto isRatioLimited() const noexcept
{ {
return is_ratio_limited_; return is_ratio_limited_;
} }

View File

@ -42,19 +42,19 @@ public:
return add(current(), old_); return add(current(), old_);
} }
void addUploaded(uint32_t n_bytes) noexcept constexpr void addUploaded(uint32_t n_bytes) noexcept
{ {
single_.uploadedBytes += n_bytes; single_.uploadedBytes += n_bytes;
is_dirty_ = true; is_dirty_ = true;
} }
void addDownloaded(uint32_t n_bytes) noexcept constexpr void addDownloaded(uint32_t n_bytes) noexcept
{ {
single_.downloadedBytes += n_bytes; single_.downloadedBytes += n_bytes;
is_dirty_ = true; is_dirty_ = true;
} }
void addFileCreated() noexcept constexpr void addFileCreated() noexcept
{ {
++single_.filesAdded; ++single_.filesAdded;
is_dirty_ = true; is_dirty_ = true;

View File

@ -87,7 +87,7 @@ public:
{ {
return blockInfo().blockSize(block); 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); return blockInfo().blockSpanForPiece(piece);
} }

View File

@ -2732,12 +2732,6 @@ void tr_torrent::markChanged()
this->anyDate = tr_time(); 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) void tr_torrent::setBlocks(tr_bitfield blocks)
{ {
this->completion.setBlocks(std::move(blocks)); this->completion.setBlocks(std::move(blocks));

View File

@ -154,7 +154,7 @@ public:
{ {
return metainfo_.blockSize(block); 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); return metainfo_.blockSpanForPiece(piece);
} }
@ -415,32 +415,32 @@ public:
metainfo_.setName(name); metainfo_.setName(name);
} }
[[nodiscard]] auto const& name() const noexcept [[nodiscard]] constexpr auto const& name() const noexcept
{ {
return metainfo_.name(); return metainfo_.name();
} }
[[nodiscard]] auto const& infoHash() const noexcept [[nodiscard]] constexpr auto const& infoHash() const noexcept
{ {
return metainfo_.infoHash(); return metainfo_.infoHash();
} }
[[nodiscard]] auto isPrivate() const noexcept [[nodiscard]] constexpr auto isPrivate() const noexcept
{ {
return metainfo_.isPrivate(); return metainfo_.isPrivate();
} }
[[nodiscard]] auto isPublic() const noexcept [[nodiscard]] constexpr auto isPublic() const noexcept
{ {
return !this->isPrivate(); return !this->isPrivate();
} }
[[nodiscard]] auto const& infoHashString() const noexcept [[nodiscard]] constexpr auto const& infoHashString() const noexcept
{ {
return metainfo_.infoHashString(); return metainfo_.infoHashString();
} }
[[nodiscard]] auto dateCreated() const noexcept [[nodiscard]] constexpr auto dateCreated() const noexcept
{ {
return metainfo_.dateCreated(); return metainfo_.dateCreated();
} }
@ -465,27 +465,27 @@ public:
return metainfo_.magnet(); return metainfo_.magnet();
} }
[[nodiscard]] auto const& comment() const noexcept [[nodiscard]] constexpr auto const& comment() const noexcept
{ {
return metainfo_.comment(); return metainfo_.comment();
} }
[[nodiscard]] auto const& creator() const noexcept [[nodiscard]] constexpr auto const& creator() const noexcept
{ {
return metainfo_.creator(); return metainfo_.creator();
} }
[[nodiscard]] auto const& source() const noexcept [[nodiscard]] constexpr auto const& source() const noexcept
{ {
return metainfo_.source(); return metainfo_.source();
} }
[[nodiscard]] auto infoDictSize() const noexcept [[nodiscard]] constexpr auto infoDictSize() const noexcept
{ {
return metainfo_.infoDictSize(); return metainfo_.infoDictSize();
} }
[[nodiscard]] auto infoDictOffset() const noexcept [[nodiscard]] constexpr auto infoDictOffset() const noexcept
{ {
return metainfo_.infoDictOffset(); return metainfo_.infoDictOffset();
} }
@ -515,17 +515,17 @@ public:
return this->isDone() ? TR_UP : TR_DOWN; return this->isDone() ? TR_UP : TR_DOWN;
} }
[[nodiscard]] auto allowsPex() const noexcept [[nodiscard]] constexpr auto allowsPex() const noexcept
{ {
return this->isPublic() && this->session->allowsPEX(); return this->isPublic() && this->session->allowsPEX();
} }
[[nodiscard]] auto allowsDht() const [[nodiscard]] constexpr auto allowsDht() const noexcept
{ {
return this->isPublic() && this->session->allowsDHT(); 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(); return this->isPublic() && this->session->allowsLPD();
} }
@ -586,7 +586,15 @@ public:
return unique_id_; 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); void setLabels(std::vector<tr_quark> const& new_labels);
@ -594,7 +602,7 @@ public:
torrent's content than any other mime-type. */ torrent's content than any other mime-type. */
[[nodiscard]] std::string_view primaryMimeType() const; [[nodiscard]] std::string_view primaryMimeType() const;
void setDirty() constexpr void setDirty() noexcept
{ {
this->isDirty = true; this->isDirty = true;
} }