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 19, 2023
1 parent af92d6f commit 3ba39ee
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 17 deletions.
14 changes: 7 additions & 7 deletions include/dxfeed_graal_cpp_api/api/DXEndpoint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -617,9 +617,11 @@ struct DXEndpoint : std::enable_shared_from_this<DXEndpoint> {
*/
template <typename StateChangeListener>
std::size_t addStateChangeListener(StateChangeListener &&listener) noexcept
#if __cpp_concepts
requires requires {
{ listener(State{}, State{}) } -> std::same_as<void>;
}
{ listener(State{}, State{}) } -> std::same_as<void>;
}
#endif
{
return onStateChange_ += listener;
}
Expand Down Expand Up @@ -805,7 +807,7 @@ struct DXEndpoint : std::enable_shared_from_this<DXEndpoint> {
class Builder : public std::enable_shared_from_this<Builder> {
friend DXEndpoint;

// mutable std::recursive_mutex mtx_{};
// mutable std::recursive_mutex mtx_{};
handler_utils::JavaObjectHandler<Builder> handler_;
Role role_ = Role::FEED;
std::unordered_map<std::string, std::string> properties_;
Expand Down Expand Up @@ -850,7 +852,7 @@ struct DXEndpoint : std::enable_shared_from_this<DXEndpoint> {
* @return `this` endpoint builder.
*/
std::shared_ptr<Builder> withName(const std::string &name) {
//TODO: check invalid utf-8
// TODO: check invalid utf-8
if constexpr (isDebug) {
debug("DXEndpoint::Builder{{{}}}::withName(name = {})", handler_.toString(), name);
}
Expand Down Expand Up @@ -933,8 +935,6 @@ struct DXEndpoint : std::enable_shared_from_this<DXEndpoint> {
return Builder::create();
}

std::string toString() const {
return fmt::format("DXEndpoint{{{}}}", handler_.toString());
}
std::string toString() const { return fmt::format("DXEndpoint{{{}}}", handler_.toString()); }
};
} // namespace dxfcpp
6 changes: 3 additions & 3 deletions include/dxfeed_graal_cpp_api/api/DXFeed.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ struct DXFeed : std::enable_shared_from_this<DXFeed> {

template <typename EventTypesCollection>
std::shared_ptr<DXFeedSubscription> createSubscription(EventTypesCollection &&eventTypes) noexcept
#if __cpp_concepts
requires requires { ElementTypeIs<EventTypesCollection, EventTypeEnum>; }
#endif
{
if constexpr (isDebug) {
debug("{}::createSubscription(eventTypes = {})", toString(),
Expand All @@ -96,9 +98,7 @@ struct DXFeed : std::enable_shared_from_this<DXFeed> {
return sub;
}

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

} // namespace dxfcpp
25 changes: 23 additions & 2 deletions include/dxfeed_graal_cpp_api/api/DXFeedSubscription.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,12 @@ class DXFeedSubscription : public std::enable_shared_from_this<DXFeedSubscriptio

template <typename EventTypesCollection>
explicit DXFeedSubscription(EventTypesCollection &&eventTypes) noexcept
#if __cpp_concepts
requires requires { ElementTypeIs<EventTypesCollection, EventTypeEnum>; }
#endif
: DXFeedSubscription(std::begin(std::forward<EventTypesCollection>(eventTypes)),
std::end(std::forward<EventTypesCollection>(eventTypes))) {}
std::end(std::forward<EventTypesCollection>(eventTypes))) {
}

void closeImpl() noexcept;

Expand Down Expand Up @@ -147,7 +150,9 @@ class DXFeedSubscription : public std::enable_shared_from_this<DXFeedSubscriptio
*/
template <typename EventTypesCollection>
static std::shared_ptr<DXFeedSubscription> create(EventTypesCollection &&eventTypes) noexcept
#if __cpp_concepts
requires requires { ElementTypeIs<EventTypesCollection, EventTypeEnum>; }
#endif
{
auto sub =
std::shared_ptr<DXFeedSubscription>(new DXFeedSubscription(std::forward<EventTypesCollection>(eventTypes)));
Expand Down Expand Up @@ -191,9 +196,11 @@ class DXFeedSubscription : public std::enable_shared_from_this<DXFeedSubscriptio

template <typename EventListener>
std::size_t addEventListener(EventListener &&listener) noexcept
#if __cpp_concepts
requires requires {
{ listener(std::vector<std::shared_ptr<EventType>>{}) } -> std::same_as<void>;
}
#endif
{
return onEvent_ += listener;
}
Expand All @@ -202,14 +209,28 @@ class DXFeedSubscription : public std::enable_shared_from_this<DXFeedSubscriptio

const auto &onEvent() noexcept { return onEvent_; }

template <typename Symbol> void addSymbol(Symbol &&symbol) noexcept;
#if __cpp_lib_string_view
void addSymbol(std::string_view symbol) noexcept;
#endif

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;

#if __cpp_lib_string_view
void removeSymbol(std::string_view symbol) noexcept;
#endif

void removeSymbol(const char *symbol) noexcept;

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

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

/**
Expand Down
6 changes: 4 additions & 2 deletions include/dxfeed_graal_cpp_api/event/EventFlag.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,11 @@ class EventFlag final {
*/
template <typename EventFlagsMask>
bool in(const EventFlagsMask &eventFlagsMask) const
#if __cpp_concepts
requires requires {
{ eventFlagsMask.getMask() } -> std::same_as<std::uint32_t>;
}
{ eventFlagsMask.getMask() } -> std::same_as<std::uint32_t>;
}
#endif
{
return in(eventFlagsMask.getMask());
}
Expand Down
2 changes: 2 additions & 0 deletions include/dxfeed_graal_cpp_api/event/EventType.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ struct EventType : public SharedEntity {

template <typename EntityType>
friend std::ostream &operator<<(std::ostream &os, const std::shared_ptr<EntityType> &e)
#if __cpp_concepts
requires(std::is_base_of_v<EventType, EntityType>)
#endif
{
return os << e->toString();
}
Expand Down
2 changes: 2 additions & 0 deletions include/dxfeed_graal_cpp_api/event/market/TradeBase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ class TradeBase : public MarketEvent, public LastingEvent {

template <typename ChildType, typename GraalNativeEventType, typename ChildGraalNativeEventType, auto clazz>
static std::shared_ptr<ChildType> fromGraalNative(void *graalNative) noexcept
#if __cpp_concepts
requires(std::is_base_of_v<TradeBase, ChildType>)
#endif
{
if (!graalNative) {
return {};
Expand Down
4 changes: 4 additions & 0 deletions include/dxfeed_graal_cpp_api/internal/Common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ constexpr inline auto is_constant_evaluated(bool default_value = false) noexcept
// Implementation of std::bit_cast for pre-C++20.
template <typename To, typename From>
constexpr To bit_cast(const From &from)
#if __cpp_concepts
requires(sizeof(To) == sizeof(From))
#endif
{
#ifdef __cpp_lib_bit_cast
if (is_constant_evaluated())
Expand Down Expand Up @@ -204,7 +206,9 @@ struct EventClassList {
} // namespace handler_utils

template <typename It>
#if __cpp_concepts
requires requires { std::is_same_v<std::decay_t<decltype(It {} -> getName())>, std::string>; }
#endif
std::string namesToString(It begin, It end) {
std::string result{"["};

Expand Down
1 change: 1 addition & 0 deletions include/dxfeed_graal_cpp_api/internal/Enum.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ template <typename Child, typename Code> struct Enum {
return found->second;
}

//TODO: try to implement C++11-like code for this
if constexpr (requires { Child::getDefault(); }) {
return Child::getDefault();
} else {
Expand Down
11 changes: 9 additions & 2 deletions samples/cpp/PrintQuoteEvents/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <dxfeed_graal_c_api/api.h>
#include <dxfeed_graal_cpp_api/api.hpp>

#include <filesystem>

auto cApiStateToString(dxfc_dxendpoint_state_t state) {
switch (state) {
case DXFC_DXENDPOINT_STATE_NOT_CONNECTED:
Expand All @@ -22,6 +24,7 @@ auto cApiStateToString(dxfc_dxendpoint_state_t state) {
int main() {
{
using namespace std::string_literals;
using namespace std::string_view_literals;

auto builder = dxfcpp::DXEndpoint::newBuilder()->withRole(dxfcpp::DXEndpoint::Role::FEED);
auto endpoint = builder->build();
Expand All @@ -40,14 +43,18 @@ int main() {
}
});

sub->addSymbol("AAPL"s);
sub->addSymbol("IBM"s);
sub->addSymbol("AAPL");
sub->addSymbol("IBM"sv);
sub->addSymbol("TSLA"s);

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

std::this_thread::sleep_for(std::chrono::seconds(5));

sub->removeSymbol("TSLA");

std::this_thread::sleep_for(std::chrono::seconds(5));

endpoint->close();
}
}
80 changes: 79 additions & 1 deletion src/api/DXFeedSubscription.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,41 @@ void DXFeedSubscription::detach(std::shared_ptr<DXFeed> feed) noexcept {
feed->detachSubscription(shared_from_this());
}

template <> void DXFeedSubscription::addSymbol(std::string &&symbol) noexcept {
#if __cpp_lib_string_view
void DXFeedSubscription::addSymbol(std::string_view 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.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);
}

runIsolatedOrElse(
[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;
},
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()};
Expand All @@ -35,6 +69,50 @@ template <> void DXFeedSubscription::addSymbol(std::string &&symbol) noexcept {
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);
}

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

return dxfg_DXFeedSubscription_removeSymbol(threadHandle, handler, (dxfg_symbol_t *)&s) == 0;
},
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 3ba39ee

Please sign in to comment.