refactor: add TR_CONSTEXPR20 to make future C++20 migration easier (#4457)

This commit is contained in:
Charles Kerr
2022-12-23 15:21:40 -06:00
committed by GitHub
parent 559b759ea0
commit 1402cb2949
18 changed files with 80 additions and 68 deletions

View File

@@ -61,34 +61,34 @@ private:
using trackers_t = std::vector<tracker_info>; using trackers_t = std::vector<tracker_info>;
public: public:
[[nodiscard]] auto begin() const noexcept [[nodiscard]] TR_CONSTEXPR20 auto begin() const noexcept
{ {
return std::begin(trackers_); return std::begin(trackers_);
} }
[[nodiscard]] auto end() const noexcept [[nodiscard]] TR_CONSTEXPR20 auto end() const noexcept
{ {
return std::end(trackers_); return std::end(trackers_);
} }
[[nodiscard]] bool empty() const noexcept [[nodiscard]] TR_CONSTEXPR20 bool empty() const noexcept
{ {
return std::empty(trackers_); return std::empty(trackers_);
} }
[[nodiscard]] size_t size() const noexcept [[nodiscard]] TR_CONSTEXPR20 size_t size() const noexcept
{ {
return std::size(trackers_); return std::size(trackers_);
} }
[[nodiscard]] tracker_info const& at(size_t i) const [[nodiscard]] TR_CONSTEXPR20 tracker_info const& at(size_t i) const
{ {
return trackers_.at(i); return trackers_.at(i);
} }
[[nodiscard]] tr_tracker_tier_t nextTier() const; [[nodiscard]] tr_tracker_tier_t nextTier() const;
[[nodiscard]] bool operator==(tr_announce_list const& that) const [[nodiscard]] TR_CONSTEXPR20 bool operator==(tr_announce_list const& that) const
{ {
return trackers_ == that.trackers_; return trackers_ == that.trackers_;
} }

View File

@@ -13,6 +13,8 @@
#include <cstdint> #include <cstdint>
#include <vector> #include <vector>
#include "tr-macros.h"
/** /**
* @brief Implementation of the BitTorrent spec's Bitfield array of bits. * @brief Implementation of the BitTorrent spec's Bitfield array of bits.
* *
@@ -117,7 +119,7 @@ private:
[[nodiscard]] size_t countFlags() const noexcept; [[nodiscard]] size_t countFlags() const noexcept;
[[nodiscard]] size_t countFlags(size_t begin, size_t end) const noexcept; [[nodiscard]] size_t countFlags(size_t begin, size_t end) const noexcept;
[[nodiscard]] bool testFlag(size_t n) const [[nodiscard]] TR_CONSTEXPR20 bool testFlag(size_t n) const
{ {
if (n >> 3U >= std::size(flags_)) if (n >> 3U >= std::size(flags_))
{ {

View File

@@ -57,7 +57,7 @@ public:
[[nodiscard]] file_offset_t fileOffset(uint64_t offset) const; [[nodiscard]] file_offset_t fileOffset(uint64_t offset) const;
[[nodiscard]] size_t size() const [[nodiscard]] TR_CONSTEXPR20 size_t size() const
{ {
return std::size(file_pieces_); return std::size(file_pieces_);
} }

View File

@@ -37,14 +37,14 @@ public:
return name_; return name_;
} }
[[nodiscard]] constexpr auto webseedCount() const noexcept [[nodiscard]] TR_CONSTEXPR20 auto webseedCount() const noexcept
{ {
return std::size(webseed_urls_); return std::size(webseed_urls_);
} }
[[nodiscard]] auto const& webseed(size_t i) const [[nodiscard]] TR_CONSTEXPR20 auto const& webseed(size_t i) const
{ {
return webseed_urls_[i]; return webseed_urls_.at(i);
} }
[[nodiscard]] constexpr auto& announceList() noexcept [[nodiscard]] constexpr auto& announceList() noexcept

View File

@@ -123,12 +123,12 @@ public:
return comment_; return comment_;
} }
[[nodiscard]] auto fileCount() const noexcept [[nodiscard]] TR_CONSTEXPR20 auto fileCount() const noexcept
{ {
return files_.fileCount(); return files_.fileCount();
} }
[[nodiscard]] auto fileSize(tr_file_index_t i) const noexcept [[nodiscard]] TR_CONSTEXPR20 auto fileSize(tr_file_index_t i) const noexcept
{ {
return files_.fileSize(i); return files_.fileSize(i);
} }

View File

@@ -95,7 +95,7 @@ public:
/// ///
[[nodiscard]] auto read_buffer_size() const noexcept [[nodiscard]] TR_CONSTEXPR20 auto read_buffer_size() const noexcept
{ {
return std::size(inbuf_); return std::size(inbuf_);
} }

View File

@@ -413,7 +413,7 @@ public:
is_endgame_ = uint64_t(std::size(active_requests)) * tr_block_info::BlockSize >= tor->leftUntilDone(); is_endgame_ = uint64_t(std::size(active_requests)) * tr_block_info::BlockSize >= tor->leftUntilDone();
} }
[[nodiscard]] auto constexpr isEndgame() const noexcept [[nodiscard]] constexpr auto isEndgame() const noexcept
{ {
return is_endgame_; return is_endgame_;
} }
@@ -447,7 +447,7 @@ public:
stats.active_webseed_count = 0; stats.active_webseed_count = 0;
} }
[[nodiscard]] auto isAllSeeds() const noexcept [[nodiscard]] TR_CONSTEXPR20 auto isAllSeeds() const noexcept
{ {
if (!pool_is_all_seeds_) if (!pool_is_all_seeds_)
{ {

View File

@@ -878,7 +878,7 @@ public:
web_->fetch(std::move(options)); web_->fetch(std::move(options));
} }
[[nodiscard]] auto const& bandwidthGroups() const noexcept [[nodiscard]] constexpr auto const& bandwidthGroups() const noexcept
{ {
return bandwidth_groups_; return bandwidth_groups_;
} }

View File

@@ -29,17 +29,17 @@ struct tr_error;
struct tr_torrent_files struct tr_torrent_files
{ {
public: public:
[[nodiscard]] bool empty() const noexcept [[nodiscard]] TR_CONSTEXPR20 bool empty() const noexcept
{ {
return std::empty(files_); return std::empty(files_);
} }
[[nodiscard]] size_t fileCount() const noexcept [[nodiscard]] TR_CONSTEXPR20 size_t fileCount() const noexcept
{ {
return std::size(files_); return std::size(files_);
} }
[[nodiscard]] uint64_t fileSize(tr_file_index_t file_index) const [[nodiscard]] TR_CONSTEXPR20 uint64_t fileSize(tr_file_index_t file_index) const
{ {
return files_.at(file_index).size_; return files_.at(file_index).size_;
} }
@@ -49,7 +49,7 @@ public:
return total_size_; return total_size_;
} }
[[nodiscard]] std::string const& path(tr_file_index_t file_index) const [[nodiscard]] TR_CONSTEXPR20 std::string const& path(tr_file_index_t file_index) const
{ {
return files_.at(file_index).path_; return files_.at(file_index).path_;
} }

View File

@@ -23,7 +23,7 @@ struct tr_error;
struct tr_torrent_metainfo : public tr_magnet_metainfo struct tr_torrent_metainfo : public tr_magnet_metainfo
{ {
public: public:
[[nodiscard]] constexpr auto empty() const noexcept [[nodiscard]] TR_CONSTEXPR20 auto empty() const noexcept
{ {
return std::empty(files_); return std::empty(files_);
} }
@@ -42,15 +42,15 @@ public:
{ {
return files_; return files_;
} }
[[nodiscard]] auto fileCount() const noexcept [[nodiscard]] TR_CONSTEXPR20 auto fileCount() const noexcept
{ {
return files().fileCount(); return files().fileCount();
} }
[[nodiscard]] auto fileSize(tr_file_index_t i) const [[nodiscard]] TR_CONSTEXPR20 auto fileSize(tr_file_index_t i) const
{ {
return files().fileSize(i); return files().fileSize(i);
} }
[[nodiscard]] auto const& fileSubpath(tr_file_index_t i) const [[nodiscard]] TR_CONSTEXPR20 auto const& fileSubpath(tr_file_index_t i) const
{ {
return files().path(i); return files().path(i);
} }
@@ -130,7 +130,7 @@ public:
[[nodiscard]] tr_sha1_digest_t const& pieceHash(tr_piece_index_t piece) const; [[nodiscard]] tr_sha1_digest_t const& pieceHash(tr_piece_index_t piece) const;
[[nodiscard]] bool hasV1Metadata() const noexcept [[nodiscard]] TR_CONSTEXPR20 bool hasV1Metadata() const noexcept
{ {
// need 'pieces' field and 'files' or 'length' // need 'pieces' field and 'files' or 'length'
// TODO check for 'files' or 'length' // TODO check for 'files' or 'length'

View File

@@ -386,17 +386,17 @@ public:
/// METAINFO - FILES /// METAINFO - FILES
[[nodiscard]] tr_file_index_t fileCount() const noexcept [[nodiscard]] TR_CONSTEXPR20 auto fileCount() const noexcept
{ {
return metainfo_.fileCount(); return metainfo_.fileCount();
} }
[[nodiscard]] std::string const& fileSubpath(tr_file_index_t i) const [[nodiscard]] TR_CONSTEXPR20 auto const& fileSubpath(tr_file_index_t i) const
{ {
return metainfo_.fileSubpath(i); return metainfo_.fileSubpath(i);
} }
[[nodiscard]] auto fileSize(tr_file_index_t i) const [[nodiscard]] TR_CONSTEXPR20 auto fileSize(tr_file_index_t i) const
{ {
return metainfo_.fileSize(i); return metainfo_.fileSize(i);
} }
@@ -412,22 +412,22 @@ public:
/// METAINFO - TRACKERS /// METAINFO - TRACKERS
[[nodiscard]] auto const& announceList() const noexcept [[nodiscard]] constexpr auto const& announceList() const noexcept
{ {
return metainfo_.announceList(); return metainfo_.announceList();
} }
[[nodiscard]] auto& announceList() noexcept [[nodiscard]] constexpr auto& announceList() noexcept
{ {
return metainfo_.announceList(); return metainfo_.announceList();
} }
[[nodiscard]] auto trackerCount() const noexcept [[nodiscard]] TR_CONSTEXPR20 auto trackerCount() const noexcept
{ {
return std::size(this->announceList()); return std::size(this->announceList());
} }
[[nodiscard]] auto const& tracker(size_t i) const [[nodiscard]] TR_CONSTEXPR20 auto const& tracker(size_t i) const
{ {
return this->announceList().at(i); return this->announceList().at(i);
} }
@@ -441,12 +441,12 @@ public:
/// METAINFO - WEBSEEDS /// METAINFO - WEBSEEDS
[[nodiscard]] auto webseedCount() const noexcept [[nodiscard]] TR_CONSTEXPR20 auto webseedCount() const noexcept
{ {
return metainfo_.webseedCount(); return metainfo_.webseedCount();
} }
[[nodiscard]] auto const& webseed(size_t i) const [[nodiscard]] TR_CONSTEXPR20 auto const& webseed(size_t i) const
{ {
return metainfo_.webseed(i); return metainfo_.webseed(i);
} }

View File

@@ -42,22 +42,6 @@ struct CompareTorrentByHash
} // namespace } // namespace
tr_torrent* tr_torrents::get(tr_torrent_id_t id)
{
TR_ASSERT(static_cast<size_t>(id) < std::size(by_id_));
if (static_cast<size_t>(id) >= std::size(by_id_))
{
return nullptr;
}
auto* const tor = by_id_.at(id);
TR_ASSERT(tor == nullptr || tor->id() == id);
TR_ASSERT(
std::count_if(std::begin(removed_), std::end(removed_), [&id](auto const& removed) { return id == removed.first; }) ==
(tor == nullptr ? 1 : 0));
return tor;
}
tr_torrent* tr_torrents::get(std::string_view magnet_link) tr_torrent* tr_torrents::get(std::string_view magnet_link)
{ {
auto magnet = tr_magnet_metainfo{}; auto magnet = tr_magnet_metainfo{};

View File

@@ -31,7 +31,11 @@ public:
void remove(tr_torrent const* tor, time_t current_time); void remove(tr_torrent const* tor, time_t current_time);
// O(1) // O(1)
[[nodiscard]] tr_torrent* get(tr_torrent_id_t id); [[nodiscard]] TR_CONSTEXPR20 tr_torrent* get(tr_torrent_id_t id)
{
auto const uid = static_cast<size_t>(id);
return uid >= std::size(by_id_) ? nullptr : by_id_.at(uid);
}
// O(log n) // O(log n)
[[nodiscard]] tr_torrent const* get(tr_sha1_digest_t const& hash) const; [[nodiscard]] tr_torrent const* get(tr_sha1_digest_t const& hash) const;
@@ -60,40 +64,40 @@ public:
[[nodiscard]] std::vector<tr_torrent_id_t> removedSince(time_t) const; [[nodiscard]] std::vector<tr_torrent_id_t> removedSince(time_t) const;
[[nodiscard]] auto cbegin() const noexcept [[nodiscard]] TR_CONSTEXPR20 auto cbegin() const noexcept
{ {
return std::cbegin(by_hash_); return std::cbegin(by_hash_);
} }
[[nodiscard]] auto begin() const noexcept [[nodiscard]] TR_CONSTEXPR20 auto begin() const noexcept
{ {
return cbegin(); return cbegin();
} }
[[nodiscard]] auto begin() noexcept [[nodiscard]] TR_CONSTEXPR20 auto begin() noexcept
{ {
return std::begin(by_hash_); return std::begin(by_hash_);
} }
[[nodiscard]] auto cend() const noexcept [[nodiscard]] TR_CONSTEXPR20 auto cend() const noexcept
{ {
return std::cend(by_hash_); return std::cend(by_hash_);
} }
[[nodiscard]] auto end() const noexcept [[nodiscard]] TR_CONSTEXPR20 auto end() const noexcept
{ {
return cend(); return cend();
} }
[[nodiscard]] auto end() noexcept [[nodiscard]] TR_CONSTEXPR20 auto end() noexcept
{ {
return std::end(by_hash_); return std::end(by_hash_);
} }
[[nodiscard]] constexpr auto size() const noexcept [[nodiscard]] TR_CONSTEXPR20 auto size() const noexcept
{ {
return std::size(by_hash_); return std::size(by_hash_);
} }
[[nodiscard]] constexpr auto empty() const noexcept [[nodiscard]] TR_CONSTEXPR20 auto empty() const noexcept
{ {
return std::empty(by_hash_); return std::empty(by_hash_);
} }

View File

@@ -189,7 +189,7 @@ public:
} }
template<typename T> template<typename T>
[[nodiscard]] bool startsWith(T const& needle) const [[nodiscard]] TR_CONSTEXPR20 bool startsWith(T const& needle) const
{ {
auto const n_bytes = std::size(needle); auto const n_bytes = std::size(needle);
auto const needle_begin = reinterpret_cast<std::byte const*>(std::data(needle)); auto const needle_begin = reinterpret_cast<std::byte const*>(std::data(needle));

View File

@@ -8,6 +8,27 @@
#include <array> #include <array>
#include <cstddef> // size_t #include <cstddef> // size_t
///
#ifdef _MSVC_LANG
#define TR_CPLUSPLUS _MSVC_LANG
#else
#define TR_CPLUSPLUS __cplusplus
#endif
#if ((TR_CPLUSPLUS >= 202002L) && (!defined(_GLIBCXX_RELEASE) || _GLIBCXX_RELEASE > 9)) || \
(TR_CPLUSPLUS >= 201709L && TR_GCC_VERSION >= 1002)
#define TR_CONSTEXPR20 constexpr
#else
#define TR_CONSTEXPR20
#endif
// Placeholder for future use.
// Can't implement right now because __cplusplus version for C++23 is currently TBD
#define TR_CONSTEXPR23
///
/*** /***
**** ****
***/ ***/

View File

@@ -11,6 +11,7 @@
#define TR_POPCNT_H #define TR_POPCNT_H
#include <cstdint> #include <cstdint>
#include <type_traits>
/* Avoid defining irrelevant helpers that might interfere with other /* Avoid defining irrelevant helpers that might interfere with other
* preprocessor logic. */ * preprocessor logic. */
@@ -58,7 +59,7 @@ struct tr_popcnt
#if defined(TR_HAVE_STD_POPCOUNT) #if defined(TR_HAVE_STD_POPCOUNT)
/* If we have std::popcount just use that. */ /* If we have std::popcount just use that. */
static inline unsigned count(T v) static constexpr inline unsigned count(T v)
{ {
unsigned_T unsigned_v = static_cast<unsigned_T>(v); unsigned_T unsigned_v = static_cast<unsigned_T>(v);
return static_cast<unsigned>(std::popcount(unsigned_v)); return static_cast<unsigned>(std::popcount(unsigned_v));

View File

@@ -43,17 +43,17 @@ public:
return children_.at(row); return children_.at(row);
} }
[[nodiscard]] int childCount() const noexcept [[nodiscard]] TR_CONSTEXPR20 int childCount() const noexcept
{ {
return std::size(children_); return std::size(children_);
} }
[[nodiscard]] auto* parent() noexcept [[nodiscard]] constexpr auto* parent() noexcept
{ {
return parent_; return parent_;
} }
[[nodiscard]] auto const* parent() const noexcept [[nodiscard]] constexpr auto const* parent() const noexcept
{ {
return parent_; return parent_;
} }

View File

@@ -135,14 +135,14 @@ public:
} }
} }
[[nodiscard]] auto operator==(TorrentHash const& that) const [[nodiscard]] TR_CONSTEXPR20 auto operator==(TorrentHash const& that) const
{ {
return data_ == that.data_; return data_ == that.data_;
} }
[[nodiscard]] auto operator!=(TorrentHash const& that) const [[nodiscard]] TR_CONSTEXPR20 auto operator!=(TorrentHash const& that) const
{ {
return data_ != that.data_; return !(*this == that);
} }
[[nodiscard]] auto operator<(TorrentHash const& that) const [[nodiscard]] auto operator<(TorrentHash const& that) const