From 841c4899bb87c729f02b8463ec972994f3f7f1c9 Mon Sep 17 00:00:00 2001 From: Stephane Janel Date: Thu, 9 Nov 2023 21:12:20 +0100 Subject: [PATCH] Modernize array with std::array without template parameter arguments when applicable --- src/api/exchanges/src/binancepublicapi.cpp | 7 ++++--- src/api/exchanges/src/huobipublicapi.cpp | 5 +++-- src/api/exchanges/src/kucoinpublicapi.cpp | 8 ++++---- src/api/exchanges/src/upbitpublicapi.cpp | 4 ++-- src/engine/include/commandlineoptionsparser.hpp | 6 +++--- src/engine/src/metricsexporter.cpp | 2 +- src/engine/test/commandlineoptionsparser_test.cpp | 9 ++++----- 7 files changed, 21 insertions(+), 20 deletions(-) diff --git a/src/api/exchanges/src/binancepublicapi.cpp b/src/api/exchanges/src/binancepublicapi.cpp index 96b33bbf..d075129e 100644 --- a/src/api/exchanges/src/binancepublicapi.cpp +++ b/src/api/exchanges/src/binancepublicapi.cpp @@ -1,6 +1,7 @@ #include "binancepublicapi.hpp" #include +#include #include #include #include @@ -464,10 +465,10 @@ MarketOrderBookMap BinancePublic::AllOrderBooksFunc::operator()(int depth) { MarketOrderBook BinancePublic::OrderBookFunc::operator()(Market mk, int depth) { // Binance has a fixed range of authorized values for depth - static constexpr int kAuthorizedDepths[] = {5, 10, 20, 50, 100, 500, 1000, 5000}; + static constexpr std::array kAuthorizedDepths = {5, 10, 20, 50, 100, 500, 1000, 5000}; auto lb = std::ranges::lower_bound(kAuthorizedDepths, depth); - if (lb == std::end(kAuthorizedDepths)) { - lb = std::next(std::end(kAuthorizedDepths), -1); + if (lb == kAuthorizedDepths.end()) { + lb = std::next(kAuthorizedDepths.end(), -1); log::error("Invalid depth {}, default to {}", depth, *lb); } CurlPostData postData{{"symbol", mk.assetsPairStrUpper()}, {"limit", *lb}}; diff --git a/src/api/exchanges/src/huobipublicapi.cpp b/src/api/exchanges/src/huobipublicapi.cpp index f2273097..70a521a2 100644 --- a/src/api/exchanges/src/huobipublicapi.cpp +++ b/src/api/exchanges/src/huobipublicapi.cpp @@ -1,6 +1,7 @@ #include "huobipublicapi.hpp" #include +#include #include #include #include @@ -345,9 +346,9 @@ MarketOrderBook HuobiPublic::OrderBookFunc::operator()(Market mk, int depth) { // Huobi has a fixed range of authorized values for depth CurlPostData postData{{"symbol", mk.assetsPairStrLower()}, {"type", "step0"}}; if (depth != kHuobiStandardOrderBookDefaultDepth) { - static constexpr int kAuthorizedDepths[] = {5, 10, 20, kHuobiStandardOrderBookDefaultDepth}; + static constexpr std::array kAuthorizedDepths = {5, 10, 20, kHuobiStandardOrderBookDefaultDepth}; auto lb = std::ranges::lower_bound(kAuthorizedDepths, depth); - if (lb == std::end(kAuthorizedDepths)) { + if (lb == kAuthorizedDepths.end()) { log::warn("Invalid depth {}, default to {}", depth, kHuobiStandardOrderBookDefaultDepth); } else if (*lb != kHuobiStandardOrderBookDefaultDepth) { postData.append("depth", *lb); diff --git a/src/api/exchanges/src/kucoinpublicapi.cpp b/src/api/exchanges/src/kucoinpublicapi.cpp index 811be539..edc45b86 100644 --- a/src/api/exchanges/src/kucoinpublicapi.cpp +++ b/src/api/exchanges/src/kucoinpublicapi.cpp @@ -1,10 +1,10 @@ #include "kucoinpublicapi.hpp" #include +#include #include #include #include -#include #include #include @@ -269,10 +269,10 @@ void FillOrderBook(Market mk, int depth, bool isAsk, InputIt beg, InputIt end, v MarketOrderBook KucoinPublic::OrderBookFunc::operator()(Market mk, int depth) { // Kucoin has a fixed range of authorized values for depth - static constexpr int kAuthorizedDepths[] = {20, 100}; + static constexpr std::array kAuthorizedDepths = {20, 100}; auto lb = std::ranges::lower_bound(kAuthorizedDepths, depth); - if (lb == std::end(kAuthorizedDepths)) { - lb = std::next(std::end(kAuthorizedDepths), -1); + if (lb == kAuthorizedDepths.end()) { + lb = std::next(kAuthorizedDepths.end(), -1); log::warn("Invalid depth {}, default to {}", depth, *lb); } diff --git a/src/api/exchanges/src/upbitpublicapi.cpp b/src/api/exchanges/src/upbitpublicapi.cpp index 3b34bf3d..5610d0bc 100644 --- a/src/api/exchanges/src/upbitpublicapi.cpp +++ b/src/api/exchanges/src/upbitpublicapi.cpp @@ -280,8 +280,8 @@ MonetaryAmount UpbitPublic::SanitizeVolume(MonetaryAmount vol, MonetaryAmount pr /// Value found in this page: /// https://cryptoexchangenews.net/2021/02/upbit-notes-information-on-changing-the-minimum-order-amount-at-krw-market-to-stabilize-the/ /// confirmed with some tests. However, it could change in the future. - static constexpr std::array kMinOrderAmounts{ - {MonetaryAmount(5000, "KRW"), MonetaryAmount(5, "BTC", 4)}}; // 5000 KRW or 0.0005 BTC is min + static constexpr std::array kMinOrderAmounts = {MonetaryAmount(5000, "KRW"), + MonetaryAmount(5, "BTC", 4)}; // 5000 KRW or 0.0005 BTC is min for (MonetaryAmount minOrderAmount : kMinOrderAmounts) { if (vol.currencyCode() == minOrderAmount.currencyCode()) { if (vol < minOrderAmount) { diff --git a/src/engine/include/commandlineoptionsparser.hpp b/src/engine/include/commandlineoptionsparser.hpp index ca9f3701..e3fa1398 100644 --- a/src/engine/include/commandlineoptionsparser.hpp +++ b/src/engine/include/commandlineoptionsparser.hpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -44,9 +45,8 @@ class CommandLineOptionsParser { append(init); } - template - CommandLineOptionsParser& append(const CommandLineOptionWithValue (&opts)[N]) { - const auto insertedIt = _opts.insert(_opts.end(), std::begin(opts), std::end(opts)); + CommandLineOptionsParser& append(std::ranges::input_range auto&& opts) { + const auto insertedIt = _opts.insert(_opts.end(), std::ranges::begin(opts), std::ranges::end(opts)); const auto sortByFirst = [](const auto& lhs, const auto& rhs) { return lhs.first < rhs.first; }; std::sort(insertedIt, _opts.end(), sortByFirst); std::inplace_merge(_opts.begin(), insertedIt, _opts.end(), sortByFirst); diff --git a/src/engine/src/metricsexporter.cpp b/src/engine/src/metricsexporter.cpp index 29440271..b83e762c 100644 --- a/src/engine/src/metricsexporter.cpp +++ b/src/engine/src/metricsexporter.cpp @@ -152,7 +152,7 @@ void MetricsExporter::exportLastTradesMetrics(Market mk, const LastTradesPerExch void MetricsExporter::createSummariesAndHistograms() { for (const auto &[k, v] : CurlMetrics::kRequestDurationKeys) { - static constexpr double kRequestDurationBoundariesMs[] = {5, 10, 20, 50, 100, 200, 500, 1000}; + static constexpr std::array kRequestDurationBoundariesMs = {5.0, 10.0, 20.0, 50.0, 100.0, 200.0, 500.0, 1000.0}; _pMetricsGateway->createHistogram(v, kRequestDurationBoundariesMs); } } diff --git a/src/engine/test/commandlineoptionsparser_test.cpp b/src/engine/test/commandlineoptionsparser_test.cpp index 5a52e23e..7b02053a 100644 --- a/src/engine/test/commandlineoptionsparser_test.cpp +++ b/src/engine/test/commandlineoptionsparser_test.cpp @@ -245,9 +245,9 @@ using ExtParserType = CommandLineOptionsParser; using ExtCommandLineOptionType = AllowedCommandLineOptionsBase::CommandLineOptionType; using ExtCommandLineOptionWithValue = AllowedCommandLineOptionsBase::CommandLineOptionWithValue; -static constexpr ExtCommandLineOptionWithValue kAdditionalOpts[] = { - {{{"Monitoring", 3}, "--optExt", "", "extension value string"}, &OptsExt::sv2}, - {{{"Monitoring", 3}, "--intExt", "", "extension value int"}, &OptsExt::int3Opt}, +static constexpr std::array kAdditionalOpts = { + ExtCommandLineOptionWithValue{{{"Monitoring", 3}, "--optExt", "", "extension value string"}, &OptsExt::sv2}, + ExtCommandLineOptionWithValue{{{"Monitoring", 3}, "--intExt", "", "extension value int"}, &OptsExt::int3Opt}, }; class CommandLineOptionsParserExtTest : public ::testing::Test { @@ -264,8 +264,7 @@ class CommandLineOptionsParserExtTest : public ::testing::Test { }; TEST_F(CommandLineOptionsParserExtTest, AppendOtherOptions) { - static_assert(StaticCommandLineOptionsDuplicatesCheck(std::to_array(MainOptions::value), - std::to_array(kAdditionalOpts)), + static_assert(StaticCommandLineOptionsDuplicatesCheck(std::to_array(MainOptions::value), kAdditionalOpts), "It should detect no duplicated option names"); EXPECT_EQ(createOptions({"--optSV1", "Hey Listen!"}).sv, std::string_view("Hey Listen!")); EXPECT_EQ(createOptions({"--optExt", "I am your father"}).sv2, std::string_view("I am your father"));