Skip to content

Commit

Permalink
[MDAPI-82][C++] Implement MarketDepthModel
Browse files Browse the repository at this point in the history
  • Loading branch information
AnatolyKalin committed Nov 27, 2024
1 parent 53c7798 commit d541b1f
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 1 deletion.
34 changes: 34 additions & 0 deletions include/dxfeed_graal_cpp_api/model/IndexedTxModel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ struct DXFCPP_EXPORT IndexedTxModel : RequireMakeShared<IndexedTxModel> {
* builder->withListener(listener);
* ```
*
* @tparam E The type of event (derived from IndexedEvent)
* @param listener The transaction listener.
* @return `this` builder.
*/
Expand All @@ -191,6 +192,39 @@ struct DXFCPP_EXPORT IndexedTxModel : RequireMakeShared<IndexedTxModel> {
listener->template sharedAs<TxModelListenerCommon>());
}

/**
* Sets the listener for transaction notifications.
* The listener cannot be changed or added once the model has been built.
*
* ```cpp
* auto builder = IndexedTxModel::newBuilder(Order::TYPE);
*
* builder = builder->withListener<Order>([](const auto &, const auto &events, bool isSnapshot) {
* if (isSnapshot) {
* std::cout << "Snapshot:" << std::endl;
* } else {
* std::cout << "Update:" << std::endl;
* }
*
* for (const auto &e : events) {
* std::cout << "[" << e->getEventFlagsMask().toString() << "]:" << e << std::endl;
* }
*
* std::cout << std::endl;
* });
* ```
*
* @tparam E The type of event (derived from IndexedEvent)
* @param onEventsReceived A functional object, lambda, or function to which indexed event data will be passed.
* @return `this` builder.
*/
template <Derived<IndexedEvent> E>
std::shared_ptr<Builder> withListener(std::function<void(const IndexedEventSource & /* source */,
const std::vector<std::shared_ptr<E>> & /* events */, bool /* isSnapshot */)>
onEventsReceived) const {
return withListener(TxModelListener<E>::create(onEventsReceived));
}

/**
* Sets the sources from which to subscribe for indexed events.
* If no sources have been set, subscriptions will default to all possible sources.
Expand Down
12 changes: 11 additions & 1 deletion include/dxfeed_graal_cpp_api/model/MarketDepthModel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,17 @@ struct DXFCPP_EXPORT MarketDepthModel : RequireMakeShared<MarketDepthModel> {

template <Derived<OrderBase> E>
std::shared_ptr<Builder> withListener(std::shared_ptr<MarketDepthModelListener<E>> listener) {
listener = listener->template sharedAs<MarketDepthModelListenerCommon>();
//listener = listener->template sharedAs<MarketDepthModelListenerCommon>();
this->listener = listener;

return sharedAs<Builder>();
}

template <Derived<OrderBase> E>
std::shared_ptr<Builder> withListener(std::function<void(const std::vector<std::shared_ptr<E>> & /* buy */,
const std::vector<std::shared_ptr<E>> & /* sell */)>
onEventsReceived) {


return sharedAs<Builder>();
}
Expand Down
34 changes: 34 additions & 0 deletions include/dxfeed_graal_cpp_api/model/TimeSeriesTxModel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ struct DXFCPP_EXPORT TimeSeriesTxModel : RequireMakeShared<TimeSeriesTxModel> {
* builder->withListener(listener);
* ```
*
* @tparam E The type of event (derived from TimeSeriesEvent)
* @param listener The transaction listener.
* @return `this` builder.
*/
Expand All @@ -189,6 +190,39 @@ struct DXFCPP_EXPORT TimeSeriesTxModel : RequireMakeShared<TimeSeriesTxModel> {
listener->template sharedAs<TxModelListenerCommon>());
}

/**
* Sets the listener for transaction notifications.
* The listener cannot be changed or added once the model has been built.
*
* ```cpp
* auto builder = TimeSeriesTxModel::newBuilder(Candle::TYPE);
*
* builder = builder->withListener<Candle>([](const auto &, const auto &events, bool isSnapshot) {
* if (isSnapshot) {
* std::cout << "Snapshot:" << std::endl;
* } else {
* std::cout << "Update:" << std::endl;
* }
*
* for (const auto &e : events) {
* std::cout << "[" << e->getEventFlagsMask().toString() << "]:" << e << std::endl;
* }
*
* std::cout << std::endl;
* });
* ```
*
* @tparam E The type of event (derived from TimeSeriesEvent)
* @param onEventsReceived A functional object, lambda, or function to which time series event data will be passed.
* @return `this` builder.
*/
template <Derived<TimeSeriesEvent> E>
std::shared_ptr<Builder> withListener(std::function<void(const IndexedEventSource & /* source */,
const std::vector<std::shared_ptr<E>> & /* events */, bool /* isSnapshot */)>
onEventsReceived) const {
return withListener(TxModelListener<E>::create(onEventsReceived));
}

/**
* Sets the time from which to subscribe for time-series.
*
Expand Down
8 changes: 8 additions & 0 deletions src/model/MarketDepthModel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright (c) 2024 Devexperts LLC.
// SPDX-License-Identifier: MPL-2.0

#include "dxfeed_graal_cpp_api/isolated/IsolatedCommon.hpp"

DXFCPP_BEGIN_NAMESPACE

DXFCPP_END_NAMESPACE
8 changes: 8 additions & 0 deletions src/model/MarketDepthModelListener.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright (c) 2024 Devexperts LLC.
// SPDX-License-Identifier: MPL-2.0

#include "dxfeed_graal_cpp_api/isolated/IsolatedCommon.hpp"

DXFCPP_BEGIN_NAMESPACE

DXFCPP_END_NAMESPACE

0 comments on commit d541b1f

Please sign in to comment.