PLCnext API Documentation 24.0.0.71
NotificationRegistration.hpp
1
2//
3// Copyright PHOENIX CONTACT Electronics GmbH
4//
6
7#pragma once
8
10#include "Arp/System/Nm/INotificationSending.hpp"
11#include "Arp/System/Nm/NotificationRegistrationBase.hpp"
12
13namespace Arp { namespace System { namespace Nm
14{
15
16
29template<typename PayloadType>
30class ARP_CXX_SYMBOL_EXPORT NotificationRegistration
31 : public NotificationRegistrationBase<NotificationRegistration<PayloadType>>
32{
33private:
35
36public:
37 using payload_type = PayloadType;
38
43
50 NotificationRegistration(const String& notificationName, const String& senderName,
51 Severity severity, INotificationSending& notificationSending);
52
56
57 NotificationRegistration& operator=(const NotificationRegistration&) = delete;
59
60public:
65 template<typename... Args>
66 NotificationIdType SendNotification(Args&& ... args);
67
72 template<typename... Args>
73 NotificationIdType SendNotificationWithTimestamp(const DateTime& timestamp, Args&& ... args);
74
76 NotificationNameIdType GetNotificationNameId() const override;
77
78private:
79 void Initialize();
80 void DisposeImpl() override;
81
82
83private: // fields
84 INotificationSending* NotificationSending = nullptr;
85 NotificationNameIdType NotificationNameId;
86};
87
88
89template<typename PayloadType>
91 const String& senderName, Severity severity, INotificationSending& notificationSending)
92 : base_type(notificationName, senderName, severity),
93 NotificationSending(&notificationSending)
94{
95 this->Initialize();
96}
97
98
99template<typename PayloadType>
102 : base_type(std::move(other)),
103 NotificationSending(std::exchange(other.NotificationSending, nullptr)),
104 NotificationNameId(std::exchange(other.NotificationNameId, NotificationNameIdType()))
105{
106}
107
108
109template<typename PayloadType>
110NotificationRegistration<PayloadType>::~NotificationRegistration()
111{
112 this->Dispose();
113}
114
115
116template<typename PayloadType>
117NotificationRegistration<PayloadType>& NotificationRegistration<PayloadType>::operator=(
118 NotificationRegistration<PayloadType>&& other)
119{
120 if (this == &other)
121 {
122 return *this;
123 }
124
125 base_type::operator=(std::move(other));
126
127 this->NotificationSending = std::exchange(other.NotificationSending, nullptr);
128 this->NotificationNameId = std::exchange(other.NotificationNameId, NotificationNameIdType());
129
130 return *this;
131}
132
133
134template<typename PayloadType>
135void NotificationRegistration<PayloadType>::Initialize()
136{
137 if ((this->NotificationSending != nullptr) && (!this->NotificationNameId.IsValid()))
138 {
139 this->NotificationNameId = this->NotificationSending->RegisterNotification(
140 this->NotificationName, this->SenderName, this->severity, this->GetPayloadTypeId());
141 }
142}
143
144
145template<typename PayloadType>
146void NotificationRegistration<PayloadType>::DisposeImpl()
147{
148 if ((this->NotificationSending != nullptr) && (this->NotificationNameId.IsValid()))
149 {
150 this->NotificationSending->UnregisterNotification(this->NotificationNameId);
151 this->NotificationNameId = NotificationNameIdType();
152 }
153}
154
155
156template<typename PayloadType>
157template<typename... Args>
159{
160 return this->SendNotificationWithTimestamp(DateTime::GetUtcNow(), std::forward<Args>(args)...);
161}
162
163
164template<typename PayloadType>
165template<typename... Args>
167 const DateTime& timestamp, Args&& ... args)
168{
169 if (this->NotificationSending == nullptr)
170 {
171 return NotificationIdType();
172 }
173 if (!this->NotificationNameId.IsValid())
174 {
175 return NotificationIdType();
176 }
177 return this->NotificationSending->SendNotification(
178 this->NotificationNameId, timestamp, PayloadType{std::forward<Args>(args)...});
179}
180
181
182template<class PayloadType>
184{
185 return this->NotificationNameId;
186}
187
188
189}}} // namespace Arp::System::Nm
The class contains date and time informations.
Definition: DateTime.hpp:46
static DateTime GetUtcNow(void)
Gets the current time as DateTime, expressed as the UTC time.
Interface for sending of notifications
Definition: INotificationSending.hpp:21
Base class with common behavior of NotificationRegistration and NonBlockingNotificationRegistration
Definition: NotificationRegistrationBase.hpp:24
Proxy object for a NotificationRegistration
Definition: NotificationRegistration.hpp:32
NotificationNameIdType GetNotificationNameId() const override
Returns the NotificationNameId
Definition: NotificationRegistration.hpp:183
NotificationIdType SendNotificationWithTimestamp(const DateTime &timestamp, Args &&... args)
Sends a notification with a specified timestamp
Definition: NotificationRegistration.hpp:166
NotificationRegistration()=default
Creates an empty NotificationRegistration
NotificationIdType SendNotification(Args &&... args)
Sends a notification
Definition: NotificationRegistration.hpp:158
@ System
System components used by the System, Device, Plc or Io domains.
enum ARP_CXX_SYMBOL_EXPORT Severity
Enumeration of Severities for notifications
Definition: Severity.hpp:14
IdType< Arp::uint64, struct NotificationIdTag > NotificationIdType
type for Notification ids
Definition: NotificationManagerTypes.hpp:19
IdType< Arp::uint32, struct NotificationNameIdTag > NotificationNameIdType
type for Notification name ids
Definition: NotificationManagerTypes.hpp:22
Root namespace for the PLCnext API
Namespace of the C++ standard library