dxFeed Graal CXX API
Loading...
Searching...
No Matches
DXFeedSubscription Class Reference

Subscription for a set of symbols and event types. More...

#include <DXFeedSubscription.hpp>

+ Inheritance diagram for DXFeedSubscription:

Public Types

using Ptr = std::shared_ptr<DXFeedSubscription>
 The alias to a type of shared pointer to the DXFeedSubscription object.
 
using Unique = std::unique_ptr<DXFeedSubscription>
 The alias to a type of unique pointer to the DXFeedSubscription object.
 
- Public Types inherited from SharedEntity
using Ptr = std::shared_ptr<SharedEntity>
 The alias to a type of shared pointer to the SharedEntity object.
 

Public Member Functions

std::string toString () const noexcept override
 Returns a string representation of the current object.
 
void attach (std::shared_ptr< DXFeed > feed) noexcept
 Attaches subscription to the specified feed.
 
void detach (std::shared_ptr< DXFeed > feed) noexcept
 Detaches subscription from the specified feed.
 
void close () noexcept
 Closes this subscription and makes it permanently detached.
 
template<typename EventListener >
std::size_t addEventListener (EventListener &&listener) noexcept
 Adds listener for events.
 
template<typename EventT >
std::size_t addEventListener (std::function< void(const std::vector< std::shared_ptr< EventT > > &)> &&listener) noexcept
 Adds typed listener for events.
 
void removeEventListener (std::size_t listenerId) noexcept
 Removes listener for events.
 
auto & onEvent () noexcept
 Returns a reference to an incoming events' handler (delegate), to which listeners can be added and removed.
 
void addSymbols (const SymbolWrapper &symbolWrapper) const noexcept
 Adds the specified symbol to the set of subscribed symbols.
 
void removeSymbols (const SymbolWrapper &symbolWrapper) const noexcept
 Removes the specified symbol from the set of subscribed symbols.
 
template<typename SymbolIt >
void addSymbols (SymbolIt begin, SymbolIt end) const
 Adds the specified collection (using iterators) of symbols to the set of subscribed symbols.
 
template<ConvertibleToSymbolWrapperCollection SymbolsCollection>
void addSymbols (const SymbolsCollection &collection) const noexcept
 Adds the specified collection of symbols to the set of subscribed symbols.
 
void addSymbols (std::initializer_list< SymbolWrapper > collection) const noexcept
 Adds the specified collection (initializer list) of symbols to the set of subscribed symbols.
 
template<typename SymbolIt >
void removeSymbols (SymbolIt begin, SymbolIt end) const
 Removes the specified collection (using iterators) of symbols from the set of subscribed symbols.
 
template<ConvertibleToSymbolWrapperCollection SymbolsCollection>
void removeSymbols (SymbolsCollection &&collection) const noexcept
 Removes the specified collection of symbols from the set of subscribed symbols.
 
void removeSymbols (std::initializer_list< SymbolWrapper > collection) const noexcept
 Removes the specified collection (initializer list) of symbols from the set of subscribed symbols.
 
template<typename SymbolIt >
void setSymbols (SymbolIt begin, SymbolIt end) const
 Changes the set of subscribed symbols so that it contains just the symbols from the specified collection (using iterators).
 
template<ConvertibleToSymbolWrapperCollection SymbolsCollection>
void setSymbols (SymbolsCollection &&collection) const noexcept
 Changes the set of subscribed symbols so that it contains just the symbols from the specified collection.
 
void setSymbols (std::initializer_list< SymbolWrapper > collection) const noexcept
 Changes the set of subscribed symbols so that it contains just the symbols from the specified collection (initializer list).
 
void clear () const noexcept
 Clears the set of subscribed symbols.
 
bool isClosed () const noexcept
 Returns true if this subscription is closed.
 
const std::unordered_set< EventTypeEnum > & getEventTypes () const noexcept
 Returns a set of subscribed event types.
 
bool containsEventType (const EventTypeEnum &eventType) const noexcept
 Returns true if this subscription contains the corresponding event type.
 
std::vector< SymbolWrappergetSymbols () const noexcept
 Returns a set of subscribed symbols (depending on the actual implementation of subscription).
 
std::vector< SymbolWrappergetDecoratedSymbols () noexcept
 Returns a set of decorated symbols (depending on the actual implementation of subscription).
 
