PLCnext API Documentation 25.0.2.69
NonBlockingNotificationRegistration3.hpp
1
2//
3// Copyright Phoenix Contact GmbH & Co. KG
4//
6#pragma once
8#include "Arp/System/Commons/Threading/LockGuard.hpp"
9#include "Arp/System/Commons/Threading/Mutex.hpp"
10#include "Arp/System/Nm/NonBlockingNotificationRegistration3ArgumentsBuffer.hpp"
11#include "Arp/System/Nm/NonBlockingNotificationRegistration3Base.hpp"
12#include "Arp/System/Nm/NonBlockingNotificationSendingAdapter.hpp"
13#include "Arp/System/Nm/NotificationRegistrationBase.hpp"
14#include <limits>
15#include <numeric>
16namespace Arp { namespace System { namespace Nm
17{
18
19class NotificationManager;
20
50template<typename PayloadType, typename ArgumentsType = typename PayloadType::ArgumentsType>
51class ARP_CXX_SYMBOL_EXPORT NonBlockingNotificationRegistration3
52 : public NotificationRegistrationBase<NonBlockingNotificationRegistration3<PayloadType, ArgumentsType>>
54{
55 friend class NotificationManager; // to call private constructor
56
57private:
59
60public:
61 using payload_type = PayloadType;
62 using arguments_type = ArgumentsType;
63
64 static_assert(std::is_constructible<PayloadType, typename std::add_const<ArgumentsType>::type>::value,
65 "PayloadType must be constructible by const ArgumentsType!");
66 static_assert(std::is_default_constructible<ArgumentsType>::value, "ArgumentType must be default constructible");
67 static_assert(std::is_copy_assignable<ArgumentsType>::value, "ArgumentsType must be copy assignable");
68
69private:
72 NonBlockingNotificationRegistration3(const String& notificationName, const String& senderName, Severity severity,
73 size_t argumentsBufferCapacity, std::unique_ptr<NonBlockingNotificationSendingAdapter> adapter);
74
75public:
80
84
87
92 template<typename... Args>
93 Future<NotificationIdType> SendNotification(Args&& ... args);
94
99 template<typename... Args>
100 Future<NotificationIdType> SendNotificationWithTimestamp(const DateTime& timestamp, Args&& ... args);
101
103 NotificationNameIdType GetNotificationNameId() const override;
104
105private:
106 void DisposeImpl() override;
107
109 static RawPayloadType CreatePayload(CreatePayloadFunctorArg arg);
110
111protected:
113
114private:
115 // shared ownership is required here
116 std::shared_ptr<ArgumentsBuffer> argumentsBuffer = nullptr;
117};
118
119template<typename PayloadType, typename ArgumentsType>
121 const String& notificationName, const String& senderName, Severity severity, size_t argumentsBufferCapacity,
122 std::unique_ptr<NonBlockingNotificationSendingAdapter> adapter)
123 : base_type(notificationName, senderName, severity),
125 argumentsBuffer(std::make_shared<ArgumentsBuffer>(argumentsBufferCapacity))
126{
127 this->Register(notificationName, senderName, severity, this->GetPayloadTypeId());
128}
129
130template<typename PayloadType, typename ArgumentsType>
133 : base_type(std::move(other)),
135 argumentsBuffer(std::move(other.argumentsBuffer))
136{
137}
138
139template<typename PayloadType, typename ArgumentsType>
141{
142 this->Dispose();
143}
144
145template<typename PayloadType, typename ArgumentsType>
146NonBlockingNotificationRegistration3<PayloadType, ArgumentsType>&
147NonBlockingNotificationRegistration3<PayloadType, ArgumentsType>::operator=(
148 NonBlockingNotificationRegistration3<PayloadType, ArgumentsType>&& other) noexcept
149{
150 if (this == &other)
151 {
152 return *this;
153 }
154
155 base_type::operator=(std::move(other));
156 NonBlockingNotificationRegistration3Base::operator=(std::move(other));
157
158 this->argumentsBuffer = std::move(other.argumentsBuffer);
159
160 return *this;
161}
162
163template<typename PayloadType, typename ArgumentsType>
164void NonBlockingNotificationRegistration3<PayloadType, ArgumentsType>::DisposeImpl()
165{
166 this->Unregister();
167}
168
169template<typename PayloadType, typename ArgumentsType>
170template<typename... Args>
172 Args&& ... args)
173{
174 return this->SendNotificationWithTimestamp(DateTime::GetUtcNow(), std::forward<Args>(args)...);
175}
176
177template<typename PayloadType, typename ArgumentsType>
178template<typename... Args>
181 const DateTime& timestamp, Args&& ... args)
182{
183 static_assert(std::is_constructible<ArgumentsType, Args...>::value,
184 "The ArgumentsType must be constructible by the passed arguments (Args).");
185
186 if (this->IsReadyToSend())
187 {
188 auto insertResult = this->argumentsBuffer->Insert(ArgumentsType{std::forward<Args>(args)...});
189 this->SendNotificationInternal(
190 timestamp, this->argumentsBuffer, insertResult, &NonBlockingNotificationRegistration3::CreatePayload);
191 }
192 else
193 {
194 this->ResetFutureNotificationId();
195 }
196 return this->GetFutureNotificationId();
197}
198
199template<typename PayloadType, typename ArgumentsType>
201{
202 return this->GetNotificationNameIdInternal();
203}
204
205template<typename PayloadType, typename ArgumentsType>
207 CreatePayloadFunctorArg arg)
208{
209 auto buffer = std::dynamic_pointer_cast<ArgumentsBuffer>(arg.BufferPtr);
210 if (!buffer)
211 {
212 return {};
213 }
214
215 auto removeResult = buffer->Remove(arg.Index);
216 if (!removeResult.IsValid)
217 {
218 return {};
219 }
220
221 return PayloadType{removeResult.Args}.MoveOutRawPayload();
222}
223
224}}} // namespace Arp::System::Nm
This class contains date and time informations.
Definition: DateTime.hpp:27
static DateTime GetUtcNow(void)
Gets the current time in UTC.
Definition: DateTime.cpp:186
This class represents the Arp String. The implementation is based on std::string.
Definition: String.hpp:39
Future object as proxy for return value an asynchronous function call
Definition: Future.hpp:68
Definition: NonBlockingNotificationRegistration3ArgumentsBuffer.hpp:21
Internal class for pimpl pattern
Definition: NonBlockingNotificationRegistration3Base.hpp:20
Proxy object for a non-blocking NotificationRegistration
Definition: NonBlockingNotificationRegistration3.hpp:54
Future< NotificationIdType > SendNotification(Args &&... args)
Sends a notification
Definition: NonBlockingNotificationRegistration3.hpp:171
NonBlockingNotificationRegistration3()=default
Creates an empty NotificationRegistration
NotificationNameIdType GetNotificationNameId() const override
Returns the NotificationNameId
Definition: NonBlockingNotificationRegistration3.hpp:200
Future< NotificationIdType > SendNotificationWithTimestamp(const DateTime &timestamp, Args &&... args)
Sends a notification with a specified timestamp
Definition: NonBlockingNotificationRegistration3.hpp:180
Primary access to the NotificationManager
Definition: NotificationManager.hpp:48
Base class with common behavior of NotificationRegistration and NonBlockingNotificationRegistration
Definition: NotificationRegistrationBase.hpp:24
PayloadTypeIdType GetPayloadTypeId() const
Returns the PayloadTypeId
Definition: NotificationRegistrationBase.hpp:119
enum ARP_CXX_SYMBOL_EXPORT Severity
Enumeration of Severities for notifications
Definition: Severity.hpp:14
std::vector< Arp::Base::Rsc::Commons::RscVariant< RawPayloadTypeLength > > RawPayloadType
type for the internally transferred payloads
Definition: NotificationManagerTypes.hpp:32
Root namespace for the PLCnext API
Namespace of the C++ standard library
Definition: NonBlockingNotificationSendingAdapter.hpp:29