Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernize array with std::array without template parameter arguments when applicable #481

Merged
merged 1 commit into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/api/exchanges/src/binancepublicapi.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "binancepublicapi.hpp"

#include <algorithm>
#include <array>
#include <cassert>
#include <chrono>
#include <cstdint>
Expand Down Expand Up @@ -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}};
Expand Down
5 changes: 3 additions & 2 deletions src/api/exchanges/src/huobipublicapi.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "huobipublicapi.hpp"

#include <algorithm>
#include <array>
#include <cstdint>
#include <memory>
#include <string_view>
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions src/api/exchanges/src/kucoinpublicapi.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include "kucoinpublicapi.hpp"

#include <algorithm>
#include <array>
#include <cassert>
#include <cstdint>
#include <iterator>
#include <memory>
#include <string_view>
#include <utility>

Expand Down Expand Up @@ -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);
}

Expand Down
4 changes: 2 additions & 2 deletions src/api/exchanges/src/upbitpublicapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<MonetaryAmount, 2> 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) {
Expand Down
6 changes: 3 additions & 3 deletions src/engine/include/commandlineoptionsparser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <iterator>
#include <optional>
#include <ostream>
#include <ranges>
#include <span>
#include <string_view>
#include <type_traits>
Expand Down Expand Up @@ -44,9 +45,8 @@ class CommandLineOptionsParser {
append(init);
}

template <unsigned N>
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);
Expand Down
2 changes: 1 addition & 1 deletion src/engine/src/metricsexporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
9 changes: 4 additions & 5 deletions src/engine/test/commandlineoptionsparser_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,9 @@ using ExtParserType = CommandLineOptionsParser<OptsExt>;
using ExtCommandLineOptionType = AllowedCommandLineOptionsBase<OptsExt>::CommandLineOptionType;
using ExtCommandLineOptionWithValue = AllowedCommandLineOptionsBase<OptsExt>::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 {
Expand All @@ -264,8 +264,7 @@ class CommandLineOptionsParserExtTest : public ::testing::Test {
};

TEST_F(CommandLineOptionsParserExtTest, AppendOtherOptions) {
static_assert(StaticCommandLineOptionsDuplicatesCheck(std::to_array(MainOptions<OptsExt>::value),
std::to_array(kAdditionalOpts)),
static_assert(StaticCommandLineOptionsDuplicatesCheck(std::to_array(MainOptions<OptsExt>::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"));
Expand Down
Loading