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 22, 2023
1 parent db84275 commit fa70e60
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 79 deletions.
72 changes: 59 additions & 13 deletions include/dxfeed_graal_cpp_api/api/DXFeedSubscription.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ class DXFeedSubscription : public std::enable_shared_from_this<DXFeedSubscriptio

bool isClosedImpl() noexcept;

void addSymbolImpl(const char *symbol) noexcept;

void removeSymbolImpl(const char *symbol) noexcept;

public:
std::string toString() const { return fmt::format("DXFeedSubscription{{{}}}", handler_.toString()); }

Expand Down Expand Up @@ -194,6 +198,20 @@ class DXFeedSubscription : public std::enable_shared_from_this<DXFeedSubscriptio
closeImpl();
}

/**
* Adds listener for events.
* Event lister can be added only when subscription is not producing any events.
* The subscription must be either empty
* (its set of @ref ::getSymbols() "symbols" is empty or not @ref ::attach() "attached" to any feed
* (its set of change listeners is empty).
*
* This method does nothing if this subscription is closed.
*
* @tparam EventListener The listener type. Listener can be callable with signature: `void(const
* std::vector<std::shared_ptr<EventType>&)`
* @param listener The event listener
* @return The listener id
*/
template <typename EventListener>
std::size_t addEventListener(EventListener &&listener) noexcept
#if __cpp_concepts
Expand All @@ -205,31 +223,59 @@ class DXFeedSubscription : public std::enable_shared_from_this<DXFeedSubscriptio
return onEvent_ += listener;
}

/**
* Removes listener for events.
*
* @param listenerId The listener id
*/
void removeEventListener(std::size_t listenerId) noexcept { onEvent_ -= listenerId; }

/**
* Returns a reference to an incoming events' handler (delegate), to which listeners can be added and removed.
*
* @return The incoming events' handler (delegate)
*/
const auto &onEvent() noexcept { return onEvent_; }

template <typename Symbol> void addSymbol(Symbol &&symbol) noexcept {
if constexpr (isDebug) {
debug("{}::addSymbol(symbol = {})", toString(), symbol);
}

if constexpr (std::is_same_v<std::decay_t<Symbol>, std::string>) {
addSymbolImpl(symbol.c_str());
}
#if __cpp_lib_string_view
void addSymbol(std::string_view symbol) noexcept;
else if constexpr (std::is_same_v<std::decay_t<Symbol>, std::string_view>) {
addSymbolImpl(symbol.data());
}
#endif
else {
addSymbolImpl(symbol);
}
}

void addSymbol(const char *symbol) noexcept;

void addSymbol(const std::string &symbol) noexcept;

template <typename SymbolsCollection> void addSymbols(SymbolsCollection &&collection) noexcept;

template <typename Symbol> void addSymbols(std::initializer_list<Symbol> collection) noexcept;

template <typename Symbol> void removeSymbol(Symbol &&symbol) noexcept;
template <typename Symbol> void removeSymbol(Symbol &&symbol) noexcept {
if constexpr (isDebug) {
debug("{}::removeSymbol(symbol = {})", toString(), symbol);
}

if constexpr (std::is_same_v<std::decay_t<Symbol>, std::string>) {
removeSymbolImpl(symbol.c_str());
}
#if __cpp_lib_string_view
void removeSymbol(std::string_view symbol) noexcept;
else if constexpr (std::is_same_v<std::decay_t<Symbol>, std::string_view>) {
removeSymbolImpl(symbol.data());
}
#endif
else {
removeSymbolImpl(symbol);
}
}

void removeSymbol(const char *symbol) noexcept;
template <typename SymbolsCollection> void addSymbols(SymbolsCollection &&collection) noexcept;

void removeSymbol(const std::string &symbol) noexcept;
template <typename Symbol> void addSymbols(std::initializer_list<Symbol> collection) noexcept;

template <typename SymbolsCollection> void removeSymbols(SymbolsCollection &&collection) noexcept;

Expand Down
4 changes: 4 additions & 0 deletions include/dxfeed_graal_cpp_api/internal/Isolate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,9 @@ class Isolate final {
}

