Skip to content

Commit

Permalink
[DXFC-402] Implement adding multiple symbols and removing symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
AnatolyKalin committed May 26, 2023
1 parent ce4cb2d commit 2955594
Show file tree
Hide file tree
Showing 247 changed files with 225 additions and 113,355 deletions.
13 changes: 10 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ set(CMAKE_C_STANDARD 11)

cmake_policy(SET CMP0135 NEW)

if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
set(DXFC_IS_ROOT_PROJECT ON)
else()
else ()
set(DXFC_ROOT_PROJECT OFF)
endif()
endif ()

option(DXFC_BUILD_UNIT_TESTS "" ON)

Expand Down Expand Up @@ -84,6 +84,12 @@ set(dxFeedNativeAPIAPISources
src/api/DXFeedSubscription.cpp
)

set(dxFeedNativeAPISymbolsSources
src/symbols/StringSymbol.cpp
src/symbols/WildcardSymbol.cpp
src/symbols/SymbolWrapper.cpp
)

set(dxFeedNativeAPISystemSources
src/system/System.cpp
)
Expand Down Expand Up @@ -125,6 +131,7 @@ add_library(dxFeedGraalCxxApi
${dxFeedNativeAPIInternalUtilsSources}
${dxFeedNativeAPIInternalUtilsDebugSources}
${dxFeedNativeAPIAPISources}
${dxFeedNativeAPISymbolsSources}
${dxFeedNativeAPISystemSources}
${dxFeedNativeAPIEventSources}
${dxFeedNativeAPIEventMarketSources}
Expand Down
5 changes: 4 additions & 1 deletion include/dxfeed_graal_cpp_api/api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
#include "internal/Common.hpp"
#include "internal/Enum.hpp"
#include "internal/EventClassList.hpp"
#include "internal/SymbolList.hpp"
#include "internal/Id.hpp"
#include "internal/JavaObjectHandler.hpp"
#include "internal/NonCopyable.hpp"
#include "internal/RawListWrapper.hpp"
#include "internal/SymbolList.hpp"
#include "internal/context/ApiContext.hpp"
#include "internal/managers/DXEndpointManager.hpp"
#include "internal/managers/DXFeedSubscriptionManager.hpp"
Expand All @@ -24,4 +24,7 @@
#include "api/DXFeed.hpp"
#include "api/DXFeedSubscription.hpp"
#include "event/DXEvent.hpp"
#include "symbols/StringSymbol.hpp"
#include "symbols/WildcardSymbol.hpp"
#include "symbols/SymbolWrapper.hpp"
#include "system/System.hpp"
15 changes: 11 additions & 4 deletions include/dxfeed_graal_cpp_api/api/DXFeedSubscription.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#include "../event/DXEvent.hpp"
#include "../internal/Common.hpp"
#include "../symbols/StringSymbol.hpp"
#include "../symbols/WildcardSymbol.hpp"

#include <unordered_set>

Expand Down Expand Up @@ -65,6 +67,8 @@ class DXFeedSubscription : public SharedEntity {

void addSymbolImpl(const char *symbol) noexcept;

void addSymbolImpl(void* graalSymbol) noexcept;

void removeSymbolImpl(const char *symbol) noexcept;

public:
Expand Down Expand Up @@ -227,17 +231,20 @@ class DXFeedSubscription : public SharedEntity {

template <typename Symbol> void addSymbol(Symbol &&symbol) noexcept {
if constexpr (Debugger::isDebug) {
Debugger::debug(toString() + "::addSymbol(symbol = " + std::string(symbol) + ")");
Debugger::debug(toString() + "::addSymbol<" + typeid(symbol).name() + ">(symbol = " + std::string(symbol) +
")");
}

if constexpr (std::is_same_v<std::decay_t<Symbol>, std::string>) {
if constexpr (std::is_same_v<std::decay_t<Symbol>, WildcardSymbol>) {
addSymbolImpl(symbol.toGraal());
} if constexpr (ConvertibleToStringSymbol<Symbol>) {
addSymbolImpl(StringSymbol(std::forward<Symbol>(symbol)).toGraal());
} else if constexpr (std::is_same_v<std::decay_t<Symbol>, std::string>) {
addSymbolImpl(symbol.c_str());
}
#if __cpp_lib_string_view
else if constexpr (std::is_same_v<std::decay_t<Symbol>, std::string_view>) {
addSymbolImpl(symbol.data());
}
#endif
else {
addSymbolImpl(symbol);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#pragma once

// #define DXFCPP_DEBUG 1
#define DXFCPP_DEBUG 1
// #define DXFCPP_TRACE_LISTS 1
// #define DXFCPP_TRACE_ISOLATES 1

Expand Down
52 changes: 52 additions & 0 deletions include/dxfeed_graal_cpp_api/symbols/StringSymbol.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (c) 2023 Devexperts LLC.
// SPDX-License-Identifier: MPL-2.0

#pragma once

#include <cstdint>
#include <memory>
#include <utility>
#include <variant>

namespace dxfcpp {

struct StringSymbol {
using DataType = typename std::variant<std::string_view, std::string>;

private:
DataType data_;

struct Impl;
std::unique_ptr<Impl> impl_;

public:
StringSymbol() noexcept;
~StringSymbol() noexcept;

template <typename SymbolType> StringSymbol(SymbolType &&symbol) noexcept : StringSymbol() {
if constexpr (std::is_convertible_v<SymbolType, std::string_view>) {
data_ = std::string_view(std::forward<SymbolType>(symbol));
} else if constexpr (std::is_convertible_v<SymbolType, std::string>) {
data_ = std::string(std::forward<SymbolType>(symbol));
}
}

void *toGraal();

std::string toString() const noexcept {
return "StringSymbol{" + std::visit([](auto &&d) { return std::string(d); }, data_) + "}";
}

operator std::string() const noexcept { return toString(); }
};

template <typename T>
concept ConvertibleToStringSymbol = std::is_convertible_v<T, std::string> || std::is_convertible_v<T, std::string_view>;

inline namespace literals {

inline StringSymbol operator""_s(const char *string, size_t length) noexcept { return {std::string_view{string, length}}; }

} // namespace literals

} // namespace dxfcpp
14 changes: 14 additions & 0 deletions include/dxfeed_graal_cpp_api/symbols/SymbolWrapper.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) 2023 Devexperts LLC.
// SPDX-License-Identifier: MPL-2.0

#pragma once

#include <cstdint>
#include <memory>
#include <utility>

namespace dxfcpp {

struct SymbolWrapper {};

} // namespace dxfcpp
42 changes: 42 additions & 0 deletions include/dxfeed_graal_cpp_api/symbols/WildcardSymbol.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) 2023 Devexperts LLC.
// SPDX-License-Identifier: MPL-2.0

#pragma once

#include <cstdint>
#include <memory>
#include <utility>

#include "StringSymbol.hpp"

namespace dxfcpp {

struct StringSymbol;

struct WildcardSymbol {
static const WildcardSymbol ALL;

private:
const std::string symbol;

WildcardSymbol(std::string symbol) : symbol{std::move(symbol)} {}

public:
const std::string &getSymbol() const noexcept;

void *toGraal() const noexcept;

std::string toString() const noexcept { return getSymbol(); }

operator std::string() const noexcept { return toString(); }
};

inline namespace literals {

inline WildcardSymbol operator""_ws(const char *string, size_t length) noexcept { return WildcardSymbol::ALL; }

inline WildcardSymbol operator""_wcs(const char *string, size_t length) noexcept { return WildcardSymbol::ALL; }

} // namespace literals

} // namespace dxfcpp
18 changes: 13 additions & 5 deletions samples/cpp/PrintQuoteEvents/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,25 @@ auto cApiStateToString(dxfc_dxendpoint_state_t state) {
}

#if !defined(__PRETTY_FUNCTION__) && !defined(__GNUC__)
#define __PRETTY_FUNCTION__ __FUNCSIG__
# define __PRETTY_FUNCTION__ __FUNCSIG__
#endif


int main() {
{
using namespace std::string_literals;
using namespace std::string_view_literals;
using namespace dxfcpp::literals;

auto s = dxfcpp::StringSymbol("123123");
s.toGraal();

auto builder = dxfcpp::DXEndpoint::newBuilder()->withRole(dxfcpp::DXEndpoint::Role::FEED);
//dxfcpp::System::setProperty("dxfeed.wildcard.enable", "true");
auto builder = dxfcpp::DXEndpoint::newBuilder()
->withRole(dxfcpp::DXEndpoint::Role::FEED)
->withProperty(dxfcpp::DXEndpoint::DXFEED_WILDCARD_ENABLE_PROPERTY, "true");
auto endpoint = builder->build();

auto sub =
endpoint->getFeed()->createSubscription({dxfcpp::EventTypeEnum::QUOTE});
auto sub = endpoint->getFeed()->createSubscription({dxfcpp::EventTypeEnum::QUOTE});

sub->addEventListener([](auto &&events) {
for (const auto &e : events) {
Expand All @@ -46,6 +51,9 @@ int main() {
sub->addSymbol("AAPL");
sub->addSymbol("IBM"sv);
sub->addSymbol("TSLA"s);
sub->addSymbol(dxfcpp::WildcardSymbol::ALL);
sub->addSymbol("AMD"_s);
sub->addSymbol("*"_wcs);

endpoint->connect("demo.dxfeed.com:7300");

Expand Down
11 changes: 10 additions & 1 deletion src/api/DXFeedSubscription.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,16 @@ void DXFeedSubscription::addSymbolImpl(const char *symbol) noexcept {
[handler = bit_cast<dxfg_subscription_t *>(handler_.get()), symbol](auto threadHandle) {
dxfg_string_symbol_t s{{STRING}, symbol};

return dxfg_DXFeedSubscription_addSymbol(threadHandle, handler, (dxfg_symbol_t *)&s) == 0;
return dxfg_DXFeedSubscription_addSymbol(threadHandle, handler, bit_cast<dxfg_symbol_t *>(&s)) == 0;
},
false);
}

void DXFeedSubscription::addSymbolImpl(void *graalSymbol) noexcept {
runIsolatedOrElse(
[handler = bit_cast<dxfg_subscription_t *>(handler_.get()), graalSymbol](auto threadHandle) {
return dxfg_DXFeedSubscription_addSymbol(threadHandle, handler, bit_cast<dxfg_symbol_t *>(graalSymbol)) ==
0;
},
false);
}
Expand Down
2 changes: 0 additions & 2 deletions src/internal/SymbolList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@ struct SymbolList::Impl

dxfgSymbol = bit_cast<dxfg_symbol_t *>(&s);
}
#if __cpp_lib_string_view
else if constexpr (std::is_same_v<std::decay_t<Symbol>, std::string_view>) {
dxfg_string_symbol_t s{{STRING}, symbol.data()};

dxfgSymbol = bit_cast<dxfg_symbol_t *>(&s);
}
#endif
else {
dxfg_string_symbol_t s{{STRING}, symbol};

Expand Down
37 changes: 37 additions & 0 deletions src/symbols/StringSymbol.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) 2023 Devexperts LLC.
// SPDX-License-Identifier: MPL-2.0

#include <dxfg_api.h>

#include <dxfeed_graal_c_api/api.h>
#include <dxfeed_graal_cpp_api/api.hpp>

#include <memory>

namespace dxfcpp {

struct StringSymbol::Impl {
dxfg_string_symbol_t graalSymbol{{STRING}, nullptr};
};

StringSymbol::StringSymbol() noexcept : impl_{std::make_unique<StringSymbol::Impl>()} {}

StringSymbol::~StringSymbol() noexcept = default;

void *StringSymbol::toGraal() {
if (impl_->graalSymbol.symbol == nullptr) {
std::visit(
[this]<typename SymbolType>(SymbolType symbol) {
if constexpr (std::is_same_v<SymbolType, std::string>) {
impl_->graalSymbol.symbol = symbol.c_str();
} else if constexpr (std::is_same_v<SymbolType, std::string_view>) {
impl_->graalSymbol.symbol = symbol.data();
}
},
data_);
}

return bit_cast<void *>(&impl_->graalSymbol);
}

} // namespace dxfcpp
10 changes: 10 additions & 0 deletions src/symbols/SymbolWrapper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright (c) 2023 Devexperts LLC.
// SPDX-License-Identifier: MPL-2.0

#include <dxfeed_graal_c_api/api.h>
#include <dxfeed_graal_cpp_api/api.hpp>

namespace dxfcpp {


}
21 changes: 21 additions & 0 deletions src/symbols/WildcardSymbol.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) 2023 Devexperts LLC.
// SPDX-License-Identifier: MPL-2.0

#include <dxfg_api.h>

#include <dxfeed_graal_c_api/api.h>
#include <dxfeed_graal_cpp_api/api.hpp>

namespace dxfcpp {

const WildcardSymbol WildcardSymbol::ALL{"*"};

void *WildcardSymbol::toGraal() const noexcept {
static const dxfg_wildcard_symbol_t wildcardGraalSymbol{{WILDCARD}};

return bit_cast<void *>(&wildcardGraalSymbol);
}

const std::string &WildcardSymbol::getSymbol() const noexcept { return symbol; }

} // namespace dxfcpp
8 changes: 0 additions & 8 deletions third_party/fmt-9.1.0/.clang-format

This file was deleted.

Loading

0 comments on commit 2955594

Please sign in to comment.