RTC Toolkit 5.0.0
Loading...
Searching...
No Matches
repositorySubscriberIf.ipp
Go to the documentation of this file.
1
13// Note this is a template implementation file and should not be included directly.
14// The typical header protection macro is not added to avoid it showing up in Doxygen API
15// documentation.
16#pragma once
17
18#include <exception>
19#include <functional>
20#include <memory>
21#include <type_traits>
22
24
25namespace detail {
26
33
40
44
45} // namespace detail
46
47// NOTE: The enable_if can be removed once the legacy Subscribe template methods using
48// SubscribeRequest have been removed.
49template <
50 typename T,
51 typename F,
52 std::enable_if_t<
53 std::is_convertible_v<F, RepositorySubscriberIf::NotifyCallbackType> or
54 (not std::is_same_v<T, void> and
55 std::is_convertible_v<F,
57 std::conditional_t<std::is_same_v<T, void>, std::byte, T>>>),
58 bool>>
61 const F& cb_value,
62 const ErrorCallbackType& cb_error) const {
64 if constexpr (std::is_convertible_v<F, NotifyCallbackType>) {
65 cb_val = cb_value;
66 } else {
67 static_assert(std::is_convertible_v<F, ValueCallbackType<T>>,
68 "The cb_value callback must have a signature compatible with"
69 " void(const DataPointPath&, T&, const MetaData&).");
70 cb_val = [buf = std::make_shared<T>(), cb_value, cb_error, this](const DataPointPath& path2,
71 const MetaData& md) {
73 BatchRequest req;
74 req.ReadDataPoint(path2, *buf, mdr);
75 SendRequest(req).Wait();
76 if (md.Contains("sequence_id")) {
77 // If backend supports sequence_id, then check for sequence id missmatch.
78 RtcUInt64 sequence_id_expected = md["sequence_id"].Cast<RtcUInt64>();
79 RtcUInt64 sequence_id_actual = mdr["sequence_id"].Cast<RtcUInt64>();
81 cb_value(path2, *buf, md);
82 } else {
83 auto eptr = std::make_exception_ptr(CII_MAKE_EXCEPTION(
85 cb_error(eptr);
86 }
87 } else {
88 // If backend does not support sequence_id, then just invoke cb_value without check.
89 cb_value(path2, *buf, md);
90 }
91 };
92 }
93 BatchRequest req;
94 auto id = req.Subscribe(path, cb_val, cb_error);
95 SendRequest(req).Wait();
96 Subscription subscription([this, id]() {
97 BatchRequest req;
98 req.Unsubscribe(id);
99 SendRequest(req).Wait();
100 });
101 return subscription;
102}
103
105// The following are legacy methods:
106
127template <typename F>
129 F handler) {
130 static_assert(
131 std::is_convertible_v<F, std::function<void(const DataPointPath&)>>,
132 "The handler callback must have a signature compatible with void(const DataPointPath&).");
133 m_params.push_back(Parameters(path, nullptr, typeid(void), handler, nullptr));
134}
135
164template <typename T, typename F>
166 T& buffer,
167 F handler) {
168 static_assert(std::is_convertible_v<F, ValueHandler<T>>,
169 "The handler callback must have a signature compatible with void(const"
170 " DataPointPath&, T&).");
171 // We use a lambda here to have type erasure and allow storing callbacks for different types in
172 // the m_newvalue_callback.
173 auto callback = [handler, &buffer](const DataPointPath& path) -> void {
174 handler(path, buffer);
175 };
176 m_params.push_back(Parameters(path, true, &buffer, typeid(buffer), nullptr, callback));
177}
178
198template <typename F>
200 F handler) {
201 static_assert(std::is_convertible_v<F, NotifyHandler>,
202 "The handler callback must have a signature compatible with void(const"
203 " DataPointPath&).");
204 // We use a lambda here to have type erasure and allow storing callbacks for different types in
205 // the m_newvalue_callback.
206 auto callback = [handler](const DataPointPath& path) -> void { handler(path); };
207 m_params.push_back(Parameters(path, false, nullptr, typeid(void), nullptr, callback));
208}
209
233template <typename T,
234 typename F,
235 std::enable_if_t<not std::is_convertible_v<T, RepositorySubscriberIf::NotifyCallbackType>,
236 bool>>
242
243} // namespace rtctk::componentFramework
This class provides a wrapper for a data point path.
Definition dataPointPath.hpp:74
void ReadDataPoint(const DataPointPath &path, T &buffer, std::optional< std::reference_wrapper< MetaData > > metadata=std::nullopt, const CallbackType &callback=nullptr) const
Definition repositoryIf.ipp:1395
void Wait()
Definition repositoryIf.cpp:342
Class for passing/receiving metadata to/from the repository.
Definition repositoryIf.hpp:195
void Wait()
Waits for the request that was sent to the repository to complete.
Definition repositoryIf.cpp:611
BatchResponse SendRequest(const BatchRequest &request)
Definition repositoryIf.cpp:364
std::function< void(const DataPointPath &)> CallbackType
Signature of the callback functions that can be registered with the repository requests.
Definition repositoryIf.hpp:114
void Unsubscribe(SubscriptionId) const
Definition repositorySubscriberIf.cpp:49
SubscriptionId Subscribe(const DataPointPath &, NotifyCallbackType, ErrorCallbackType) const
Definition repositorySubscriberIf.cpp:30
A structure to hold the arguments passed to the Add method.
Definition repositorySubscriberIf.hpp:167
A request object to pass information about datapoints to subscribe to.
Definition repositorySubscriberIf.hpp:161
void AddNewValueHandler(const DataPointPath &path, T &buffer, F handler)
Adds a request to subscribe to new data values for a particular datapoint.
Definition repositorySubscriberIf.ipp:165
void AddRemovedHandler(const DataPointPath &path, F handler)
Adds a request to subscribe to removal notifications for a particular datapoint.
Definition repositorySubscriberIf.ipp:128
void AddValueChangedHandler(const DataPointPath &path, F handler)
Adds a request to subscribe to value change notifications for a particular datapoint.
Definition repositorySubscriberIf.ipp:199
RAII wrapper class used to manage the life-time of individual subscriptions.
Definition repositorySubscriberIf.hpp:64
std::function< void(std::exception_ptr)> ErrorCallbackType
Definition repositorySubscriberIf.hpp:49
virtual RepositoryIf::Response SendSubscribeRequest(const SubscribeRequest &request) const
This is called to asynchronously send a subscription request for datapoints.
Definition repositorySubscriberIf.cpp:162
Subscription Subscribe(const DataPointPath &path, const F &cb_value, const ErrorCallbackType &cb_error) const
Subscribes to datapoint values change events by registering callbacks.
Definition repositorySubscriberIf.ipp:60
std::function< void(const DataPointPath &, T &, const MetaData &)> ValueCallbackType
Definition repositorySubscriberIf.hpp:46
std::function< void(const DataPointPath &, const MetaData &)> NotifyCallbackType
Definition repositorySubscriberIf.hpp:48
size_t SubscriptionId
Definition repositorySubscriberIf.hpp:51
Definition commandReplier.cpp:22
elt::mal::future< std::string > InjectReqRepEvent(StateMachineEngine &engine)
Definition malEventInjector.hpp:23
uint64_t RtcUInt64
Definition repositoryIf.hpp:61
RepositorySubscriberIf::CallbackType cb_created
Definition repositorySubscriberIf.ipp:36
RepositorySubscriberIf::SubscriptionId id
Definition repositorySubscriberIf.ipp:35
RepositorySubscriberIf::ErrorCallbackType cb_error
Definition repositorySubscriberIf.ipp:38
RepositorySubscriberIf::CallbackType cb_deleted
Definition repositorySubscriberIf.ipp:37
Definition repositorySubscriberIf.ipp:41
RepositorySubscriberIf::SubscriptionId id
Definition repositorySubscriberIf.ipp:42
Definition repositorySubscriberIf.ipp:27
RepositorySubscriberIf::ErrorCallbackType cb_error
Definition repositorySubscriberIf.ipp:31
RepositorySubscriberIf::SubscriptionId id
Definition repositorySubscriberIf.ipp:28
RepositorySubscriberIf::NotifyCallbackType cb_value
Definition repositorySubscriberIf.ipp:30
DataPointPath path
Definition repositorySubscriberIf.ipp:29