Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DXFC-401] Create entity managers and API context. #6

Merged
merged 10 commits into from
May 19, 2023
5 changes: 5 additions & 0 deletions include/dxfeed_graal_cpp_api/api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@

#pragma once

#include "dxfeed_graal_cpp_api/internal/managers/ErrorHandlingManager.hpp"
#include "internal/CEntryPointErrors.hpp"
#include "internal/Common.hpp"
#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
23 changes: 10 additions & 13 deletions include/dxfeed_graal_cpp_api/api/DXEndpoint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,6 @@ struct DXEndpoint : std::enable_shared_from_this<DXEndpoint> {
static inline std::mutex MTX{};
static std::unordered_map<Role, std::shared_ptr<DXEndpoint>> INSTANCES;

mutable std::recursive_mutex mtx_{};
handler_utils::JavaObjectHandler<DXEndpoint> handler_;
Role role_ = Role::FEED;
std::string name_{};
Expand All @@ -480,7 +479,7 @@ struct DXEndpoint : std::enable_shared_from_this<DXEndpoint> {
void closeImpl();

protected:
DXEndpoint() : mtx_{}, handler_{}, role_{}, feed_{}, publisher_{}, stateChangeListenerHandler_{}, onStateChange_{} {
DXEndpoint() : handler_{}, role_{}, feed_{}, publisher_{}, stateChangeListenerHandler_{}, onStateChange_{} {
if constexpr (isDebug) {
debug("DXEndpoint()");
}
Expand All @@ -491,8 +490,6 @@ struct DXEndpoint : std::enable_shared_from_this<DXEndpoint> {
if constexpr (isDebug) {
debug("DXEndpoint{{{}}}::~DXEndpoint()", handler_.toString());
}

tryCallWithLock(mtx_, [this] { closeImpl(); });
}

/**
Expand Down Expand Up @@ -619,7 +616,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 @@ -633,15 +630,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 @@ -753,8 +750,6 @@ struct DXEndpoint : std::enable_shared_from_this<DXEndpoint> {
debug("DXEndpoint{{{}}}::close()", handler_.toString());
}

std::lock_guard guard(mtx_);

closeImpl();
}

Expand Down Expand Up @@ -810,12 +805,12 @@ 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_;

Builder() : mtx_{}, handler_{}, properties_{} {
Builder() : handler_{}, properties_{} {
if constexpr (isDebug) {
debug("DXEndpoint::Builder::Builder()");
}
Expand Down Expand Up @@ -899,8 +894,6 @@ struct DXEndpoint : std::enable_shared_from_this<DXEndpoint> {
properties.size());
}

std::lock_guard guard(mtx_);

for (auto &&[k, v] : properties) {
withProperty(k, v);
}
Expand Down Expand Up @@ -939,5 +932,9 @@ struct DXEndpoint : std::enable_shared_from_this<DXEndpoint> {

return Builder::create();
}

std::string toString() const {
return fmt::format("DXEndpoint{{{}}}", handler_.toString());
}
};
} // namespace dxfcpp
5 changes: 1 addition & 4 deletions include/dxfeed_graal_cpp_api/api/DXFeed.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,14 @@ struct DXFeed : std::enable_shared_from_this<DXFeed> {
friend struct DXEndpoint;

private:
mutable std::recursive_mutex mtx_{};
handler_utils::JavaObjectHandler<DXFeed> handler_;

std::unordered_set<std::shared_ptr<DXFeedSubscription>> subscriptions_{};

static std::shared_ptr<DXFeed> create(void *feedHandle) noexcept;

protected:
DXFeed() noexcept : mtx_(), handler_{} {
DXFeed() noexcept : handler_{} {
if constexpr (isDebug) {
debug("DXFeed()");
}
Expand Down Expand Up @@ -98,8 +97,6 @@ struct DXFeed : std::enable_shared_from_this<DXFeed> {
}

std::string toString() const {
std::lock_guard guard(mtx_);

return fmt::format("DXFeed{{{}}}", handler_.toString());
}
};
Expand Down
52 changes: 37 additions & 15 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,11 +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;
void removeEventListener(std::size_t listenerId) noexcept { onEvent_ -= listenerId; }

auto onEvent() noexcept;
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: 4 additions & 4 deletions include/dxfeed_graal_cpp_api/event/IndexedEventSource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,28 @@ class IndexedEventSource {
* @param id The source id
* @param name The source name
*/
IndexedEventSource(unsigned id, std::string name) : id_{id}, name_{std::move(name)} {}
IndexedEventSource(unsigned id, std::string name) noexcept : id_{id}, name_{std::move(name)} {}

/**
* Returns the source identifier. Source identifier is non-negative.
*
* @return The source identifier.
*/
std::uint32_t id() const { return id_; }
std::uint32_t id() const noexcept { return id_; }

/**
* Returns the string representation of the object.
*
* @return The string representation of the object.
*/
const std::string &name() const { return name_; }
const std::string &name() const noexcept { return name_; }

/**
* Returns the string representation of the object.
*
* @return The string representation of the object.
*/
std::string toString() const { return name_; }
std::string toString() const noexcept { return name_; }
};

} // namespace dxfcpp
24 changes: 17 additions & 7 deletions include/dxfeed_graal_cpp_api/event/market/OrderSource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,38 @@
#include <cstdint>
#include <memory>
#include <string>
#include <unordered_map>

#include "../../internal/Common.hpp"
#include "../IndexedEventSource.hpp"

namespace dxfcpp {

// TODO: implement
template <template <typename...> typename Cache>
class OrderSourceImpl final : public IndexedEventSource {

static inline std::mutex MTX{};
static inline std::size_t CACHE_SIZE = 100UL;
static inline Cache<std::uint32_t, std::shared_ptr<OrderSourceImpl<Cache>>> SOURCES_BY_ID{};
static inline Cache<std::string, std::shared_ptr<OrderSourceImpl<Cache>>> SOURCES_BY_STRING{};
class OrderSource final : public IndexedEventSource {

static constexpr std::uint32_t PUB_ORDER = 0x0001U;
static constexpr std::uint32_t PUB_ANALYTIC_ORDER = 0x0002U;
static constexpr std::uint32_t PUB_SPREAD_ORDER = 0x0004U;
static constexpr std::uint32_t FULL_ORDER_BOOK = 0x0008U;
static constexpr std::uint32_t FLAGS_SIZE = 4U;

static const std::unordered_map<std::uint32_t, std::reference_wrapper<const OrderSource>> INTERNAL_;

static inline std::mutex MTX_{};
const std::unordered_map<std::uint32_t, std::reference_wrapper<const OrderSource>> USER_;

std::uint32_t pubFlags_{};
bool builtin_{};

OrderSource(std::uint32_t id, std::string name, std::uint32_t pubFlags) noexcept
: IndexedEventSource(id, std::move(name)), pubFlags_{pubFlags}, builtin_{true} {}

OrderSource(const std::string& name, std::uint32_t pubFlags): OrderSource(composeId(name), name, pubFlags) {}

static std::uint32_t composeId(const std::string& name) noexcept {
return {};
}
};

} // namespace dxfcpp
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
Loading