PLCnext API Documentation 25.0.2.69
NotificationRegistration.hpp
1
2//
3// Copyright Phoenix Contact GmbH & Co. KG
4//
6#pragma once
7
9#include "Arp/System/Nm/INotificationSending.hpp"
10#include "Arp/System/Nm/NotificationRegistrationBase.hpp"
11
12namespace Arp { namespace System { namespace Nm
13{
14
15
28template<typename PayloadType>
29class ARP_CXX_SYMBOL_EXPORT NotificationRegistration
30 : public NotificationRegistrationBase<NotificationRegistration<PayloadType>>
31{
32private:
34
35public:
36 using payload_type = PayloadType;
37
42
43 NotificationRegistration(const String& notificationName, const String& senderName,
44 Severity severity, INotificationSending& notificationSending);
45
49
50 NotificationRegistration& operator=(const NotificationRegistration&) = delete;
52
53public:
54 template<typename... Args>
55 NotificationIdType SendNotification(Args&& ... args);
56
57 template<typename... Args>
58 NotificationIdType SendNotificationWithTimestamp(const DateTime& timestamp, Args&& ... args);
59
60 NotificationNameIdType GetNotificationNameId() const override;
61
62private:
63 void Initialize();
64 void DisposeImpl() override;
65
66
67private: // fields
68 INotificationSending* NotificationSending = nullptr;
69 NotificationNameIdType NotificationNameId;
70};
71
72
79template<typename PayloadType>
81 const String& senderName, Severity severity, INotificationSending& notificationSending)
82 : base_type(notificationName, senderName, severity),
83 NotificationSending(&notificationSending)
84{
85 this->Initialize();
86}
87
88
89template<typename PayloadType>
92 : base_type(std::move(other)),
93 NotificationSending(std::exchange(other.NotificationSending, nullptr)),
94 NotificationNameId(std::exchange(other.NotificationNameId, NotificationNameIdType()))
95{
96}
97
98
99template<typename PayloadType>
101{
102 this->Dispose();
103}
104
105
106template<typename PayloadType>
109{
110 if (this == &other)
111 {
112 return *this;
113 }
114
115 base_type::operator=(std::move(other));
116
117 this->NotificationSending = std::exchange(other.NotificationSending, nullptr);
118 this->NotificationNameId = std::exchange(other.NotificationNameId, NotificationNameIdType());
119
120 return *this;
121}
122
123
124template<typename PayloadType>
125void NotificationRegistration<PayloadType>::Initialize()
126{
127 if ((this->NotificationSending != nullptr) && (!this->NotificationNameId.IsValid()))
128 {
129 this->NotificationNameId = this->NotificationSending->RegisterNotification(
130 this->NotificationName, this->SenderName, this->severity, this->GetPayloadTypeId());
131 }
132}
133
134
135template<typename PayloadType>
136void NotificationRegistration<PayloadType>::DisposeImpl()
137{
138 if ((this->NotificationSending != nullptr) && (this->NotificationNameId.IsValid()))
139 {
140 this->NotificationSending->UnregisterNotification(this->NotificationNameId);
141 this->NotificationNameId = NotificationNameIdType();
142 }
143}
144
145
150template<typename PayloadType>
151template<typename... Args>
153{
154 return this->SendNotificationWithTimestamp(DateTime::GetUtcNow(), std::forward<Args>(args)...);
155}
156
157
162template<typename PayloadType>
163template<typename... Args>
165 const DateTime& timestamp, Args&& ... args)
166{
167 if (this->NotificationSending == nullptr)
168 {
169 return NotificationIdType();
170 }
171 if (!this->NotificationNameId.IsValid())
172 {
173 return NotificationIdType();
174 }
175 return this->NotificationSending->SendNotification(
176 this->NotificationNameId, timestamp, PayloadType{std::forward<Args>(args)...});
177}
178
179
181template<class PayloadType>
183{
184 return this->NotificationNameId;
185}
186
187
188}}} // 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
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:31
NotificationNameIdType GetNotificationNameId() const override
Returns the NotificationNameId
Definition: NotificationRegistration.hpp:182
NotificationIdType SendNotificationWithTimestamp(const DateTime &timestamp, Args &&... args)
Sends a notification with a specified timestamp
Definition: NotificationRegistration.hpp:164
NotificationRegistration()=default
Creates an empty NotificationRegistration
NotificationIdType SendNotification(Args &&... args)
Sends a notification
Definition: NotificationRegistration.hpp:152
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:17
IdType< Arp::uint32, struct NotificationNameIdTag > NotificationNameIdType
type for Notification name ids
Definition: NotificationManagerTypes.hpp:20
Root namespace for the PLCnext API
Namespace of the C++ standard library