refactor: remove TR_N_ELEMENTS macro (#3674)

* refactor: use std::array for keys in transmission-remote

* refactor: use std::array for wide_module_path in platform.cc

* refactor: use std::array for value in tr_env_get_int()

* refactor: use std::array for SysStoreNames in web.cc

* chore: remove unused macro TR_N_ELEMENTS
This commit is contained in:
Charles Kerr 2022-08-18 21:31:53 -05:00 committed by GitHub
parent c4537e6d80
commit c532728c42
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 60 additions and 57 deletions

View File

@ -278,9 +278,9 @@ std::string tr_getWebClientDir([[maybe_unused]] tr_session const* session)
}
/* check calling module place */
wchar_t wide_module_path[MAX_PATH];
GetModuleFileNameW(nullptr, wide_module_path, TR_N_ELEMENTS(wide_module_path));
auto const module_path = tr_win32_native_to_utf8(wide_module_path);
auto wide_module_path = std::array<wchar_t, MAX_PATH>{};
GetModuleFileNameW(nullptr, std::data(wide_module_path), std::size(wide_module_path));
auto const module_path = tr_win32_native_to_utf8({ std::data(wide_module_path) });
if (auto const dir = tr_sys_path_dirname(module_path); !std::empty(dir))
{
if (auto const path = tr_pathbuf{ dir, "/Web"sv }; isWebClientDir(path))

View File

@ -978,16 +978,16 @@ int tr_env_get_int(char const* key, int default_value)
#ifdef _WIN32
char value[16];
auto value = std::array<char, 16>{};
if (GetEnvironmentVariableA(key, value, TR_N_ELEMENTS(value)) > 1)
if (GetEnvironmentVariableA(key, std::data(value), std::size(value)) > 1)
{
return atoi(value);
return atoi(std::data(value));
}
#else
if (char const* value = getenv(key); !tr_str_is_empty(value))
if (char const* const value = getenv(key); !tr_str_is_empty(value))
{
return atoi(value);
}

View File

@ -56,8 +56,6 @@ struct tr_error;
*****
****/
#define TR_N_ELEMENTS(ary) (sizeof(ary) / sizeof(*(ary)))
[[nodiscard]] std::string_view tr_get_mime_type_for_filename(std::string_view filename);
/**
@ -80,10 +78,10 @@ constexpr auto tr_saveFile(std::string_view filename, ContiguousRange const& x,
* @brief Get disk capacity and free disk space (in bytes) for the specified folder.
* @return struct with free and total as zero or positive integer on success, -1 in case of error.
*/
tr_disk_space tr_dirSpace(std::string_view directory);
[[nodiscard]] tr_disk_space tr_dirSpace(std::string_view directory);
/** @brief return the current date in milliseconds */
uint64_t tr_time_msec();
[[nodiscard]] uint64_t tr_time_msec();
/** @brief sleep the specified number of milliseconds */
void tr_wait_msec(long int delay_milliseconds);
@ -96,9 +94,9 @@ template<typename T, std::enable_if_t<std::is_floating_point<T>::value, bool> =
#ifdef _WIN32
std::string tr_win32_format_message(uint32_t code);
std::string tr_win32_native_to_utf8(std::wstring_view);
std::wstring tr_win32_utf8_to_native(std::string_view);
[[nodiscard]] std::string tr_win32_format_message(uint32_t code);
[[nodiscard]] std::string tr_win32_native_to_utf8(std::wstring_view);
[[nodiscard]] std::wstring tr_win32_utf8_to_native(std::string_view);
int tr_main_win32(int argc, char** argv, int (*real_main)(int, char**));
@ -120,7 +118,7 @@ int tr_main_win32(int argc, char** argv, int (*real_main)(int, char**));
****
***/
constexpr bool tr_str_is_empty(char const* value)
[[nodiscard]] constexpr bool tr_str_is_empty(char const* value)
{
return value == nullptr || *value == '\0';
}
@ -295,10 +293,10 @@ constexpr void tr_timeUpdate(time_t now) noexcept
}
/** @brief Portability wrapper for htonll() that uses the system implementation if available */
uint64_t tr_htonll(uint64_t);
[[nodiscard]] uint64_t tr_htonll(uint64_t);
/** @brief Portability wrapper for htonll() that uses the system implementation if available */
uint64_t tr_ntohll(uint64_t);
[[nodiscard]] uint64_t tr_ntohll(uint64_t);
/***
****
@ -317,40 +315,40 @@ extern size_t tr_mem_K;
extern uint64_t tr_size_K; /* unused? */
/* format a speed from KBps into a user-readable string. */
std::string tr_formatter_speed_KBps(double KBps);
[[nodiscard]] std::string tr_formatter_speed_KBps(double KBps);
/* format a memory size from bytes into a user-readable string. */
std::string tr_formatter_mem_B(size_t bytes);
[[nodiscard]] std::string tr_formatter_mem_B(size_t bytes);
/* format a memory size from MB into a user-readable string. */
static inline std::string tr_formatter_mem_MB(double MBps)
[[nodiscard]] static inline std::string tr_formatter_mem_MB(double MBps)
{
return tr_formatter_mem_B((size_t)(MBps * tr_mem_K * tr_mem_K));
}
/* format a file size from bytes into a user-readable string. */
std::string tr_formatter_size_B(uint64_t bytes);
[[nodiscard]] std::string tr_formatter_size_B(uint64_t bytes);
void tr_formatter_get_units(void* dict);
static inline unsigned int tr_toSpeedBytes(unsigned int KBps)
[[nodiscard]] static inline unsigned int tr_toSpeedBytes(unsigned int KBps)
{
return KBps * tr_speed_K;
}
static inline auto tr_toSpeedKBps(unsigned int Bps)
[[nodiscard]] static inline auto tr_toSpeedKBps(unsigned int Bps)
{
return Bps / double(tr_speed_K);
}
static inline auto tr_toMemBytes(unsigned int MB)
[[nodiscard]] static inline auto tr_toMemBytes(unsigned int MB)
{
auto B = uint64_t(tr_mem_K) * tr_mem_K;
B *= MB;
return B;
}
static inline auto tr_toMemMB(uint64_t B)
[[nodiscard]] static inline auto tr_toMemMB(uint64_t B)
{
return int(B / (tr_mem_K * tr_mem_K));
}
@ -360,13 +358,13 @@ static inline auto tr_toMemMB(uint64_t B)
***/
/** @brief Check if environment variable exists. */
bool tr_env_key_exists(char const* key);
[[nodiscard]] bool tr_env_key_exists(char const* key);
/** @brief Get environment variable value as int. */
int tr_env_get_int(char const* key, int default_value);
[[nodiscard]] int tr_env_get_int(char const* key, int default_value);
/** @brief Get environment variable value as string. */
std::string tr_env_get_string(std::string_view key, std::string_view default_value = {});
[[nodiscard]] std::string tr_env_get_string(std::string_view key, std::string_view default_value = {});
/***
****

View File

@ -4,6 +4,7 @@
// License text can be found in the licenses/ folder.
#include <algorithm>
#include <array>
#include <condition_variable>
#include <list>
#include <map>
@ -57,14 +58,14 @@ static CURLcode ssl_context_func(CURL* /*curl*/, void* ssl_ctx, void* /*user_dat
return CURLE_OK;
}
static LPCWSTR const sys_store_names[] = {
static auto constexpr SysStoreNames = std::array<LPCWSTR, 2>{
L"CA",
L"ROOT",
};
for (size_t i = 0; i < TR_N_ELEMENTS(sys_store_names); ++i)
for (auto& sys_store_name : SysStoreNames)
{
HCERTSTORE const sys_cert_store = CertOpenSystemStoreW(0, sys_store_names[i]);
HCERTSTORE const sys_cert_store = CertOpenSystemStoreW(0, sys_store_name);
if (sys_cert_store == nullptr)
{
continue;

View File

@ -712,14 +712,16 @@ static void addFiles(tr_variant* args, tr_quark const key, char const* arg)
}
}
static tr_quark const files_keys[] = {
// clang-format off
static auto constexpr FilesKeys = std::array<tr_quark, 4>{
TR_KEY_files,
TR_KEY_name,
TR_KEY_priorities,
TR_KEY_wanted,
};
static tr_quark const details_keys[] = {
static auto constexpr DetailsKeys = std::array<tr_quark, 52>{
TR_KEY_activityDate,
TR_KEY_addedDate,
TR_KEY_bandwidthPriority,
@ -771,26 +773,28 @@ static tr_quark const details_keys[] = {
TR_KEY_uploadLimited,
TR_KEY_uploadRatio,
TR_KEY_webseeds,
TR_KEY_webseedsSendingToUs,
TR_KEY_webseedsSendingToUs
};
static tr_quark const list_keys[] = {
TR_KEY_error, //
TR_KEY_errorString, //
TR_KEY_eta, //
TR_KEY_id, //
TR_KEY_isFinished, //
TR_KEY_leftUntilDone, //
TR_KEY_name, //
TR_KEY_peersGettingFromUs, //
TR_KEY_peersSendingToUs, //
TR_KEY_rateDownload, //
TR_KEY_rateUpload, //
TR_KEY_sizeWhenDone, //
TR_KEY_status, //
TR_KEY_uploadRatio, //
static auto constexpr ListKeys = std::array<tr_quark, 14>{
TR_KEY_error,
TR_KEY_errorString,
TR_KEY_eta,
TR_KEY_id,
TR_KEY_isFinished,
TR_KEY_leftUntilDone,
TR_KEY_name,
TR_KEY_peersGettingFromUs,
TR_KEY_peersSendingToUs,
TR_KEY_rateDownload,
TR_KEY_rateUpload,
TR_KEY_sizeWhenDone,
TR_KEY_status,
TR_KEY_uploadRatio
};
// clang-format on
static size_t writeFunc(void* ptr, size_t size, size_t nmemb, void* vbuf)
{
auto* const buf = static_cast<evbuffer*>(vbuf);
@ -2557,9 +2561,9 @@ static int processArgs(char const* rpcurl, int argc, char const* const* argv, Co
config.filter = optarg;
tr_variantDictAddInt(&top, TR_KEY_tag, TAG_FILTER);
for (size_t i = 0; i < TR_N_ELEMENTS(details_keys); ++i)
for (auto const& key : DetailsKeys)
{
tr_variantListAddQuark(fields, details_keys[i]);
tr_variantListAddQuark(fields, key);
}
addIdArg(args, config, "all");
@ -2567,9 +2571,9 @@ static int processArgs(char const* rpcurl, int argc, char const* const* argv, Co
case 'i':
tr_variantDictAddInt(&top, TR_KEY_tag, TAG_DETAILS);
for (size_t i = 0; i < TR_N_ELEMENTS(details_keys); ++i)
for (auto const& key : DetailsKeys)
{
tr_variantListAddQuark(fields, details_keys[i]);
tr_variantListAddQuark(fields, key);
}
addIdArg(args, config);
@ -2578,9 +2582,9 @@ static int processArgs(char const* rpcurl, int argc, char const* const* argv, Co
case 'l':
tr_variantDictAddInt(&top, TR_KEY_tag, TAG_LIST);
for (size_t i = 0; i < TR_N_ELEMENTS(list_keys); ++i)
for (auto const& key : ListKeys)
{
tr_variantListAddQuark(fields, list_keys[i]);
tr_variantListAddQuark(fields, key);
}
addIdArg(args, config, "all");
@ -2589,9 +2593,9 @@ static int processArgs(char const* rpcurl, int argc, char const* const* argv, Co
case 940:
tr_variantDictAddInt(&top, TR_KEY_tag, TAG_FILES);
for (size_t i = 0; i < TR_N_ELEMENTS(files_keys); ++i)
for (auto const& key : FilesKeys)
{
tr_variantListAddQuark(fields, files_keys[i]);
tr_variantListAddQuark(fields, key);
}
addIdArg(args, config);