- Public Member Functions inherited from SharedEntity
template<typename T >
bool is () const noexcept
 Checks that pointer to the current type could be converted to type T* In other words: whether type T belongs to the type hierarchy in which the current type resides.
 
template<typename T >
std::shared_ptr< T > sharedAs () noexcept
 Returns a pointer to the current object wrapped in a smart pointer to type T.
 
template<typename T >
std::shared_ptr< T > sharedAs () const noexcept
 Returns a pointer to the current object wrapped in a smart pointer to type T.
 
- Public Member Functions inherited from Entity
virtual ~Entity () noexcept=default
 The default virtual d-tor.
 

Static Public Member Functions

static std::shared_ptr< DXFeedSubscriptioncreate (const EventTypeEnum &eventType)
 Creates detached subscription for a single event type.
 
template<typename EventTypeIt >
static std::shared_ptr< DXFeedSubscriptioncreate (EventTypeIt begin, EventTypeIt end)
 Creates detached subscription for the given collection of event types.
 
static std::shared_ptr< DXFeedSubscriptioncreate (std::initializer_list< EventTypeEnum > eventTypes)
 Creates detached subscription for the given collection of event types.
 
template<typename EventTypesCollection >
static std::shared_ptr< DXFeedSubscriptioncreate (EventTypesCollection &&eventTypes)
 Creates detached subscription for the given collection of event types.
 

Friends

struct DXFeed
 

Detailed Description

Subscription for a set of symbols and event types.

Member Function Documentation

◆ addEventListener() [1/2]

template<typename EventListener >
std::size_t DXFeedSubscription::addEventListener ( EventListener && listener)
inlinenoexcept

Adds listener for events.

