Skip to content

Commit

Permalink
[DXFC-401] Create entity managers and API context.
Browse files Browse the repository at this point in the history
  • Loading branch information
AnatolyKalin committed May 18, 2023
1 parent 1f55464 commit 29420cc
Show file tree
Hide file tree
Showing 22 changed files with 248 additions and 269 deletions.
2 changes: 2 additions & 0 deletions include/dxfeed_graal_cpp_api/api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include "internal/Enum.hpp"
#include "internal/context/ApiContext.hpp"
#include "internal/managers/DXEndpointManager.hpp"
#include "internal/managers/DXFeedSubscriptionManager.hpp"
#include "internal/managers/EntityManager.hpp"

#include "api/DXEndpoint.hpp"
#include "api/DXFeed.hpp"
Expand Down
13 changes: 7 additions & 6 deletions include/dxfeed_graal_cpp_api/api/DXEndpoint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -491,9 +491,6 @@ struct DXEndpoint : std::enable_shared_from_this<DXEndpoint> {
if constexpr (isDebug) {
debug("DXEndpoint{{{}}}::~DXEndpoint()", handler_.toString());
}

//TODO: check
tryCallWithLock(mtx_, [this] { closeImpl(); });
}

/**
Expand Down Expand Up @@ -620,7 +617,7 @@ struct DXEndpoint : std::enable_shared_from_this<DXEndpoint> {
* @return the listener id
*/
template <typename StateChangeListener>
std::size_t addStateChangeListener(StateChangeListener &&listener)
std::size_t addStateChangeListener(StateChangeListener &&listener) noexcept
requires requires {
{ listener(State{}, State{}) } -> std::same_as<void>;
}
Expand All @@ -634,15 +631,15 @@ struct DXEndpoint : std::enable_shared_from_this<DXEndpoint> {
*
* @param listenerId The listener id to remove
*/
void removeStateChangeListener(std::size_t listenerId) { onStateChange_ -= listenerId; }
void removeStateChangeListener(std::size_t listenerId) noexcept { onStateChange_ -= listenerId; }

/**
* Returns the onStateChange @ref Handler<void(ArgTypes...)> "handler" that can be used to add or remove
* listeners.
*
* @return onStateChange handler with `void(State, State)` signature
*/
auto &onStateChange() { return onStateChange_; }
auto &onStateChange() noexcept { return onStateChange_; }

// TODO: implement
template <typename Executor> void executor(Executor &&) {}
Expand Down Expand Up @@ -940,5 +937,9 @@ struct DXEndpoint : std::enable_shared_from_this<DXEndpoint> {

return Builder::create();
}

std::string toString() const {
return fmt::format("DXEndpoint{{{}}}", handler_.toString());
}
};
} // namespace dxfcpp
54 changes: 35 additions & 19 deletions include/dxfeed_graal_cpp_api/api/DXFeedSubscription.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class DXFeedSubscription : public std::enable_shared_from_this<DXFeedSubscriptio
static handler_utils::JavaObjectHandler<DXFeedSubscription>
createSubscriptionHandlerFromEventClassList(const std::unique_ptr<handler_utils::EventClassList> &list) noexcept;

void setEventListenerHandler() noexcept;
void setEventListenerHandler(Id<DXFeedSubscription> id) noexcept;

template <typename EventTypeIt>
DXFeedSubscription(EventTypeIt begin, EventTypeIt end) noexcept
Expand Down Expand Up @@ -55,7 +55,6 @@ class DXFeedSubscription : public std::enable_shared_from_this<DXFeedSubscriptio
}

handler_ = createSubscriptionHandlerFromEventClassList(list);
setEventListenerHandler();
}

DXFeedSubscription(std::initializer_list<EventTypeEnum> eventTypes) noexcept
Expand All @@ -74,11 +73,7 @@ class DXFeedSubscription : public std::enable_shared_from_this<DXFeedSubscriptio
bool isClosedImpl() noexcept;

