From 97a4794c2d92b3f1b93e414f8dff754fb92a97a9 Mon Sep 17 00:00:00 2001 From: Anatoly Kalin Date: Mon, 16 Oct 2023 17:35:48 +0300 Subject: [PATCH] Fix OrderSource::valueOf --- .../event/market/OrderSource.hpp | 13 +++-- src/event/market/OrderSource.cpp | 58 +++++-------------- tests/CMakeLists.txt | 1 + tests/api/OrderSourceTest.cpp | 17 ++++++ 4 files changed, 42 insertions(+), 47 deletions(-) create mode 100644 tests/api/OrderSourceTest.cpp diff --git a/include/dxfeed_graal_cpp_api/event/market/OrderSource.hpp b/include/dxfeed_graal_cpp_api/event/market/OrderSource.hpp index 0d5d5590..46cf6a2f 100644 --- a/include/dxfeed_graal_cpp_api/event/market/OrderSource.hpp +++ b/include/dxfeed_graal_cpp_api/event/market/OrderSource.hpp @@ -26,7 +26,10 @@ class DXFCPP_EXPORT OrderSource final : public IndexedEventSource { static constexpr std::uint32_t FULL_ORDER_BOOK = 0x0008U; static constexpr std::uint32_t FLAGS_SIZE = 4U; - static const std::unordered_map> INTERNAL_SOURCES_; + public: + static const std::unordered_map, std::reference_wrapper> PREDEFINED_SOURCES; + + private: static inline std::mutex MTX_{}; static std::unordered_map USER_SOURCES_; @@ -466,7 +469,7 @@ class DXFCPP_EXPORT OrderSource final : public IndexedEventSource { * @return order source. */ static const OrderSource &valueOf(std::int32_t sourceId) noexcept { - if (auto found = INTERNAL_SOURCES_.find(sourceId); found != INTERNAL_SOURCES_.end()) { + if (auto found = PREDEFINED_SOURCES.find(sourceId); found != PREDEFINED_SOURCES.end()) { return found->second; } @@ -487,14 +490,14 @@ class DXFCPP_EXPORT OrderSource final : public IndexedEventSource { * @return order source. */ static const OrderSource &valueOf(const std::string &name) noexcept { - auto sourceId = composeId(name); - - if (auto found = INTERNAL_SOURCES_.find(sourceId); found != INTERNAL_SOURCES_.end()) { + if (auto found = PREDEFINED_SOURCES.find(name); found != PREDEFINED_SOURCES.end()) { return found->second; } std::lock_guard lock(MTX_); + auto sourceId = composeId(name); + if (auto found = USER_SOURCES_.find(sourceId); found != USER_SOURCES_.end()) { return found->second; } diff --git a/src/event/market/OrderSource.cpp b/src/event/market/OrderSource.cpp index cab471f3..3f0ab267 100644 --- a/src/event/market/OrderSource.cpp +++ b/src/event/market/OrderSource.cpp @@ -51,49 +51,23 @@ const OrderSource OrderSource::iex("iex", PUB_ORDER); const OrderSource OrderSource::MEMX("MEMX", PUB_ORDER); const OrderSource OrderSource::memx("memx", PUB_ORDER); -const std::unordered_map> OrderSource::INTERNAL_SOURCES_{ - {COMPOSITE_BID.id(), std::cref(COMPOSITE_BID)}, - {COMPOSITE_ASK.id(), std::cref(COMPOSITE_ASK)}, - {REGIONAL_BID.id(), std::cref(REGIONAL_BID)}, - {REGIONAL_ASK.id(), std::cref(REGIONAL_ASK)}, - {AGGREGATE_BID.id(), std::cref(AGGREGATE_BID)}, - {AGGREGATE_ASK.id(), std::cref(AGGREGATE_ASK)}, - {DEFAULT.id(), std::cref(DEFAULT)}, +const std::unordered_map, std::reference_wrapper> + OrderSource::PREDEFINED_SOURCES = + [](std::initializer_list> orders) { + std::unordered_map, std::reference_wrapper> + result{}; - {NTV.id(), std::cref(NTV)}, - {ntv.id(), std::cref(ntv)}, - {NFX.id(), std::cref(NFX)}, - {ESPD.id(), std::cref(ESPD)}, - {XNFI.id(), std::cref(XNFI)}, - {ICE.id(), std::cref(ICE)}, - {ISE.id(), std::cref(ISE)}, - {DEA.id(), std::cref(DEA)}, - {DEX.id(), std::cref(DEX)}, - {dex.id(), std::cref(dex)}, - {BYX.id(), std::cref(BYX)}, - {BZX.id(), std::cref(BZX)}, - {bzx.id(), std::cref(bzx)}, - {BATE.id(), std::cref(BATE)}, - {CHIX.id(), std::cref(CHIX)}, - {CEUX.id(), std::cref(CEUX)}, - {BXTR.id(), std::cref(BXTR)}, - {IST.id(), std::cref(IST)}, - {BI20.id(), std::cref(BI20)}, - {ABE.id(), std::cref(ABE)}, - {FAIR.id(), std::cref(FAIR)}, - {GLBX.id(), std::cref(GLBX)}, - {glbx.id(), std::cref(glbx)}, - {ERIS.id(), std::cref(ERIS)}, - {XEUR.id(), std::cref(XEUR)}, - {xeur.id(), std::cref(xeur)}, - {CFE.id(), std::cref(CFE)}, - {C2OX.id(), std::cref(C2OX)}, - {SMFE.id(), std::cref(SMFE)}, - {smfe.id(), std::cref(smfe)}, - {iex.id(), std::cref(iex)}, - {MEMX.id(), std::cref(MEMX)}, - {memx.id(), std::cref(memx)}, -}; + for (auto &&o : orders) { + result.emplace(o.get().id(), o); + result.emplace(o.get().name(), o); + } + + return result; + }({COMPOSITE_BID, COMPOSITE_ASK, REGIONAL_BID, REGIONAL_ASK, AGGREGATE_BID, AGGREGATE_ASK, DEFAULT, NTV, + ntv, NFX, ESPD, XNFI, ICE, ISE, DEA, DEX, + dex, BYX, BZX, bzx, BATE, CHIX, CEUX, BXTR, + IST, BI20, ABE, FAIR, GLBX, glbx, ERIS, XEUR, + xeur, CFE, C2OX, SMFE, smfe, iex, MEMX, memx}); std::unordered_map OrderSource::USER_SOURCES_{}; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index ccbe75f4..55690328 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -24,6 +24,7 @@ set(DXFC_TEST_SOURCES api/DXEndpointTest.cpp api/DXPublisherTest.cpp api/MarketEventSymbolsTest.cpp + api/OrderSourceTest.cpp system/SystemTest.cpp) #add_definitions(-DDXFCPP_DEBUG -DDXFCPP_DEBUG_ISOLATES) diff --git a/tests/api/OrderSourceTest.cpp b/tests/api/OrderSourceTest.cpp new file mode 100644 index 00000000..1575455f --- /dev/null +++ b/tests/api/OrderSourceTest.cpp @@ -0,0 +1,17 @@ +// Copyright (c) 2023 Devexperts LLC. +// SPDX-License-Identifier: MPL-2.0 + +#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN + +#include "dxfeed_graal_cpp_api/api.hpp" +#include + +using namespace dxfcpp; +using namespace std::literals; + +TEST_CASE("OrderSource::valueOf the method should work correctly with predefined sources") { + REQUIRE(OrderSource::valueOf("COMPOSITE_ASK") == OrderSource::COMPOSITE_ASK); + REQUIRE(OrderSource::valueOf("NTV") == OrderSource::NTV); + REQUIRE(OrderSource::valueOf("NTV") != OrderSource::ntv); + REQUIRE(OrderSource::valueOf("NTV2") != OrderSource::NTV); +}