Skip to content

Commit

Permalink
Add insert_or_assign for MonetaryAmountByCurrencySet
Browse files Browse the repository at this point in the history
  • Loading branch information
sjanel committed Nov 11, 2024
1 parent 73c3684 commit f731bd3
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 21 deletions.
4 changes: 2 additions & 2 deletions cmake/CoincenterUtils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function (target_set_coincenter_options name)
endif()
else()
if(MSVC)
target_compile_options(${name} PRIVATE /W1)
target_compile_options(${name} PRIVATE /W1)
else()
target_compile_options(${name} PRIVATE -Wdisabled-optimization -Winline)
endif()
Expand All @@ -46,4 +46,4 @@ function(add_coincenter_executable name)
add_executable(${name} ${MY_UNPARSED_ARGUMENTS})

target_set_coincenter_options(${name})
endfunction()
endfunction()
1 change: 1 addition & 0 deletions src/api/common/src/exchangeprivateapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "monetaryamount.hpp"
#include "monetaryamountbycurrencyset.hpp"
#include "orderid.hpp"
#include "permanentcurloptions.hpp"
#include "priceoptions.hpp"
#include "priceoptionsdef.hpp"
#include "recentdeposit.hpp"
Expand Down
1 change: 1 addition & 0 deletions src/api/common/src/exchangepublicapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "market.hpp"
#include "marketorderbook.hpp"
#include "monetaryamount.hpp"
#include "permanentcurloptions.hpp"
#include "priceoptions.hpp"
#include "priceoptionsdef.hpp"
#include "public-trade-vector.hpp"
Expand Down
1 change: 0 additions & 1 deletion src/api/exchanges/src/binancepublicapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
#include "permanentcurloptions.hpp"
#include "public-trade-vector.hpp"
#include "request-retry.hpp"
#include "runmodes.hpp"
#include "timedef.hpp"
#include "tradeside.hpp"
#include "volumeandpricenbdecimals.hpp"
Expand Down
9 changes: 9 additions & 0 deletions src/basic-objects/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,13 @@ add_unit_test(
coincenter_basic-objects
DEFINITIONS
CCT_DISABLE_SPDLOG
)

