Skip to content

Commit

Permalink
[MDAPI-79] [C++] Retrieve promise-based events from feed
Browse files Browse the repository at this point in the history
DXFeed::getLastEventsPromises
  • Loading branch information
ttldtor committed Oct 18, 2024
1 parent 1fa9bd0 commit 22e9a88
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 27 deletions.
19 changes: 19 additions & 0 deletions include/dxfeed_graal_cpp_api/api/DXFeed.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ struct DXFCPP_EXPORT DXFeed : SharedEntity {

void *getLastEventPromiseImpl(const EventTypeEnum &eventType, const SymbolWrapper &symbol) const;

void *getLastEventsPromisesImpl(const EventTypeEnum &eventType, void *graalSymbolList) const;

void *getIndexedEventsPromiseImpl(const EventTypeEnum &eventType, const SymbolWrapper &symbol,
const IndexedEventSource &source) const;

Expand Down Expand Up @@ -428,6 +430,23 @@ struct DXFCPP_EXPORT DXFeed : SharedEntity {
return std::make_shared<Promise<std::shared_ptr<E>>>(getLastEventPromiseImpl(E::TYPE, symbol));
}

template <Derived<LastingEvent> E, typename SymbolIt>
std::shared_ptr<PromiseList<E>> getLastEventsPromises(SymbolIt begin, SymbolIt end) const {
auto list = SymbolWrapper::SymbolListUtils::toGraalListUnique(begin, end);

return std::make_shared<std::shared_ptr<PromiseList<E>>>(getLastEventsPromisesImpl(E::TYPE, list.get()));
}

template <Derived<LastingEvent> E, ConvertibleToSymbolWrapperCollection SymbolsCollection>
std::shared_ptr<PromiseList<E>> getLastEventsPromises(SymbolsCollection &&collection) const {
return getLastEventsPromises<E>(std::begin(collection), std::end(collection));
}

template <Derived<LastingEvent> E>
std::shared_ptr<PromiseList<E>> getLastEventsPromises(std::initializer_list<SymbolWrapper> collection) const {
return getLastEventsPromises<E>(collection.begin(), collection.end());
}

/**
* Requests a container of indexed events for the specified event type, symbol, and source.
* This method works only for event types that implement IndexedEvent "interface".
Expand Down
15 changes: 6 additions & 9 deletions include/dxfeed_graal_cpp_api/api/DXFeedSubscription.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,10 +326,9 @@ class DXFCPP_EXPORT DXFeedSubscription : public RequireMakeShared<DXFeedSubscrip
Debugger::debug(toString() + "::setSymbols(symbols = " + elementsToString(begin, end) + ")");
}

auto *list = SymbolWrapper::SymbolListUtils::toGraalList(begin, end);
auto list = SymbolWrapper::SymbolListUtils::toGraalListUnique(begin, end);

setSymbolsImpl(list);
SymbolWrapper::SymbolListUtils::freeGraalList(list);
setSymbolsImpl(list.get());
}

/**
Expand Down Expand Up @@ -402,10 +401,9 @@ class DXFCPP_EXPORT DXFeedSubscription : public RequireMakeShared<DXFeedSubscrip
Debugger::debug(toString() + "::addSymbols(symbols = " + elementsToString(begin, end) + ")");
}

auto *list = SymbolWrapper::SymbolListUtils::toGraalList(begin, end);
auto list = SymbolWrapper::SymbolListUtils::toGraalListUnique(begin, end);

addSymbolsImpl(list);
SymbolWrapper::SymbolListUtils::freeGraalList(list);
addSymbolsImpl(list.get());
}

/**
Expand Down Expand Up @@ -475,10 +473,9 @@ class DXFCPP_EXPORT DXFeedSubscription : public RequireMakeShared<DXFeedSubscrip
Debugger::debug(toString() + "::removeSymbols(symbols = " + elementsToString(begin, end) + ")");
}

auto *list = SymbolWrapper::SymbolListUtils::toGraalList(begin, end);
auto list = SymbolWrapper::SymbolListUtils::toGraalListUnique(begin, end);

removeSymbolsImpl(list);
SymbolWrapper::SymbolListUtils::freeGraalList(list);
removeSymbolsImpl(list.get());
}

/**
Expand Down
5 changes: 2 additions & 3 deletions include/dxfeed_graal_cpp_api/api/DXPublisher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,9 @@ struct DXFCPP_EXPORT DXPublisher : SharedEntity {
Debugger::debug(toString() + "::publishEvents(events = " + elementsToString(begin, end) + ")");
}

auto *list = EventMapper::toGraalList(begin, end);
auto list = EventMapper::toGraalListUnique(begin, end);

publishEventsImpl(list);
EventMapper::freeGraalList(list);
publishEventsImpl(list.get());
}

/**
Expand Down
4 changes: 4 additions & 0 deletions include/dxfeed_graal_cpp_api/event/EventMapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ struct DXFCPP_EXPORT EventMapper {

static void freeGraalList(void *graalList);

template <typename EventIt> static std::unique_ptr<void, decltype(&freeGraalList)> toGraalListUnique(EventIt begin, EventIt end) {
return {toGraalList(begin, end), freeGraalList};
}

private:
static std::ptrdiff_t calculateGraalListSize(std::ptrdiff_t initSize) noexcept;
static void *newGraalList(std::ptrdiff_t size);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ int32_t dxfg_DXFeed_getLastEvents(graal_isolatethread_
void* getLastEventPromise(const JavaObjectHandle<DXFeed>& feed, const EventTypeEnum &eventType, const SymbolWrapper &symbol);

//dxfg_promise_list* dxfg_DXFeed_getLastEventsPromises(graal_isolatethread_t *thread, dxfg_feed_t *feed, dxfg_event_clazz_t eventClazz, dxfg_symbol_list *symbols);
void* getLastEventsPromises(const JavaObjectHandle<DXFeed>& feed, const EventTypeEnum &eventType, void *symbols);

//dxfg_promise_events_t* dxfg_DXFeed_getIndexedEventsPromise(graal_isolatethread_t *thread, dxfg_feed_t *feed, dxfg_event_clazz_t eventClazz, dxfg_symbol_t *symbol, dxfg_indexed_event_source_t* source);
void* getIndexedEventsPromise(const JavaObjectHandle<DXFeed>& feed, const EventTypeEnum &eventType, const SymbolWrapper &symbol, const IndexedEventSource& source);
Expand Down
27 changes: 21 additions & 6 deletions include/dxfeed_graal_cpp_api/symbols/SymbolWrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,31 @@ struct DXFCPP_EXPORT SymbolWrapper final {
}

template <ConvertibleToSymbolWrapperCollection SymbolsCollection>
static void *toGraalList(const SymbolsCollection &collection) noexcept {
static void *toGraalList(const SymbolsCollection &collection) {
return SymbolListUtils::toGraalList(std::begin(collection), std::end(collection));
}

static void *toGraalList(std::initializer_list<SymbolWrapper> collection) noexcept {
static void *toGraalList(std::initializer_list<SymbolWrapper> collection) {
return SymbolListUtils::toGraalList(collection.begin(), collection.end());
}

static void freeGraalList(void *graalList);

template <typename SymbolIt>
static std::unique_ptr<void, decltype(&freeGraalList)> toGraalListUnique(SymbolIt begin, SymbolIt end) {
return {toGraalList(begin, end), freeGraalList};
}

template <ConvertibleToSymbolWrapperCollection SymbolsCollection>
static std::unique_ptr<void, decltype(&freeGraalList)> toGraalListUnique(const SymbolsCollection &collection) {
return {toGraalList(collection), freeGraalList};
}

static std::unique_ptr<void, decltype(&freeGraalList)>
toGraalListUnique(std::initializer_list<SymbolWrapper> collection) {
return {toGraalList(collection), freeGraalList};
}

static std::vector<SymbolWrapper> fromGraalList(void *graalList);
};

Expand Down Expand Up @@ -278,10 +293,10 @@ struct DXFCPP_EXPORT SymbolWrapper final {
*/
std::string toStringUnderlying() const {
return std::visit(
[](const auto &symbol) {
return toStringAny(symbol);
},
data_);
[](const auto &symbol) {
return toStringAny(symbol);
},
data_);
}

