dxFeed Graal CXX API v9.0.0
Loading...
Searching...
No Matches
AutoClose.hpp
1// Copyright (c) 2026 Devexperts LLC.
2// SPDX-License-Identifier: MPL-2.0
3
4#pragma once
5
6#include "../detail/Conf.hpp"
7
8#include "../detail/CallbackBarrier.hpp"
9
10#include <memory>
11#include <utility>
12
13/**
14 * @page entity_lifetime Lifetime of the entities
15 *
16 * The entities of the library live as in the Java API: an entity that is closed is released when the last pointer to
17 * it is released, and the library does not call its listeners after it is closed.
18 *
19 * **Owners.** Releasing the last pointer of the user does not close everything:
20 *
21 * - A subscription created by DXFeed::createSubscription() or attached by DXFeed::attachSubscription() is owned by the
22 * feed: it receives events until DXFeedSubscription::close(), DXFeedSubscription::detach() or the close of the
23 * endpoint, even without a pointer of the user. A subscription without a feed is closed with its last pointer.
24 * - An endpoint created by DXEndpoint::create() or DXEndpoint::Builder::build() lives until DXEndpoint::close() or
25 * DXEndpoint::closeAndAwaitTermination(); then it is released with its feed, publisher and OnDemandService when the
26 * user releases it. The default endpoints of DXEndpoint::getInstance(), DXFeed::getInstance() and
27 * DXPublisher::getInstance() stay for the whole process.
28 * - The transaction models, MarketDepthModel, InstrumentProfileConnection and PriceLevelService are closed with their
29 * last pointer. A model owns its listener, an InstrumentProfileConnection owns its collector, an endpoint owns its
30 * OnDemandService.
31 * - A listener that keeps a pointer to the entity it listens to keeps that entity: keep a `std::weak_ptr` in it.
32 *
33 * An endpoint is not closed with its last pointer, as in the Java API, where an endpoint is not closed by the garbage
34 * collector. `DXEndpoint::create()->connect(address)->getFeed()` keeps no pointer to the endpoint, and the feed does
35 * not own its endpoint: closing it there would leave the feed without data and without an error. The last pointer can
36 * also be released in a listener on a thread of the library or at the exit of the process, where close() could block
37 * or fail.
38 *
39 * AutoClose closes an entity when it goes out of scope, as try-with-resources does in Java.
40 *
41 * **Closing elsewhere.** To close an entity in another scope or on another thread, keep a `std::shared_ptr` to it and
42 * call close() there: close() can be called from any thread and more than once. Or move the AutoClose there: return
43 * it from a function, keep it in a member of the owner or move it into a lambda.
44 *
45 * ```cpp
46 * struct Session {
47 * dxfcpp::AutoClose<dxfcpp::DXEndpoint> endpoint; // Closed when the Session is destroyed.
48 * };
49 *
50 * std::thread worker{[endpoint = dxfcpp::AutoClose{dxfcpp::DXEndpoint::create()->connect(address)}] {
51 * // ...
52 * }}; // The endpoint is closed when the thread ends.
53 * ```
54 *
55 * One AutoClose must not be used from two threads at once, as one `std::shared_ptr` must not; moving it to another
56 * thread is fine.
57 *
58 * **close().** DXFeedSubscription::close() returns after the calls of its listeners in progress on other threads
59 * end; called from a listener of the library, it does not wait. A batch of events that the Java code delivers after
60 * close() is dropped. A subscription or an endpoint can be closed from a listener of a subscription. `CLOSED` is the
61 * last state notification of an endpoint. A closed subscription delivers no events; adding a listener to it, attaching
62 * it to a feed or closing it again does nothing.
63 *
64 * DXEndpoint::close() stops the events, but it does not wait for the calls of the listeners in progress: the
65 * subscriptions of the endpoint can be released on another thread. Close the subscription whose listener uses the
66 * objects of a scope before these objects are destroyed, for example with an AutoClose declared after them.
67 *
68 * **Executor.** DXEndpoint::executor() called after DXEndpoint::getFeed() applies to the next events of the existing
69 * subscriptions, as in the Java API.
70 *
71 * **Exit.** At the exit of the process the library does not close the endpoints: the listeners that use the objects
72 * of the program can be called while these objects are being destroyed. Close the endpoints before that, or call
73 * dxfcpp::shutdown() (see its documentation).
74 */
75
76DXFCPP_BEGIN_NAMESPACE
77
78/**
79 * \addtogroup dxfcpp_api
80 * @{
81 */
82
83/**
84 * Closes an entity when it goes out of scope: a subscription, an endpoint, a model, an instrument profile connection
85 * or any other entity with a `close()` method. See @ref entity_lifetime "Lifetime of the entities".
86 *
87 * The AutoClose is moved, not copied: one AutoClose closes the entity once. The other pointers to the entity stay
88 * valid and point to the closed entity. An exception of `close()` in the destructor or in reset() is reported to
89 * ErrorManager.
90 *
91 * Do not wrap a default endpoint (DXEndpoint::getInstance()): closing it closes it for the whole process.
92 *
93 * ```cpp
94 * const dxfcpp::AutoClose endpoint{dxfcpp::DXEndpoint::create()->connect("demo.dxfeed.com:7300")};
95 * const dxfcpp::AutoClose subscription{endpoint->getFeed()->createSubscription(dxfcpp::Quote::TYPE)};
96 *
97 * subscription->addEventListener<dxfcpp::Quote>([&](const auto &quotes) {
98 * // ...
99 * });
100 * subscription->addSymbols("AAPL");
101 * // ...
102 * // Here the subscription is closed first: its listener is not called after the scope. Then the endpoint is closed.
103 * ```
104 *
105 * @tparam T The type of the entity.
106 */
107template <typename T> class AutoClose final {
108 std::shared_ptr<T> entity_{};
109
110 public:
111 /// Creates an empty AutoClose.
112 AutoClose() noexcept = default;
113
114 /**
115 * Creates an AutoClose that closes the entity.
116 *
117 * @param entity The entity or an empty pointer.
118 */
119 explicit AutoClose(std::shared_ptr<T> entity) noexcept : entity_{std::move(entity)} {
120 }
121
122 AutoClose(const AutoClose &) = delete;
123 AutoClose &operator=(const AutoClose &) = delete;
124
125 /// Takes the entity of the other AutoClose, which becomes empty.
126 AutoClose(AutoClose &&other) noexcept : entity_{std::move(other.entity_)} {
127 }
128
129 /// Closes the current entity and takes the entity of the other AutoClose, which becomes empty.
130 AutoClose &operator=(AutoClose &&other) noexcept {
131 if (this != &other) {
132 reset();
133 entity_ = std::move(other.entity_);
134 }
135
136 return *this;
137 }
138
139 /// Closes the entity.
140 ~AutoClose() noexcept {
141 reset();
142 }
143
144 /// Closes the entity now and makes this AutoClose empty.
145 void reset() noexcept {
146 if (auto entity = std::move(entity_)) {
147 runRelease("AutoClose", [&entity] {
148 entity->close();
149 });
150 }
151 }
152
153 /**
154 * Makes this AutoClose empty without closing the entity.
155 *
156 * @return The entity or an empty pointer.
157 */
158 [[nodiscard]] std::shared_ptr<T> release() noexcept {
159 return std::move(entity_);
160 }
161
162 /// @return The entity or an empty pointer.
163 [[nodiscard]] const std::shared_ptr<T> &get() const noexcept {
164 return entity_;
165 }
166
167 /// @return The entity; this AutoClose must not be empty.
168 T *operator->() const noexcept {
169 return entity_.get();
170 }
171
172 /// @return The entity; this AutoClose must not be empty.
173 T &operator*() const noexcept {
174 return *entity_;
175 }
176
177 /// @return `true` if this AutoClose has an entity.
178 explicit operator bool() const noexcept {
179 return static_cast<bool>(entity_);
180 }
181};
182
183/** @} */ // end of dxfcpp_api
184
185DXFCPP_END_NAMESPACE
DXFCPP_EXPORT dxfc_error_code_t dxfc_dxendpoint_builder_with_name(dxfc_dxendpoint_builder_t builderHandle, const char *name)
Changes the name used to distinguish multiple endpoints in the same process (GraalVM Isolate) in logs...
Definition DXEndpoint.cpp:1125
DXFCPP_EXPORT dxfc_error_code_t dxfc_dxendpoint_builder_with_properties(dxfc_dxendpoint_builder_t builder, const dxfc_dxendpoint_property_t **properties, size_t size)
Sets all supported properties from the provided properties object.
Definition DXEndpoint.cpp:1168
DXFCPP_EXPORT dxfc_error_code_t dxfc_dxendpoint_password(dxfc_dxendpoint_t endpoint, const char *password)
Changes password for this endpoint.
Definition DXEndpoint.cpp:1451
DXFCPP_EXPORT dxfc_error_code_t dxfc_dxendpoint_get_publisher(dxfc_dxendpoint_t endpoint, DXFC_OUT dxfc_dxpublisher_t *publisher)
Definition DXEndpoint.cpp:1652
DXFCPP_EXPORT dxfc_error_code_t dxfc_dxendpoint_builder_supports_property(dxfc_dxendpoint_builder_t builder, const char *key, DXFC_OUT int *supports)
Checks if a property is supported.
Definition DXEndpoint.cpp:1200
DXFCPP_EXPORT dxfc_error_code_t dxfc_dxendpoint_add_state_change_listener(dxfc_dxendpoint_t endpoint, dxfc_dxendpoint_state_change_listener listener)
Adds a listener notified about changes in state property.
Definition DXEndpoint.cpp:1598
DXFCPP_EXPORT dxfc_error_code_t dxfc_dxendpoint_disconnect(dxfc_dxendpoint_t endpoint)
Terminates all remote network connections.
Definition DXEndpoint.cpp:1513
#define DXFCPP_EXPORT
Definition api.h:35
void * dxfc_dxendpoint_builder_t
The dxFeed endpoint's builder handle.
Definition api.h:219
DXFCPP_EXPORT dxfc_error_code_t dxfc_dxendpoint_close_and_await_termination(dxfc_dxendpoint_t endpoint)
Closes this endpoint and wait until all pending data processing tasks are completed.
Definition DXEndpoint.cpp:1395
DXFCPP_EXPORT dxfc_error_code_t dxfc_dxendpoint_await_not_connected(dxfc_dxendpoint_t endpoint)
Waits while this endpoint state becomes NOT_CONNECTED or CLOSED.
Definition DXEndpoint.cpp:1564
void(*) dxfc_dxendpoint_state_change_listener(dxfc_dxendpoint_state_t old_state, dxfc_dxendpoint_state_t new_state, void *user_data)
The endpoint current state change listener.
Definition api.h:190
dxfc_dxendpoint_state_t
Represents the current state of endpoint.
Definition api.h:161
@ DXFC_DXENDPOINT_STATE_CLOSED
Endpoint was closed.
Definition api.h:181
@ DXFC_DXENDPOINT_STATE_NOT_CONNECTED
Endpoint was created by is not connected to remote endpoints.
Definition api.h:165
@ DXFC_DXENDPOINT_STATE_CONNECTING
The connect function was called to establish connection to remove endpoint, but the connection is not...
Definition api.h:171
@ DXFC_DXENDPOINT_STATE_CONNECTED
The connection to the remote endpoint is established.
Definition api.h:176
DXFCPP_EXPORT dxfc_error_code_t dxfc_dxendpoint_get_instance(void *user_data, DXFC_OUT dxfc_dxendpoint_t *endpoint)
Returns a default application-wide singleton instance of dxFeed endpoint with a FEED role.
Definition DXEndpoint.cpp:1264
#define DXFC_OUT
Definition api.h:17
DXFCPP_EXPORT dxfc_error_code_t dxfc_dxendpoint_get_state(dxfc_dxendpoint_t endpoint, DXFC_OUT dxfc_dxendpoint_state_t *state)
Returns the state of this endpoint.
Definition DXEndpoint.cpp:1581
void * dxfc_dxendpoint_t
The dxFeed endpoint handle.
Definition api.h:210
DXFCPP_EXPORT dxfc_error_code_t dxfc_dxendpoint_builder_with_property(dxfc_dxendpoint_builder_t builder, const char *key, const char *value)
Sets the specified property.
Definition DXEndpoint.cpp:1146
DXFCPP_EXPORT dxfc_error_code_t dxfc_dxendpoint_builder_free(dxfc_dxendpoint_builder_t builder)
Removes a builder from the registry.
Definition DXEndpoint.cpp:1252
DXFCPP_EXPORT dxfc_error_code_t dxfc_dxendpoint_connect(dxfc_dxendpoint_t endpoint, const char *address)
Connects to the specified remote address.
Definition DXEndpoint.cpp:1473
dxfc_error_code_t
List of error codes.
Definition api.h:61
@ DXFC_EC_ERROR
The error returned if the current operation cannot be completed.
Definition api.h:72
@ DXFC_EC_SUCCESS
OK.
Definition api.h:65
@ DXFC_EC_G_ERR
dxFeed Graal Native API error.
Definition api.h:69
DXFCPP_EXPORT dxfc_error_code_t dxfc_dxendpoint_remove_state_change_listener(dxfc_dxendpoint_t endpoint, dxfc_dxendpoint_state_change_listener listener)
Removes a listener notified about changes in state property.
Definition DXEndpoint.cpp:1624
DXFCPP_EXPORT dxfc_error_code_t dxfc_system_set_property(const char *key, const char *value)
Sets the system property indicated by the specified key.
Definition System.cpp:208
DXFCPP_EXPORT dxfc_error_code_t dxfc_dxendpoint_builder_build(dxfc_dxendpoint_builder_t builder, void *user_data, DXFC_OUT dxfc_dxendpoint_t *endpoint)
Builds the new dxFeed endpoint instance.
Definition DXEndpoint.cpp:1222
DXFCPP_EXPORT dxfc_error_code_t dxfc_dxendpoint_get_feed(dxfc_dxendpoint_t endpoint, DXFC_OUT dxfc_dxfeed_t *feed)
Definition DXEndpoint.cpp:1647
DXFCPP_EXPORT dxfc_error_code_t dxfc_dxendpoint_await_processed(dxfc_dxendpoint_t endpoint)
Waits until this endpoint stops processing data (becomes quiescent).
Definition DXEndpoint.cpp:1547
DXFCPP_EXPORT dxfc_error_code_t dxfc_dxendpoint_close(dxfc_dxendpoint_t endpoint)
Closes this endpoint.
Definition DXEndpoint.cpp:1378
DXFCPP_EXPORT dxfc_error_code_t dxfc_dxendpoint_new_builder(DXFC_OUT dxfc_dxendpoint_builder_t *builder)
Creates a new dxFeed endpoint's builder instance.
Definition DXEndpoint.cpp:1087
DXFCPP_EXPORT dxfc_error_code_t dxfc_dxendpoint_reconnect(dxfc_dxendpoint_t endpoint)
Terminates all established network connections and initiates connecting again with the same address.
Definition DXEndpoint.cpp:1496
DXFCPP_EXPORT dxfc_error_code_t dxfc_dxendpoint_get_role(dxfc_dxendpoint_t endpoint, DXFC_OUT dxfc_dxendpoint_role_t *role)
Returns the role of this endpoint.
Definition DXEndpoint.cpp:1412
DXFCPP_EXPORT dxfc_error_code_t dxfc_dxendpoint_user(dxfc_dxendpoint_t endpoint, const char *user)
Changes username for this endpoint.
Definition DXEndpoint.cpp:1429
DXFCPP_EXPORT dxfc_error_code_t dxfc_system_get_property(const char *key, DXFC_OUT char *buffer, size_t buffer_size)
Gets the system property indicated by the specified key.
dxfc_dxendpoint_role_t
Represents the role of an endpoint that was specified during its creation.
Definition api.h:101
@ DXFC_DXENDPOINT_ROLE_PUBLISHER
PUBLISHER endpoint connects to the remote publisher hub (also known as multiplexor) or creates a publ...
Definition api.h:139
@ DXFC_DXENDPOINT_ROLE_STREAM_FEED
STREAM_FEED endpoint is similar to DXFC_DXENDPOINT_ROLE_FEED and also connects to the remote data fee...
Definition api.h:128
@ DXFC_DXENDPOINT_ROLE_FEED
FEED endpoint connects to the remote data feed provider and is optimized for real-time or delayed dat...
Definition api.h:111
@ DXFC_DXENDPOINT_ROLE_STREAM_PUBLISHER
STREAM_PUBLISHER endpoint is similar to DXFC_DXENDPOINT_ROLE_PUBLISHER and also connects to the remot...
Definition api.h:148
@ DXFC_DXENDPOINT_ROLE_LOCAL_HUB
LOCAL_HUB endpoint is a local hub without the ability to establish network connections.
Definition api.h:155
@ DXFC_DXENDPOINT_ROLE_ON_DEMAND_FEED
ON_DEMAND_FEED endpoint is similar to DXFC_DXENDPOINT_ROLE_FEED, but it is designed to be used with d...
Definition api.h:119
void * dxfc_dxpublisher_t
The dxFeed publisher handle.
Definition api.h:229
DXFCPP_EXPORT dxfc_error_code_t dxfc_dxendpoint_create(void *user_data, DXFC_OUT dxfc_dxendpoint_t *endpoint)
Creates an endpoint with FEED role.
Definition DXEndpoint.cpp:1321
DXFCPP_EXPORT dxfc_error_code_t dxfc_dxendpoint_get_instance2(dxfc_dxendpoint_role_t role, void *user_data, DXFC_OUT dxfc_dxendpoint_t *endpoint)
Returns a default application-wide singleton instance of DXEndpoint for a specific role.
Definition DXEndpoint.cpp:1292
void * dxfc_dxfeed_t
The dxFeed handle.
Definition api.h:224
DXFCPP_EXPORT dxfc_error_code_t dxfc_dxendpoint_builder_with_role(dxfc_dxendpoint_builder_t builder, dxfc_dxendpoint_role_t role)
Sets role for the created dxFeed endpoint.
Definition DXEndpoint.cpp:1108
DXFCPP_EXPORT dxfc_error_code_t dxfc_dxendpoint_create2(dxfc_dxendpoint_role_t role, void *user_data, DXFC_OUT dxfc_dxendpoint_t *endpoint)
Creates an endpoint with a specified role.
Definition DXEndpoint.cpp:1349
DXFCPP_EXPORT dxfc_error_code_t dxfc_dxendpoint_free(dxfc_dxendpoint_t endpoint)
Removes the dxFeed endpoint from the registry.
Definition DXEndpoint.cpp:1657
DXFCPP_EXPORT dxfc_error_code_t dxfc_dxendpoint_disconnect_and_clear(dxfc_dxendpoint_t endpoint)
Terminates all remote network connections and clears stored data.
Definition DXEndpoint.cpp:1530
T & operator*() const noexcept
Definition AutoClose.hpp:173
AutoClose() noexcept=default
Creates an empty AutoClose.
AutoClose(std::shared_ptr< T > entity) noexcept
Creates an AutoClose that closes the entity.
Definition AutoClose.hpp:119
~AutoClose() noexcept
Closes the entity.
Definition AutoClose.hpp:140
const std::shared_ptr< T > & get() const noexcept
Definition AutoClose.hpp:163
std::shared_ptr< T > release() noexcept
Makes this AutoClose empty without closing the entity.
Definition AutoClose.hpp:158
AutoClose & operator=(AutoClose &&other) noexcept
Closes the current entity and takes the entity of the other AutoClose, which becomes empty.
Definition AutoClose.hpp:130
operator bool() const noexcept
Definition AutoClose.hpp:178
AutoClose(AutoClose &&other) noexcept
Takes the entity of the other AutoClose, which becomes empty.
Definition AutoClose.hpp:126
T * operator->() const noexcept
Definition AutoClose.hpp:168
void reset() noexcept
Closes the entity now and makes this AutoClose empty.
Definition AutoClose.hpp:145
Marks all event types that can be received via dxFeed API.
Definition EventType.hpp:42
std::string toString() const override
Returns a string representation of the current object.
Definition api.cpp:57
virtual std::int64_t getEventTime() const noexcept
Returns time when an event was created or zero when time is not available.
Definition api.cpp:44
virtual void assign(std::shared_ptr< EventType > event)
Replaces the contents of the event.
Definition api.cpp:53
virtual void setEventTime(std::int64_t eventTime) noexcept
Changes event creation time.
Definition api.cpp:48
A base abstract "shared entity" class. Has some helpers for dynamic polymorphism.
Definition SharedEntity.hpp:25
The simple key-value structure that represents an endpoint's property.
Definition api.h:196
const char * key
The property's key.
Definition api.h:198
const char * value
The property's value.
Definition api.h:200