PLCnext API Documentation  22.9.0.33
NotificationRegistration.hpp
1 //
3 // Copyright PHOENIX CONTACT Electronics GmbH
4 //
6 
7 #pragma once
8 
9 #include "Arp/System/Nm/INotificationSending.hpp"
10 #include "Arp/System/Nm/NotificationRegistrationBase.hpp"
11 
12 namespace Arp { namespace System { namespace Nm
13 {
14 
15 
28 template<typename PayloadType>
30  : public NotificationRegistrationBase<NotificationRegistration<PayloadType>>
31 {
32 private:
34 
35 public:
36  using payload_type = PayloadType;
37 
42 
49  NotificationRegistration(const String& notificationName, const String& senderName,
50  Severity severity, INotificationSending& notificationSending);
51 
54  ~NotificationRegistration() override;
55 
56  NotificationRegistration& operator=(const NotificationRegistration&) = delete;
58 
59 public:
64  template<typename... Args>
65  NotificationIdType SendNotification(Args&& ... args);
66 
71  template<typename... Args>
72  NotificationIdType SendNotificationWithTimestamp(const DateTime& timestamp, Args&& ... args);
73 
76 
77 private:
78  void Initialize();
79  void DisposeImpl() override;
80 
81 
82 private: // fields
83  INotificationSending* NotificationSending = nullptr;
84  NotificationNameIdType NotificationNameId;
85 };
86 
87 
88 template<typename PayloadType>
90  const String& senderName, Severity severity, INotificationSending& notificationSending)
91  : base_type(notificationName, senderName, severity),
92  NotificationSending(&notificationSending)
93 {
94  this->Initialize();
95 }
96 
97 
98 template<typename PayloadType>
101  : base_type(std::move(other)),
102  NotificationSending(std::exchange(other.NotificationSending, nullptr)),
103  NotificationNameId(std::exchange(other.NotificationNameId, NotificationNameIdType()))
104 {
105 }
106 
107 
108 template<typename PayloadType>
109 NotificationRegistration<PayloadType>::~NotificationRegistration()
110 {
111  this->Dispose();
112 }
113 
114 
115 template<typename PayloadType>
116 NotificationRegistration<PayloadType>& NotificationRegistration<PayloadType>::operator=(
117  NotificationRegistration<PayloadType>&& other)
118 {
119  if (this == &other)
120  {
121  return *this;
122  }
123 
124  base_type::operator=(std::move(other));
125 
126  this->NotificationSending = std::exchange(other.NotificationSending, nullptr);
127  this->NotificationNameId = std::exchange(other.NotificationNameId, NotificationNameIdType());
128 
129  return *this;
130 }
131 
132 
133 template<typename PayloadType>
134 void NotificationRegistration<PayloadType>::Initialize()
135 {
136  if ((this->NotificationSending != nullptr) && (!this->NotificationNameId.IsValid()))
137  {
138  this->NotificationNameId = this->NotificationSending->RegisterNotification(
139  this->NotificationName, this->SenderName, this->severity, this->GetPayloadTypeId());
140  }
141 }
142 
143 
144 template<typename PayloadType>
145 void NotificationRegistration<PayloadType>::DisposeImpl()
146 {
147  if ((this->NotificationSending != nullptr) && (this->NotificationNameId.IsValid()))
148  {
149  this->NotificationSending->UnregisterNotification(this->NotificationNameId);
150  this->NotificationNameId = NotificationNameIdType();
151  }
152 }
153 
154 
155 template<typename PayloadType>
156 template<typename... Args>
158 {
159  return this->SendNotificationWithTimestamp(DateTime::Now(), std::forward<Args>(args)...);
160 }
161 
162 
163 template<typename PayloadType>
164 template<typename... Args>
166  const DateTime& timestamp, Args&& ... args)
167 {
168  if (this->NotificationSending == nullptr)
169  {
170  return NotificationIdType();
171  }
172  if (!this->NotificationNameId.IsValid())
173  {
174  return NotificationIdType();
175  }
176  return this->NotificationSending->SendNotification(
177  this->NotificationNameId, timestamp, PayloadType{std::forward<Args>(args)...});
178 }
179 
180 
181 template<class PayloadType>
183 {
184  return this->NotificationNameId;
185 }
186 
187 
188 }}} // namespace Arp::System::Nm
The class contains date and time informations.
Definition: DateTime.hpp:45
static DateTime Now(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:22
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:165
NotificationRegistration()=default
Creates an empty NotificationRegistration
NotificationIdType SendNotification(Args &&... args)
Sends a notification
Definition: NotificationRegistration.hpp:157
@ System
System components used by the System, Device, Plc or Io domains.
IdType< Arp::uint64, struct NotificationIdTag > NotificationIdType
type for Notification ids
Definition: NotificationManagerTypes.hpp:19
Severity
Enumeration of Severities for notifications
Definition: Severity.hpp:15
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