template <typename F, typename R>
#if __cpp_concepts
requires std::convertible_to<R, std::invoke_result_t<F &&, GraalIsolateThreadHandle>>
#endif
auto runIsolatedOrElse(F &&f, R defaultValue) {
return std::visit(
[defaultValue =
Expand Down Expand Up @@ -280,7 +282,9 @@ class Isolate final {
template <typename F> auto runIsolated(F &&f) { return Isolate::getInstance()->runIsolated(std::forward<F>(f)); }

template <typename F, typename R>
#if __cpp_concepts
requires std::convertible_to<R, std::invoke_result_t<F &&, GraalIsolateThreadHandle>>
#endif
auto runIsolatedOrElse(F &&f, R defaultValue) {
return Isolate::getInstance()->runIsolatedOrElse(std::forward<F>(f), std::move(defaultValue));
}
Expand Down
5 changes: 5 additions & 0 deletions samples/cpp/PrintQuoteEvents/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ auto cApiStateToString(dxfc_dxendpoint_state_t state) {
return "";
}

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


int main() {
{
using namespace std::string_literals;
Expand Down
71 changes: 5 additions & 66 deletions src/api/DXFeedSubscription.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <dxfeed_graal_cpp_api/api.hpp>

#include <memory>
#include <typeinfo>

namespace dxfcpp {

Expand All @@ -25,27 +26,13 @@ void DXFeedSubscription::detach(std::shared_ptr<DXFeed> feed) noexcept {
feed->detachSubscription(shared_from_this());
}

#if __cpp_lib_string_view
void DXFeedSubscription::addSymbol(std::string_view symbol) noexcept {
template <typename Symbol> auto symbolDataRetriever(Symbol &&s) {
if constexpr (isDebug) {
debug("{}::addSymbol(symbol = {})", toString(), symbol);
debug("{}", typeid(decltype(s)).name());
}

runIsolatedOrElse(
[handler = bit_cast<dxfg_subscription_t *>(handler_.get()), symbol](auto threadHandle) {
dxfg_string_symbol_t s{{STRING}, symbol.data()};

return dxfg_DXFeedSubscription_addSymbol(threadHandle, handler, (dxfg_symbol_t *)&s) == 0;
},
false);
}
#endif

void DXFeedSubscription::addSymbol(const char *symbol) noexcept {
if constexpr (isDebug) {
debug("{}::addSymbol(symbol = {})", toString(), symbol);
}

void DXFeedSubscription::addSymbolImpl(const char *symbol) noexcept {
runIsolatedOrElse(
[handler = bit_cast<dxfg_subscription_t *>(handler_.get()), symbol](auto threadHandle) {
dxfg_string_symbol_t s{{STRING}, symbol};
Expand All @@ -55,41 +42,7 @@ void DXFeedSubscription::addSymbol(const char *symbol) noexcept {
false);
}

void DXFeedSubscription::addSymbol(const std::string &symbol) noexcept {
if constexpr (isDebug) {
debug("{}::addSymbol(symbol = {})", toString(), symbol);
}

runIsolatedOrElse(
[handler = bit_cast<dxfg_subscription_t *>(handler_.get()), symbol](auto threadHandle) {
dxfg_string_symbol_t s{{STRING}, symbol.c_str()};

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

#if __cpp_lib_string_view
void DXFeedSubscription::removeSymbol(std::string_view symbol) noexcept {
if constexpr (isDebug) {
debug("{}::removeSymbol(symbol = {})", toString(), symbol);
}

runIsolatedOrElse(
[handler = bit_cast<dxfg_subscription_t *>(handler_.get()), symbol](auto threadHandle) {
dxfg_string_symbol_t s{{STRING}, symbol.data()};

return dxfg_DXFeedSubscription_removeSymbol(threadHandle, handler, (dxfg_symbol_t *)&s) == 0;
},
false);
}
#endif

void DXFeedSubscription::removeSymbol(const char *symbol) noexcept {
if constexpr (isDebug) {
debug("{}::removeSymbol(symbol = {})", toString(), symbol);
}

void DXFeedSubscription::removeSymbolImpl(const char *symbol) noexcept {
runIsolatedOrElse(
[handler = bit_cast<dxfg_subscription_t *>(handler_.get()), symbol](auto threadHandle) {
dxfg_string_symbol_t s{{STRING}, symbol};
Expand All @@ -99,20 +52,6 @@ void DXFeedSubscription::removeSymbol(const char *symbol) noexcept {
false);
}

void DXFeedSubscription::removeSymbol(const std::string &symbol) noexcept {
if constexpr (isDebug) {
debug("{}::removeSymbol(symbol = {})", toString(), symbol);
}

runIsolatedOrElse(
[handler = bit_cast<dxfg_subscription_t *>(handler_.get()), symbol](auto threadHandle) {
dxfg_string_symbol_t s{{STRING}, symbol.c_str()};

return dxfg_DXFeedSubscription_removeSymbol(threadHandle, handler, (dxfg_symbol_t *)&s) == 0;
},
false);
}

void DXFeedSubscription::closeImpl() noexcept {
if (!handler_) {
return;
Expand Down

0 comments on commit fa70e60

Please sign in to comment.