dxFeed Graal CXX API v4.2.0
Loading...
Searching...
No Matches
SharedEntity.hpp
1// Copyright (c) 2025 Devexperts LLC.
2// SPDX-License-Identifier: MPL-2.0
3
4#pragma once
5
6#include "../internal/Conf.hpp"
7
9
10#include "../internal/Common.hpp"
11
12#include <memory>
13#include <string>
14#include <vector>
15
16#include "Entity.hpp"
17
19
20/// Base abstract "shared entity" class. Has some helpers for dynamic polymorphism
22 /// The alias to a type of shared pointer to the SharedEntity object.
23 using Ptr = std::shared_ptr<SharedEntity>;
24
25 SharedEntity();
26
27 ~SharedEntity() noexcept override;
28
29 /**
30 * Checks that pointer to the current type could be converted to type T*
31 * In other words: whether type T belongs to the type hierarchy in which the current type resides.
32 * @tparam T The type to check
33 * @return true if type belongs to the type hierarchy in which the current type resides.
34 */
35 template <typename T> bool is() const noexcept {
36 try {
37 auto p = dynamic_cast<const T *>(this);
38
39 ignoreUnused(p);
40
41 return true;
42 } catch (const std::bad_cast &) {
43 return false;
44 }
45 }
46
47 /**
48 * Returns a pointer to the current object wrapped in a smart pointer to type T
49 *
50 * @warning Please do not use this method unless the object was created with `std::shared_ptr<T>(new T(...))` or
51 * `std::make_shared<T>(...)`
52 *
53 * @tparam T The type to convert to a pointer to
54 * @return a smart pointer to type T
55 */
56 template <typename T> std::shared_ptr<T> sharedAs() noexcept {
57 return std::dynamic_pointer_cast<T>(shared_from_this());
58 }
59
60 /**
61 * Returns a pointer to the current object wrapped in a smart pointer to type T
62 *
63 * @warning Please do not use this method unless the object was created with `std::shared_ptr<T>(new T(...))` or
64 * `std::make_shared<T>(...)`
65 *
66 * @tparam T The type to convert to a pointer to
67 * @return a smart pointer to type T
68 */
69 template <typename T> std::shared_ptr<T> sharedAs() const noexcept {
70 return std::dynamic_pointer_cast<T>(shared_from_this());
71 }
72
73 /**
74 * Returns a string representation of the current object.
75 *
76 * @return a string representation
77 */
78 virtual std::string toString() const {
79 return "SharedEntity{}";
80 }
81};
82
83DXFCXX_DISABLE_GCC_WARNINGS_PUSH("-Wvirtual-move-assign")
84
85/**
86 * A helper class needed to construct smart pointers to objects, and does not allow explicit construction of objects.
87 * @tparam T The object type.
88 */
89template <typename T> struct RequireMakeShared : virtual SharedEntity {
90 protected:
91 struct LockExternalConstructionTag {
92 explicit LockExternalConstructionTag() = default;
93 };
94
95 public:
96 /**
97 * Creates smart pointer to object.
98 *
99 * @tparam Args Types or arguments.
100 * @param args The arguments.
101 * @return A new smart pointer to object.
102 */
103 template <typename... Args> static auto createShared(Args &&...args) {
104 static_assert(std::is_convertible_v<T *, RequireMakeShared *>, "Must derive publicly from RequireMakeShared");
105
106 return std::make_shared<T>(LockExternalConstructionTag{}, std::forward<Args>(args)...);
107 }
108};
109
111
112template <typename EBase, Derived<EBase> EDerived>
113static std::shared_ptr<EDerived> convertEvent(const std::shared_ptr<EBase> &source) {
114 if (!source) {
115 return {};
116 }
117
118 return source->template sharedAs<EDerived>();
119}
120
121template <typename EBase, Derived<EBase> EDerived>
122static std::vector<std::shared_ptr<EDerived>> convertEvents(const std::vector<std::shared_ptr<EBase>> &source) {
123 std::vector<std::shared_ptr<EDerived>> result{};
124
125 result.reserve(source.size());
126
127 for (const auto &e : source) {
128 result.emplace_back(e->template sharedAs<EDerived>());
129 }
130
131 return result;
132}
133
135
#define DXFCXX_DISABLE_MSC_WARNINGS_POP()
Definition Conf.hpp:22
#define DXFCPP_END_NAMESPACE
Definition Conf.hpp:70
#define DXFCPP_BEGIN_NAMESPACE
Definition Conf.hpp:67
#define DXFCXX_DISABLE_GCC_WARNINGS_PUSH(warnings)
Definition Conf.hpp:38
#define DXFCXX_DISABLE_GCC_WARNINGS_POP()
Definition Conf.hpp:40
#define DXFCXX_DISABLE_MSC_WARNINGS_PUSH(warnings)
Definition Conf.hpp:21
#define DXFCPP_EXPORT
Definition api.h:35
Base abstract class for all dxFeed C++ API entities.
Definition Entity.hpp:13
Marks all event types that can be received via dxFeed API.
Definition EventType.hpp:31
A helper class needed to construct smart pointers to objects, and does not allow explicit constructio...
Definition SharedEntity.hpp:89
static auto createShared(Args &&...args)
Creates smart pointer to object.
Definition SharedEntity.hpp:103
Base abstract "shared entity" class. Has some helpers for dynamic polymorphism.
Definition SharedEntity.hpp:21
virtual std::string toString() const
Returns a string representation of the current object.
Definition SharedEntity.hpp:78
std::shared_ptr< T > sharedAs() const noexcept
Returns a pointer to the current object wrapped in a smart pointer to type T.
Definition SharedEntity.hpp:69
std::shared_ptr< T > sharedAs() noexcept
Returns a pointer to the current object wrapped in a smart pointer to type T.
Definition SharedEntity.hpp:56
bool is() const noexcept
Checks that pointer to the current type could be converted to type T* In other words: whether type T ...
Definition SharedEntity.hpp:35