Skip to content

Commit

Permalink
[EN-7314] Implement the DXPublisher: Quote
Browse files Browse the repository at this point in the history
  • Loading branch information
AnatolyKalin committed Aug 21, 2023
1 parent e24643a commit 66fc51a
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 51 deletions.
3 changes: 3 additions & 0 deletions include/dxfeed_graal_cpp_api/event/market/Quote.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ class DXFCPP_EXPORT Quote final : public MarketEvent, public LastingEvent {
getSequence());
}

void fillData(void *graalNative) noexcept override;
void fillGraalData(void *graalNative) const noexcept override;

static std::shared_ptr<Quote> fromGraal(void *graalNative) noexcept;
void* toGraal() const noexcept;
static void freeGraal(void* graalNative) noexcept;
Expand Down
118 changes: 67 additions & 51 deletions src/event/market/Quote.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,41 +43,48 @@ void Quote::setAskExchangeCode(std::int16_t askExchangeCode) noexcept {
data_.askExchangeCode = askExchangeCode;
}

std::shared_ptr<Quote> Quote::fromGraal(void *graalNative) noexcept {
if (!graalNative) {
return {};
void Quote::fillData(void *graalNative) noexcept {
if (graalNative == nullptr) {
return;
}

auto eventType = dxfcpp::bit_cast<dxfg_event_type_t *>(graalNative);
MarketEvent::fillData(graalNative);

auto graalQuote = static_cast<dxfg_quote_t *>(graalNative);

data_ = {
.timeMillisSequence = graalQuote->time_millis_sequence,
.timeNanoPart = graalQuote->time_nano_part,
.bidTime = graalQuote->bid_time,
.bidExchangeCode = graalQuote->bid_exchange_code,
.bidPrice = graalQuote->bid_price,
.bidSize = graalQuote->bid_size,
.askTime = graalQuote->ask_time,
.askExchangeCode = graalQuote->ask_exchange_code,
.askPrice = graalQuote->ask_price,
.askSize = graalQuote->ask_size,
};
}

if (eventType->clazz != dxfg_event_clazz_t::DXFG_EVENT_QUOTE) {
return {};
void Quote::fillGraalData(void *graalNative) const noexcept {
if (graalNative == nullptr) {
return;
}

try {
auto graalQuote = dxfcpp::bit_cast<dxfg_quote_t *>(graalNative);
auto quote = std::make_shared<Quote>(dxfcpp::toString(graalQuote->market_event.event_symbol));

quote->setEventTime(graalQuote->market_event.event_time);

quote->data_ = {
.timeMillisSequence = graalQuote->time_millis_sequence,
.timeNanoPart = graalQuote->time_nano_part,
.bidTime = graalQuote->bid_time,
.bidExchangeCode = graalQuote->bid_exchange_code,
.bidPrice = graalQuote->bid_price,
.bidSize = graalQuote->bid_size,
.askTime = graalQuote->ask_time,
.askExchangeCode = graalQuote->ask_exchange_code,
.askPrice = graalQuote->ask_price,
.askSize = graalQuote->ask_size,
};
MarketEvent::fillGraalData(graalNative);

return quote;
} catch (...) {
// TODO: error handling
return {};
}
auto graalQuote = static_cast<dxfg_quote_t *>(graalNative);

graalQuote->time_millis_sequence = data_.timeMillisSequence;
graalQuote->time_nano_part = data_.timeNanoPart;
graalQuote->bid_time = data_.bidTime;
graalQuote->bid_exchange_code = data_.bidExchangeCode;
graalQuote->bid_price = data_.bidPrice;
graalQuote->bid_size = data_.bidSize;
graalQuote->ask_time = data_.askTime;
graalQuote->ask_exchange_code = data_.askExchangeCode;
graalQuote->ask_price = data_.askPrice;
graalQuote->ask_size = data_.askSize;
}

std::string Quote::toString() const noexcept {
Expand All @@ -91,33 +98,43 @@ std::string Quote::toString() const noexcept {
dxfcpp::toString(getAskSize()));
}

std::shared_ptr<Quote> Quote::fromGraal(void *graalNative) noexcept {
if (!graalNative) {
return {};
}

auto eventType = static_cast<dxfg_event_type_t *>(graalNative);

if (eventType->clazz != dxfg_event_clazz_t::DXFG_EVENT_QUOTE) {
return {};
}

try {
auto quote = std::make_shared<Quote>();

quote->fillData(graalNative);

return quote;
} catch (...) {
// TODO: error handling
return {};
}
}

void *Quote::toGraal() const noexcept {
if constexpr (Debugger::isDebug) {
Debugger::debug(toString() + "::toGraal()");
}

auto *graalQuote = new (std::nothrow) dxfg_quote_t{
.market_event = {.event_type = {.clazz = dxfg_event_clazz_t::DXFG_EVENT_QUOTE},
.event_symbol = createCString(MarketEvent::getEventSymbol()),
.event_time = MarketEvent::getEventTime()},
.time_millis_sequence = data_.timeMillisSequence,
.time_nano_part = data_.timeNanoPart,
.bid_time = data_.bidTime,
.bid_exchange_code = data_.bidExchangeCode,
.bid_price = data_.bidPrice,
.bid_size = data_.bidSize,
.ask_time = data_.askTime,
.ask_exchange_code = data_.askExchangeCode,
.ask_price = data_.askPrice,
.ask_size = data_.askSize,
};

MarketEvent::fillGraalData(static_cast<void*>(graalQuote));
auto *graalQuote = new (std::nothrow)
dxfg_quote_t{.market_event = {.event_type = {.clazz = dxfg_event_clazz_t::DXFG_EVENT_QUOTE}}};

if (!graalQuote) {
// TODO: error handling
}

fillGraalData(static_cast<void *>(graalQuote));

return static_cast<void *>(graalQuote);
}

Expand All @@ -126,15 +143,14 @@ void Quote::freeGraal(void *graalNative) noexcept {
return;
}

auto eventType = dxfcpp::bit_cast<dxfg_event_type_t *>(graalNative);

if (eventType->clazz != dxfg_event_clazz_t::DXFG_EVENT_QUOTE) {
if (static_cast<dxfg_event_type_t *>(graalNative)->clazz != dxfg_event_clazz_t::DXFG_EVENT_QUOTE) {
return;
}

auto graalQuote = dxfcpp::bit_cast<dxfg_quote_t *>(graalNative);
auto graalQuote = static_cast<dxfg_quote_t *>(graalNative);

MarketEvent::freeGraalData(graalNative);

delete[] graalQuote->market_event.event_symbol;
delete graalQuote;
}

Expand Down

0 comments on commit 66fc51a

Please sign in to comment.