/**
Expand Down
7 changes: 6 additions & 1 deletion src/api/DXFeed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,12 @@ void *DXFeed::getLastEventPromiseImpl(const EventTypeEnum &eventType, const Symb
return isolated::api::IsolatedDXFeed::getLastEventPromise(handle_, eventType, symbol);
}

void *DXFeed::getIndexedEventsPromiseImpl(const EventTypeEnum &eventType, const SymbolWrapper &symbol, const IndexedEventSource& source) const {
void *DXFeed::getLastEventsPromisesImpl(const EventTypeEnum &eventType, void *graalSymbolList) const {
return isolated::api::IsolatedDXFeed::getLastEventsPromises(handle_, eventType, graalSymbolList);
}

void *DXFeed::getIndexedEventsPromiseImpl(const EventTypeEnum &eventType, const SymbolWrapper &symbol,
const IndexedEventSource &source) const {
return isolated::api::IsolatedDXFeed::getIndexedEventsPromise(handle_, eventType, symbol, source);
}

Expand Down
28 changes: 20 additions & 8 deletions src/isolated/api/IsolatedDXFeed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,27 @@ void *getLastEventPromise(const JavaObjectHandle<DXFeed> &feed, const EventTypeE

auto graalSymbol = symbol.toGraalUnique();

auto result = dxfcpp::bit_cast<void *>(runGraalFunctionAndThrowIfNullptr(
return dxfcpp::bit_cast<void *>(runGraalFunctionAndThrowIfNullptr(
dxfg_DXFeed_getLastEventPromise, static_cast<dxfg_feed_t *>(feed.get()),
static_cast<dxfg_event_clazz_t>(eventType.getId()), static_cast<dxfg_symbol_t *>(graalSymbol.get())));
}

// dxfg_promise_list* dxfg_DXFeed_getLastEventsPromises(graal_isolatethread_t *thread, dxfg_feed_t *feed,
// dxfg_event_clazz_t eventClazz, dxfg_symbol_list *symbols);
void *getLastEventsPromises(const JavaObjectHandle<DXFeed> &feed, const EventTypeEnum &eventType, void *symbols) {
if (!feed) {
throw InvalidArgumentException(
"Unable to execute function `dxfg_DXFeed_getLastEventsPromises`. The `feed` handle is invalid");
}

return result;
if (!symbols) {
throw InvalidArgumentException(
"Unable to execute function `dxfg_DXFeed_getLastEventsPromises`. The `symbols` is nullptr");
}

return runGraalFunctionAndThrowIfNullptr(dxfg_DXFeed_getLastEventsPromises, static_cast<dxfg_feed_t *>(feed.get()),
static_cast<dxfg_event_clazz_t>(eventType.getId()),
static_cast<dxfg_symbol_list *>(symbols));
}

// dxfg_promise_events_t* dxfg_DXFeed_getIndexedEventsPromise(graal_isolatethread_t *thread, dxfg_feed_t
Expand All @@ -93,12 +109,10 @@ void *getIndexedEventsPromise(const JavaObjectHandle<DXFeed> &feed, const EventT
auto graalSymbol = symbol.toGraalUnique();
auto graalSource = source.toGraalUnique();

auto result = dxfcpp::bit_cast<void *>(runGraalFunctionAndThrowIfNullptr(
return dxfcpp::bit_cast<void *>(runGraalFunctionAndThrowIfNullptr(
dxfg_DXFeed_getIndexedEventsPromise, static_cast<dxfg_feed_t *>(feed.get()),
static_cast<dxfg_event_clazz_t>(eventType.getId()), static_cast<dxfg_symbol_t *>(graalSymbol.get()),
static_cast<dxfg_indexed_event_source_t *>(graalSource.get())));

return result;
}

/* dxfg_promise_events_t* */ void *getTimeSeriesPromise(/* dxfg_feed_t * */ const JavaObjectHandle<DXFeed> &feed,
Expand All @@ -112,12 +126,10 @@ void *getIndexedEventsPromise(const JavaObjectHandle<DXFeed> &feed, const EventT

auto graalSymbol = symbol.toGraalUnique();

auto result = dxfcpp::bit_cast<void *>(
return dxfcpp::bit_cast<void *>(
runGraalFunctionAndThrowIfNullptr(dxfg_DXFeed_getTimeSeriesPromise, static_cast<dxfg_feed_t *>(feed.get()),
static_cast<dxfg_event_clazz_t>(eventType.getId()),
static_cast<dxfg_symbol_t *>(graalSymbol.get()), fromTime, toTime));

return result;
}

} // namespace isolated::api::IsolatedDXFeed
Expand Down

0 comments on commit 22e9a88

Please sign in to comment.