Event lister can be added only when subscription is not producing any events. The subscription must be either empty (its set of symbols is empty or not attached to any feed (its set of change listeners is empty).

This method does nothing if this subscription is closed.

Example:

auto sub = endpoint->getFeed()->createSubscription({dxfcpp::Quote::TYPE, dxfcpp::TimeAndSale::TYPE});
sub->addEventListener([](auto &&events) {
for (const auto &e : events) {
if (auto quote = e->template sharedAs<dxfcpp::Quote>(); quote) {
std::cout << "Q : " + quote->toString() << std::endl;
} else if (auto tns = e->template sharedAs<dxfcpp::TimeAndSale>(); tns) {
std::cout << "TnS : " + tns->toString() << std::endl;
}
}
});
sub->addSymbols({"$TOP10L/Q", "$SP500#45", "$TICK", "SPX"});
std::shared_ptr< T > sharedAs() noexcept
Returns a pointer to the current object wrapped in a smart pointer to type T.
Definition SharedEntity.hpp:47
Template Parameters
EventListenerThe listener type. Listener can be callable with signature: void(const std::vector<std::shared_ptr<EventType>&)
Parameters
listenerThe event listener
Returns
The listener id

◆ addEventListener() [2/2]

template<typename EventT >
std::size_t DXFeedSubscription::addEventListener ( std::function< void(const std::vector< std::shared_ptr< EventT > > &)> && listener)
inlinenoexcept

Adds typed listener for events.

Event lister can be added only when subscription is not producing any events. The subscription must be either empty (its set of symbols is empty or not attached to any feed (its set of change listeners is empty).

This method does nothing if this subscription is closed.

Example:

auto sub = endpoint->getFeed()->createSubscription({dxfcpp::Quote::TYPE, dxfcpp::TimeAndSale::TYPE});
sub->addEventListener(std::function([](const std::vector<std::shared_ptr<dxfcpp::Quotes>> &quotes) -> void {
for (const auto &q : quotes) {
std::cout << "Q : " + q->toString() << std::endl;
}
}));
sub->addEventListener<dxfcpp::TimeAndSale>([](const auto &timeAndSales) -> void {
for (const auto &tns : timeAndSales) {
std::cout << "TnS : " + tns->toString() << std::endl;
}
});
sub->addEventListener<dxfcpp::MarketEvent>([](const auto &marketEvents) -> void {
for (const auto &me : marketEvents) {
std::cout << "Market Event's symbol: " + me->getEventSymbol() << std::endl;
}
});
sub->addSymbols({"$TOP10L/Q", "AAPL", "$TICK", "SPX"});
Template Parameters
EventTThe event type (EventType's child with field TYPE, convertible to EventTypeEnum or MarketEvent or LastingEvent or TimeSeriesEvent or IndexedEvent)
Parameters
listenerThe listener. Listener can be callable with signature: void(const std::vector<std::shared_ptr<EventT>&)
Returns
The listener id

◆ addSymbols() [1/4]

template<ConvertibleToSymbolWrapperCollection SymbolsCollection>
void DXFeedSubscription::addSymbols ( const SymbolsCollection & collection) const
inlinenoexcept

Adds the specified collection of symbols to the set of subscribed symbols.

Example:

auto v = std::vector<dxfcpp::SymbolWrapper>{"XBT/USD:GDAX"s, "BTC/EUR:CXBITF"sv, "TSLA", "GOOG"_s};
sub->addSymbols(std::vector{"AAPL", "IBM"});
sub->addSymbols(v);
Template Parameters
SymbolsCollectionThe symbols collection's type
Parameters
collectionThe symbols collection

◆ addSymbols() [2/4]

void DXFeedSubscription::addSymbols ( const SymbolWrapper & symbolWrapper) const
inlinenoexcept

Adds the specified symbol to the set of subscribed symbols.

This is a convenience method to subscribe to one symbol at a time that has a return fast-path for a case when the symbol is already in the set. When subscribing to multiple symbols at once it is preferable to use addSymbols(symbols) method.

Example:

sub->addSymbols("TSLA");
sub->addSymbols("XBT/USD:GDAX"s);
sub->addSymbols("BTC/EUR:CXBITF"sv);
Parameters
symbolWrapperThe symbol.

References toString().

◆ addSymbols() [3/4]

void DXFeedSubscription::addSymbols ( std::initializer_list< SymbolWrapper > collection) const
inlinenoexcept

Adds the specified collection (initializer list) of symbols to the set of subscribed symbols.

Example:

sub->addSymbols({"AAPL", "IBM"sv, "TSLA"s, "GOOG"_s});
Parameters
collectionThe symbols collection

◆ addSymbols() [4/4]

template<typename SymbolIt >
void DXFeedSubscription::addSymbols ( SymbolIt begin,
SymbolIt end ) const
inline

Adds the specified collection (using iterators) of symbols to the set of subscribed symbols.

Example:

auto v = std::vector<dxfcpp::SymbolWrapper>{"XBT/USD:GDAX"s, "BTC/EUR:CXBITF"sv, "TSLA", "GOOG"_s};
sub->addSymbols(v.begin(), v.end());
Template Parameters
SymbolItThe collection's iterator type
Parameters
beginThe beginning of the collection of symbols.
endThe end of symbol collection.

◆ attach()

DXFCPP_BEGIN_NAMESPACE void DXFeedSubscription::attach ( std::shared_ptr< DXFeed > feed)
noexcept

Attaches subscription to the specified feed.

Parameters
feedThe feed to attach to.

◆ close()

void DXFeedSubscription::close ( )
inlinenoexcept

Closes this subscription and makes it permanently detached.

This method notifies all installed instances of subscription change listeners by invoking subscriptionClosed while holding the lock for this subscription. This method clears lists of all installed event listeners and subscription change listeners and makes sure that no more listeners can be added.

References toString().

◆ containsEventType()

bool DXFeedSubscription::containsEventType ( const EventTypeEnum & eventType) const
inlinenoexcept

Returns true if this subscription contains the corresponding event type.

Parameters
eventTypeThe type of event that is checked.
Returns
true if this subscription contains the corresponding event type.
See also
DXFeedSubscription::getEventTypes()

◆ create() [1/4]

static std::shared_ptr< DXFeedSubscription > DXFeedSubscription::create ( const EventTypeEnum & eventType)
inlinestatic

Creates detached subscription for a single event type.

Example:

auto sub = dxfcpp::DXFeedSubscription::create(dxfcpp::Quote::TYPE);
Parameters
eventTypethe event type.

References EventTypeEnum::getName().

◆ create() [2/4]

template<typename EventTypeIt >
static std::shared_ptr< DXFeedSubscription > DXFeedSubscription::create ( EventTypeIt begin,
EventTypeIt end )
inlinestatic

Creates detached subscription for the given collection of event types.

Example:

auto eventTypes = {dxfcpp::Quote::TYPE, dxfcpp::TimeAndSale::TYPE};
auto sub = dxfcpp::DXFeedSubscription::create(eventTypes.begin(), eventTypes.end());
std::vector types{dxfcpp::Quote::TYPE, dxfcpp::Trade::TYPE, dxfcpp::Summary::TYPE};
auto sub = dxfcpp::DXFeedSubscription::create(types.begin(), types.end());
Template Parameters
EventTypeItThe collection's iterator type
Parameters
beginThe beginning of the collection of event types.
endThe end of event type collection.
Returns
The new detached subscription for the given collection of event types.

◆ create() [3/4]

template<typename EventTypesCollection >
static std::shared_ptr< DXFeedSubscription > DXFeedSubscription::create ( EventTypesCollection && eventTypes)
inlinestatic

Creates detached subscription for the given collection of event types.

Example:

auto sub = dxfcpp::DXFeedSubscription::create(std::unordered_set{dxfcpp::Quote::TYPE,
dxfcpp::TimeAndSale::TYPE});
std::vector types = {dxfcpp::Quote::TYPE, dxfcpp::TimeAndSale::TYPE};
auto sub = dxfcpp::DXFeedSubscription::create(types);
Template Parameters
EventTypesCollectionThe type of the collection of event types
Parameters
eventTypesThe event type collection.
Returns
The new detached subscription for the given collection of event types.

◆ create() [4/4]

static std::shared_ptr< DXFeedSubscription > DXFeedSubscription::create ( std::initializer_list< EventTypeEnum > eventTypes)
inlinestatic

Creates detached subscription for the given collection of event types.

Example:

auto sub = dxfcpp::DXFeedSubscription::create({dxfcpp::Quote::TYPE, dxfcpp::TimeAndSale::TYPE});
Parameters
eventTypesThe event type collection.
Returns
The new detached subscription for the given collection of event types.

◆ detach()

void DXFeedSubscription::detach ( std::shared_ptr< DXFeed > feed)
noexcept

Detaches subscription from the specified feed.

Parameters
feedThe feed to detach from.

◆ getDecoratedSymbols()

std::vector< SymbolWrapper > DXFeedSubscription::getDecoratedSymbols ( )
inlinenoexcept

Returns a set of decorated symbols (depending on the actual implementation of subscription).

The resulting set maybe either a snapshot of the set of the subscribed symbols at the time of invocation or a weakly consistent view of the set.

Returns
A set of decorated subscribed symbols.

References toString().

◆ getEventTypes()

const std::unordered_set< EventTypeEnum > & DXFeedSubscription::getEventTypes ( ) const
inlinenoexcept

Returns a set of subscribed event types.

Returns
A set of subscribed event types.

◆ getSymbols()

std::vector< SymbolWrapper > DXFeedSubscription::getSymbols ( ) const
inlinenoexcept

Returns a set of subscribed symbols (depending on the actual implementation of subscription).

The resulting set maybe either a snapshot of the set of the subscribed symbols at the time of invocation or a weakly consistent view of the set.

Returns
A set of subscribed symbols.

References toString().

◆ isClosed()

bool DXFeedSubscription::isClosed ( ) const
inlinenoexcept

Returns true if this subscription is closed.

Returns
true if this subscription is closed.
See also
DXFeedSubscription::close()

References toString().

◆ onEvent()

auto & DXFeedSubscription::onEvent ( )
inlinenoexcept

Returns a reference to an incoming events' handler (delegate), to which listeners can be added and removed.

Listener can be callable with signature: void(const std::vector<std::shared_ptr<EventType>&)

Example:

auto sub = endpoint->getFeed()->createSubscription({dxfcpp::Quote::TYPE, dxfcpp::TimeAndSale::TYPE});
auto id = sub->onEvent() += [](auto &&events) {
for (const auto &e : events) {
if (auto quote = e->template sharedAs<dxfcpp::Quote>(); quote) {
std::cout << "Q : " + quote->toString() << std::endl;
} else if (auto tns = e->template sharedAs<dxfcpp::TimeAndSale>(); tns) {
std::cout << "TnS : " + tns->toString() << std::endl;
}
}
};
sub->addSymbols({"$TOP10L/Q", "$SP500#45", "$TICK", "SPX"});
sub->onEvent() -= id;
Returns
The incoming events' handler (delegate)

◆ removeEventListener()

void DXFeedSubscription::removeEventListener ( std::size_t listenerId)
inlinenoexcept

Removes listener for events.

Example:

auto id = sub->addEventListener([](auto){});
sub->removeEventListener(id);
Parameters
listenerIdThe listener id

◆ removeSymbols() [1/4]

void DXFeedSubscription::removeSymbols ( const SymbolWrapper & symbolWrapper) const
inlinenoexcept

Removes the specified symbol from the set of subscribed symbols.

To conveniently remove one or few symbols you can use removeSymbols(symbols) method.

Example:

sub->removeSymbols("TSLA");
sub->removeSymbols("XBT/USD:GDAX"s);
sub->removeSymbols("BTC/EUR:CXBITF"sv);
Parameters
symbolWrapperThe symbol.

References toString().

◆ removeSymbols() [2/4]

void DXFeedSubscription::removeSymbols ( std::initializer_list< SymbolWrapper > collection) const
inlinenoexcept

Removes the specified collection (initializer list) of symbols from the set of subscribed symbols.

Example:

sub->removeSymbols({"AAPL", "IBM"sv, "TSLA"s, "GOOG"_s});
Parameters
collectionThe symbols collection

◆ removeSymbols() [3/4]

template<typename SymbolIt >
void DXFeedSubscription::removeSymbols ( SymbolIt begin,
SymbolIt end ) const
inline

Removes the specified collection (using iterators) of symbols from the set of subscribed symbols.

Example:

auto v = std::vector<dxfcpp::SymbolWrapper>{"XBT/USD:GDAX"s, "BTC/EUR:CXBITF"sv, "TSLA", "GOOG"_s};
sub->removeSymbols(v.begin(), v.end());
Template Parameters
SymbolItThe collection's iterator type
Parameters
beginThe beginning of the collection of symbols.
endThe end of symbol collection.

◆ removeSymbols() [4/4]

template<ConvertibleToSymbolWrapperCollection SymbolsCollection>
void DXFeedSubscription::removeSymbols ( SymbolsCollection && collection) const
inlinenoexcept

Removes the specified collection of symbols from the set of subscribed symbols.

Example:

auto v = std::vector<dxfcpp::SymbolWrapper>{"XBT/USD:GDAX"s, "BTC/EUR:CXBITF"sv, "TSLA", "GOOG"_s};
sub->removeSymbols(std::vector{"AAPL", "IBM"});
sub->removeSymbols(v);
Template Parameters
SymbolsCollectionThe symbols collection's type
Parameters
collectionThe symbols collection

◆ setSymbols() [1/3]

void DXFeedSubscription::setSymbols ( std::initializer_list< SymbolWrapper > collection) const
inlinenoexcept

Changes the set of subscribed symbols so that it contains just the symbols from the specified collection (initializer list).

Example:

sub->setSymbols({"AAPL", "IBM"sv, "TSLA"s, "GOOG"_s});
Parameters
collectionThe symbols collection

◆ setSymbols() [2/3]

template<typename SymbolIt >
void DXFeedSubscription::setSymbols ( SymbolIt begin,
SymbolIt end ) const
inline

Changes the set of subscribed symbols so that it contains just the symbols from the specified collection (using iterators).

Example:

auto v = std::vector<dxfcpp::SymbolWrapper>{"XBT/USD:GDAX"s, "BTC/EUR:CXBITF"sv, "TSLA", "GOOG"_s};
sub->setSymbols(v.begin(), v.end());
Template Parameters
SymbolItThe collection's iterator type
Parameters
beginThe beginning of the collection of symbols.
endThe end of symbol collection.

◆ setSymbols() [3/3]

template<ConvertibleToSymbolWrapperCollection SymbolsCollection>
void DXFeedSubscription::setSymbols ( SymbolsCollection && collection) const
inlinenoexcept

Changes the set of subscribed symbols so that it contains just the symbols from the specified collection.

Example:

auto v = std::vector<dxfcpp::SymbolWrapper>{"XBT/USD:GDAX"s, "BTC/EUR:CXBITF"sv, "TSLA", "GOOG"_s};
sub->setSymbols(std::vector{"AAPL", "IBM"});
sub->setSymbols(v);
Template Parameters
SymbolsCollectionThe symbols collection's type
Parameters
collectionThe symbols collection

◆ toString()

std::string DXFeedSubscription::toString ( ) const
overridevirtualnoexcept

Returns a string representation of the current object.

Returns
a string representation

Reimplemented from SharedEntity.

Referenced by addSymbols(), clear(), close(), getDecoratedSymbols(), getSymbols(), isClosed(), and removeSymbols().