Skip to content

Commit

Permalink
Fix OrderSource::valueOf
Browse files Browse the repository at this point in the history
  • Loading branch information
AnatolyKalin committed Oct 16, 2023
1 parent 4839c7b commit 97a4794
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 47 deletions.
13 changes: 8 additions & 5 deletions include/dxfeed_graal_cpp_api/event/market/OrderSource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::int32_t, std::reference_wrapper<const OrderSource>> INTERNAL_SOURCES_;
public:
static const std::unordered_map<std::variant<std::int32_t, std::string>, std::reference_wrapper<const OrderSource>> PREDEFINED_SOURCES;

private:

static inline std::mutex MTX_{};
static std::unordered_map<std::int32_t, OrderSource> USER_SOURCES_;
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}
Expand Down
58 changes: 16 additions & 42 deletions src/event/market/OrderSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::int32_t, std::reference_wrapper<const OrderSource>> 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::variant<std::int32_t, std::string>, std::reference_wrapper<const OrderSource>>
OrderSource::PREDEFINED_SOURCES =
[](std::initializer_list<std::reference_wrapper<const OrderSource>> orders) {
std::unordered_map<std::variant<std::int32_t, std::string>, std::reference_wrapper<const OrderSource>>
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<std::int32_t, OrderSource> OrderSource::USER_SOURCES_{};

Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
17 changes: 17 additions & 0 deletions tests/api/OrderSourceTest.cpp
Original file line number Diff line number Diff line change
@@ -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 <doctest.h>

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);
}

0 comments on commit 97a4794

Please sign in to comment.