public:
std::string toString() const {
std::lock_guard lock(mtx_);

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

virtual ~DXFeedSubscription() {
if constexpr (isDebug) {
Expand All @@ -98,7 +93,12 @@ class DXFeedSubscription : public std::enable_shared_from_this<DXFeedSubscriptio
debug("DXFeedSubscription::create(eventType = {})", eventType.getName());
}

return std::shared_ptr<DXFeedSubscription>(new DXFeedSubscription(eventType));
auto sub = std::shared_ptr<DXFeedSubscription>(new DXFeedSubscription(eventType));
auto id = ApiContext::getInstance()->getDxFeedSubscriptionManager()->registerEntity(sub);

sub->setEventListenerHandler(id);

return sub;
}

/**
Expand All @@ -115,7 +115,12 @@ class DXFeedSubscription : public std::enable_shared_from_this<DXFeedSubscriptio
debug("DXFeedSubscription::create(eventTypes = {})", namesToString(begin, end));
}

return std::shared_ptr<DXFeedSubscription>(new DXFeedSubscription(begin, end));
auto sub = std::shared_ptr<DXFeedSubscription>(new DXFeedSubscription(begin, end));
auto id = ApiContext::getInstance()->getDxFeedSubscriptionManager()->registerEntity(sub);

sub->setEventListenerHandler(id);

return sub;
}

/**
Expand All @@ -125,7 +130,12 @@ class DXFeedSubscription : public std::enable_shared_from_this<DXFeedSubscriptio
* @return The new <i>detached</i> subscription for the given collection of event types.
*/
static std::shared_ptr<DXFeedSubscription> create(std::initializer_list<EventTypeEnum> eventTypes) noexcept {
return std::shared_ptr<DXFeedSubscription>(new DXFeedSubscription(eventTypes));
auto sub = std::shared_ptr<DXFeedSubscription>(new DXFeedSubscription(eventTypes));
auto id = ApiContext::getInstance()->getDxFeedSubscriptionManager()->registerEntity(sub);

sub->setEventListenerHandler(id);

return sub;
}

/**
Expand All @@ -139,8 +149,13 @@ class DXFeedSubscription : public std::enable_shared_from_this<DXFeedSubscriptio
static std::shared_ptr<DXFeedSubscription> create(EventTypesCollection &&eventTypes) noexcept
requires requires { ElementTypeIs<EventTypesCollection, EventTypeEnum>; }
{
return std::shared_ptr<DXFeedSubscription>(
new DXFeedSubscription(std::forward<EventTypesCollection>(eventTypes)));
auto sub =
std::shared_ptr<DXFeedSubscription>(new DXFeedSubscription(std::forward<EventTypesCollection>(eventTypes)));
auto id = ApiContext::getInstance()->getDxFeedSubscriptionManager()->registerEntity(sub);

sub->setEventListenerHandler(id);

return sub;
}

/**
Expand Down Expand Up @@ -174,17 +189,18 @@ class DXFeedSubscription : public std::enable_shared_from_this<DXFeedSubscriptio
closeImpl();
}

template <typename EventListener> std::size_t addEventListener(EventListener &&listener) noexcept {
template <typename EventListener>
std::size_t addEventListener(EventListener &&listener) noexcept
requires requires {
{ listener(std::vector<std::shared_ptr<EventType>>{}) } -> std::same_as<void>;
}
{
return onEvent_ += listener;
}

void removeEventListener(std::size_t listenerId) noexcept {
onEvent_ -= listenerId;
}
void removeEventListener(std::size_t listenerId) noexcept { onEvent_ -= listenerId; }

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

template <typename Symbol> void addSymbol(Symbol &&symbol) noexcept;

Expand Down
20 changes: 20 additions & 0 deletions include/dxfeed_graal_cpp_api/event/EventType.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,26 @@ struct EventType : public SharedEntity {
virtual void setEventTime(std::int64_t eventTime){
// The default implementation is empty
};

/**
* Returns a string representation of the current object.
*
* @return a string representation
*/
virtual std::string toString() const noexcept { return "EventType{}"; }

friend std::ostream &operator<<(std::ostream &os, const EventType &e) { return os << e.toString(); }

friend std::ostream &operator<<(std::ostream &os, const std::shared_ptr<EventType> &e) {
return os << e->toString();
}

template <typename EntityType>
friend std::ostream &operator<<(std::ostream &os, const std::shared_ptr<EntityType> &e)
requires(std::is_base_of_v<EventType, EntityType>)
{
return os << e->toString();
}
};