add_unit_test(
monetaryamountbycurrencyset_test
test/monetaryamountbycurrencyset_test.cpp
LIBRARIES
coincenter_basic-objects
DEFINITIONS
CCT_DISABLE_SPDLOG
)
8 changes: 4 additions & 4 deletions src/basic-objects/include/cct_const.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace cct {

enum class SupportedExchangeName : int8_t { binance, bithumb, huobi, kraken, kucoin, upbit };
enum class ExchangeNameEnum : int8_t { binance, bithumb, huobi, kraken, kucoin, upbit };

static constexpr std::string_view kDefaultDataDir = CCT_DATA_DIR;

Expand All @@ -32,16 +32,16 @@ static constexpr std::string_view kExchangeConfigFileName = "exchangeconfig.json

// To make enum serializable as strings
template <>
struct glz::meta<cct::SupportedExchangeName> {
using enum cct::SupportedExchangeName;
struct glz::meta<cct::ExchangeNameEnum> {
using enum cct::ExchangeNameEnum;

static constexpr auto value = enumerate(binance, bithumb, huobi, kraken, kucoin, upbit);
};

namespace cct {

/// Ordered list of supported exchange names.
static constexpr auto kSupportedExchanges = json::reflect<SupportedExchangeName>::keys;
static constexpr auto kSupportedExchanges = json::reflect<ExchangeNameEnum>::keys;

static_assert(std::ranges::is_sorted(kSupportedExchanges));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ class MonetaryAmountByCurrencySet {

void clear() noexcept { _set.clear(); }

const_iterator find(const MonetaryAmount &v) const { return _set.find(v); }
bool contains(const MonetaryAmount &v) const { return find(v) != end(); }
const_iterator find(const value_type &v) const { return _set.find(v); }
bool contains(const value_type &v) const { return find(v) != end(); }

const_iterator find(CurrencyCode standardCode) const {
// This is possible as MonetaryAmount are ordered by standard code
Expand All @@ -65,22 +65,46 @@ class MonetaryAmountByCurrencySet {

bool contains(CurrencyCode standardCode) const { return find(standardCode) != end(); }

std::pair<iterator, bool> insert(const MonetaryAmount &v) { return _set.insert(v); }
std::pair<iterator, bool> insert(MonetaryAmount &&v) { return _set.insert(std::move(v)); }
std::pair<iterator, bool> insert(const value_type &v) { return _set.insert(v); }
std::pair<iterator, bool> insert(value_type &&v) { return _set.insert(std::move(v)); }

iterator insert(const_iterator hint, const MonetaryAmount &v) { return _set.insert(hint, v); }
iterator insert(const_iterator hint, MonetaryAmount &&v) { return _set.insert(hint, std::move(v)); }
iterator insert(const_iterator hint, const value_type &v) { return _set.insert(hint, v); }
iterator insert(const_iterator hint, value_type &&v) { return _set.insert(hint, std::move(v)); }

template <class InputIt>
void insert(InputIt first, InputIt last) {
_set.insert(first, last);
}

iterator insert_or_assign(const value_type &v) {
auto [it, inserted] = _set.insert(v);
// We only change the amount, not the currency, so the order is still valid
const_cast<value_type &>(*it) = v;
return it;
}

// Inserts elements in the set as if by insert, but if an amount in the range [first, last) already exists in the
// set for a given currency, assigns the amount of that element with the amount of this MonetaryAmount being inserted.
template <class InputIt>
void insert_or_assign(InputIt first, InputIt last) {
iterator insertHint = begin();
while (first != last) {
insertHint = _set.insert(insertHint, *first);

// We only change the amount, not the currency, so the order is still valid
const_cast<value_type &>(*insertHint) = *first;

++first;
}
}

template <class... Args>
std::pair<iterator, bool> emplace(Args &&...args) {
return _set.emplace(std::forward<Args &&>(args)...);
}

auto operator<=>(const MonetaryAmountByCurrencySet &) const = default;

using trivially_relocatable = is_trivially_relocatable<SetType>::type;

private:
Expand Down
31 changes: 31 additions & 0 deletions src/basic-objects/test/monetaryamountbycurrencyset_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "monetaryamountbycurrencyset.hpp"

#include <gtest/gtest.h>

namespace cct {

class MonetaryAmountByCurrencySetTest : public ::testing::Test {
protected:
MonetaryAmountByCurrencySet set{MonetaryAmount("1.5 EUR"), MonetaryAmount("2.5 DOGE"), MonetaryAmount("3.5 BTC")};
};

TEST_F(MonetaryAmountByCurrencySetTest, InsertOrAssign) {
set.insert_or_assign(MonetaryAmount("2.5 EUR"));
auto it = set.find(CurrencyCode("EUR"));
ASSERT_NE(it, set.end());
EXPECT_EQ(*it, MonetaryAmount("2.5 EUR"));
}

TEST_F(MonetaryAmountByCurrencySetTest, InsertOrAssignRange) {
MonetaryAmountByCurrencySet other{MonetaryAmount("4 ETH"), MonetaryAmount("5.5 DOGE"), MonetaryAmount("6.5 POW")};
set.insert_or_assign(other.begin(), other.end()); // Inserting the same currency as in set should overwrite the value

EXPECT_EQ(set.size(), 5U);

MonetaryAmountByCurrencySet expected{MonetaryAmount("1.5 EUR"), MonetaryAmount("4 ETH"), MonetaryAmount("5.5 DOGE"),
MonetaryAmount("6.5 POW"), MonetaryAmount("3.5 BTC")};

EXPECT_EQ(set, expected);
}

} // namespace cct
18 changes: 10 additions & 8 deletions src/schema/include/read-json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
namespace cct {

namespace {
constexpr auto JsonOptions = json::opts{.raw_string = true}; // NOLINT(readability-implicit-bool-conversion)
constexpr auto kJsonOptions =
json::opts{.error_on_const_read = true, .raw_string = true}; // NOLINT(readability-implicit-bool-conversion)
}

template <json::opts opts = kJsonOptions>
void ReadJsonOrThrow(std::string_view strContent, auto &outObject) {
auto ec = json::read<JsonOptions>(outObject, strContent);
auto ec = json::read<opts>(outObject, strContent);

if (ec) {
std::string_view prefixJsonContent = strContent.substr(0, std::min<int>(strContent.size(), 20));
Expand All @@ -22,23 +24,23 @@ void ReadJsonOrThrow(std::string_view strContent, auto &outObject) {
}
}

template <class T>
template <class T, json::opts opts = kJsonOptions>
T ReadJsonOrThrow(std::string_view strContent) {
T outObject;
ReadJsonOrThrow(strContent, outObject);
ReadJsonOrThrow<opts>(strContent, outObject);
return outObject;
}

template <class T>
template <class T, json::opts opts = kJsonOptions>
T ReadJsonOrThrow(const Reader &reader) {
return ReadJsonOrThrow<T>(reader.readAll());
return ReadJsonOrThrow<T, opts>(reader.readAll());
}

template <class T>
template <class T, json::opts opts = kJsonOptions>
T ReadJsonOrCreateFile(const File &file) {
T outObject;
if (file.exists()) {
ReadJsonOrThrow(file.readAll(), outObject);
ReadJsonOrThrow<opts>(file.readAll(), outObject);
} else {
file.write(WriteJsonOrThrow<true>(outObject));
}
Expand Down

0 comments on commit f731bd3

Please sign in to comment.