/**
Expand Down
8 changes: 1 addition & 7 deletions include/dxfeed_graal_cpp_api/event/market/Profile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ class Profile final : public MarketEvent, public LastingEvent {
*
* @return a string representation
*/
std::string toString() const noexcept {
std::string toString() const noexcept override {
return fmt::format("Profile{{{}, eventTime={}, description='{}', SSR={}, status={}, statusReason='{}', "
"haltStartTime={}, haltEndTime={}, highLimitPrice={}, lowLimitPrice={}, high52WeekPrice={}, "
"low52WeekPrice={}, beta={}, earningsPerShare={}, dividendFrequency={}, "
Expand All @@ -361,12 +361,6 @@ class Profile final : public MarketEvent, public LastingEvent {
getBeta(), getEarningsPerShare(), getDividendFrequency(), getExDividendAmount(),
day_util::getYearMonthDayByDayId(getExDividendDayId()), getShares(), getFreeFloat());
}

template <typename OStream> friend OStream &operator<<(OStream &os, const Profile &e) { return os << e.toString(); }

template <typename OStream> friend OStream &operator<<(OStream &os, const std::shared_ptr<Profile> &e) {
return os << e->toString();
}
};

} // namespace dxfcpp
8 changes: 1 addition & 7 deletions include/dxfeed_graal_cpp_api/event/market/Quote.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ class Quote final : public MarketEvent, public LastingEvent {
*
* @return a string representation
*/
std::string toString() const noexcept {
std::string toString() const noexcept override {
return fmt::format(
"Quote{{{}, eventTime={}, time={}, timeNanoPart={}, sequence={}, bidTime={}, bidExchange={}, bidPrice={}, "
"bidSize={}, askTime={}, askExchange={}, askPrice={}, askSize={}}}",
Expand All @@ -294,12 +294,6 @@ class Quote final : public MarketEvent, public LastingEvent {
string_util::encodeChar(getBidExchangeCode()), getBidPrice(), getBidSize(), formatTimeStamp(getAskTime()),
string_util::encodeChar(getAskExchangeCode()), getAskPrice(), getAskSize());
}

template <typename OStream> friend OStream &operator<<(OStream &os, const Quote &e) { return os << e.toString(); }

template <typename OStream> friend OStream &operator<<(OStream &os, const std::shared_ptr<Quote> &e) {
return os << e->toString();
}
};

} // namespace dxfcpp
8 changes: 1 addition & 7 deletions include/dxfeed_graal_cpp_api/event/market/Summary.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ class Summary final : public MarketEvent, public LastingEvent {
*
* @return a string representation
*/
std::string toString() const noexcept {
std::string toString() const noexcept override {
return fmt::format("Summary{{{}, eventTime={}, day={}, dayOpen={}, dayHigh={}, dayLow='{}', "
"dayClose={}, dayCloseType={}, prevDay={}, prevDayClose={}, prevDayCloseType={}, "
"prevDayVolume={}, openInterest={}}}",
Expand All @@ -254,12 +254,6 @@ class Summary final : public MarketEvent, public LastingEvent {
day_util::getYearMonthDayByDayId(getPrevDayId()), getPrevDayClosePrice(),
getPrevDayClosePriceType().toString(), getPrevDayVolume(), getOpenInterest());
}

template <typename OStream> friend OStream &operator<<(OStream &os, const Summary &e) { return os << e.toString(); }

template <typename OStream> friend OStream &operator<<(OStream &os, const std::shared_ptr<Summary> &e) {
return os << e->toString();
}
};

} // namespace dxfcpp
10 changes: 1 addition & 9 deletions include/dxfeed_graal_cpp_api/event/market/TimeAndSale.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ class TimeAndSale final : public MarketEvent, public TimeSeriesEvent {
*
* @return a string representation
*/
std::string toString() const noexcept {
std::string toString() const noexcept override {
return fmt::format("TimeAndSale{{{}, eventTime={}, eventFlags={:#x}, time={}, timeNanoPart={}, sequence={}, "
"exchange={}, price={}, size={}, bid={}, "
"ask={}, ESC='{}', TTE={}, side={}, spread={}, ETH={}, validTick={}, type={}{}{}}}",
Expand All @@ -481,14 +481,6 @@ class TimeAndSale final : public MarketEvent, public TimeSeriesEvent {
getBuyer().empty() ? std::string{} : fmt::format(", buyer='{}'", getBuyer()),
getSeller().empty() ? std::string{} : fmt::format(", seller='{}'", getSeller()));
}

template <typename OStream> friend OStream &operator<<(OStream &os, const TimeAndSale &e) {
return os << e.toString();
}

template <typename OStream> friend OStream &operator<<(OStream &os, const std::shared_ptr<TimeAndSale> &e) {
return os << e->toString();
}
};

} // namespace dxfcpp
8 changes: 1 addition & 7 deletions include/dxfeed_graal_cpp_api/event/market/Trade.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,7 @@ class Trade final : public TradeBase {
*
* @return a string representation
*/
std::string toString() const noexcept { return fmt::format("Trade{{{}}}", baseFieldsToString()); }

template <typename OStream> friend OStream &operator<<(OStream &os, const Trade &e) { return os << e.toString(); }

template <typename OStream> friend OStream &operator<<(OStream &os, const std::shared_ptr<Trade> &e) {
return os << e->toString();
}
std::string toString() const noexcept override { return fmt::format("Trade{{{}}}", baseFieldsToString()); }
};

} // namespace dxfcpp
10 changes: 1 addition & 9 deletions include/dxfeed_graal_cpp_api/event/market/TradeETH.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,7 @@ class TradeETH final : public TradeBase {
*
* @return a string representation
*/
std::string toString() const noexcept { return fmt::format("TradeETH{{{}}}", baseFieldsToString()); }

template <typename OStream> friend OStream &operator<<(OStream &os, const TradeETH &e) {
return os << e.toString();
}

template <typename OStream> friend OStream &operator<<(OStream &os, const std::shared_ptr<TradeETH> &e) {
return os << e->toString();
}
std::string toString() const noexcept override { return fmt::format("TradeETH{{{}}}", baseFieldsToString()); }
};

} // namespace dxfcpp
8 changes: 1 addition & 7 deletions include/dxfeed_graal_cpp_api/event/option/Greeks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,20 +271,14 @@ class Greeks final : public MarketEvent, public TimeSeriesEvent, public LastingE
*
* @return a string representation
*/
std::string toString() const noexcept {
std::string toString() const noexcept override {
return fmt::format(
"Greeks{{{}, eventTime={}, eventFlags={:#x}, time={}, sequence={}, price={}, volatility={}, delta={}, "
"gamma={}, theta={}, rho={}, vega={}}}",
MarketEvent::getEventSymbol(), formatTimeStampWithMillis(MarketEvent::getEventTime()),
getEventFlags().getMask(), formatTimeStampWithMillis(getTime()), getSequence(), getPrice(), getVolatility(),
getDelta(), getGamma(), getTheta(), getRho(), getVega());
}

template <typename OStream> friend OStream &operator<<(OStream &os, const Greeks &e) { return os << e.toString(); }

template <typename OStream> friend OStream &operator<<(OStream &os, const std::shared_ptr<Greeks> &e) {
return os << e->toString();
}
};

} // namespace dxfcpp
8 changes: 1 addition & 7 deletions include/dxfeed_graal_cpp_api/event/option/Series.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ class Series final : public MarketEvent, public IndexedEvent {
*
* @return a string representation
*/
std::string toString() const noexcept {
std::string toString() const noexcept override {
return fmt::format(
"Series{{{}, eventTime={}, eventFlags={:#x}, index={:#x}, time={}, sequence={}, expiration={}, "
"volatility={}, callVolume={}, putVolume={}, putCallRatio={}, forwardPrice={}, dividend={}, interest={}}}",
Expand All @@ -313,12 +313,6 @@ class Series final : public MarketEvent, public IndexedEvent {
day_util::getYearMonthDayByDayId(getExpiration()), getVolatility(), getCallVolume(), getPutVolume(),
getPutCallRatio(), getForwardPrice(), getDividend(), getInterest());
}

template <typename OStream> friend OStream &operator<<(OStream &os, const Series &e) { return os << e.toString(); }

template <typename OStream> friend OStream &operator<<(OStream &os, const std::shared_ptr<Series> &e) {
return os << e->toString();
}
};

} // namespace dxfcpp
10 changes: 1 addition & 9 deletions include/dxfeed_graal_cpp_api/event/option/TheoPrice.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,22 +237,14 @@ class TheoPrice final : public MarketEvent, public TimeSeriesEvent, public Lasti
*
* @return a string representation
*/
std::string toString() const noexcept {
std::string toString() const noexcept override {
return fmt::format(
"TheoPrice{{{}, eventTime={}, eventFlags={:#x}, time={}, sequence={}, price={}, underlyingPrice={}, "
"delta={}, gamma={}, dividend={}, interest={}}}",
MarketEvent::getEventSymbol(), formatTimeStampWithMillis(MarketEvent::getEventTime()),
getEventFlags().getMask(), formatTimeStampWithMillis(getTime()), getSequence(), getPrice(),
getUnderlyingPrice(), getDelta(), getGamma(), getDividend(), getInterest());
}

template <typename OStream> friend OStream &operator<<(OStream &os, const TheoPrice &e) {
return os << e.toString();
}

template <typename OStream> friend OStream &operator<<(OStream &os, const std::shared_ptr<TheoPrice> &e) {
return os << e->toString();
}
};

} // namespace dxfcpp
Loading

0 comments on commit 29420cc

Please sign in to comment.