PLCnext API Documentation  21.0.0.35466
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 
41  NotificationRegistration() = default;
42 
49  NotificationRegistration(const String& notificationName, const String& senderName,
50  Severity severity, INotificationSending& notificationSending);
51 
53  ~NotificationRegistration() override;
54 
56 
58  NotificationRegistration& operator=(const NotificationRegistration&) = delete;
59 
60 
61 public:
66  template<typename... Args>
67  NotificationIdType SendNotification(Args&& ... args);
68 
73  template<typename... Args>
74  NotificationIdType SendNotificationWithTimestamp(const DateTime& timestamp, Args&& ... args);
75 
78 
79 private:
80  void Initialize();
81  void DisposeImpl() override;
82 
83 
84 private: // fields
85  INotificationSending* NotificationSending = nullptr;
86  NotificationNameIdType NotificationNameId;
87 };
88 
89 
90 template<typename PayloadType>
92  const String& senderName, Severity severity, INotificationSending& notificationSending)
93  : base_type(notificationName, senderName, severity),
94  NotificationSending(&notificationSending)
95 {
96  this->Initialize();
97 }
98 
99 
100 template<typename PayloadType>
103  : base_type(std::move(other)),
104  NotificationSending(std::move(other.NotificationSending)),
105  NotificationNameId(std::move(other.NotificationNameId))
106 {
107  other.NotificationSending = nullptr;
108  other.severity = Severity::Default;
109 }
110 
111 
112 template<typename PayloadType>
114 {
115  this->Dispose();
116 }
117 
118 
119 template<typename PayloadType>
122 {
123  if (this == &other)
124  {
125  return *this;
126  }
127 
128  base_type::operator=(std::move(other));
129 
130  this->NotificationSending = std::move(other.NotificationSending);
131  other.NotificationSending = nullptr;
132 
133  this->NotificationNameId = std::move(other.NotificationNameId);
134 
135  return *this;
136 }
137 
138 
139 template<typename PayloadType>
141 {
142  if (this->NotificationSending != nullptr && !this->NotificationNameId.IsValid())
143  {
144  this->NotificationNameId = this->NotificationSending->RegisterNotification(
145  this->NotificationName, this->SenderName, this->severity, this->GetPayloadTypeId());
146  }
147 }
148 
149 
150 template<typename PayloadType>
152 {
153  if (this->NotificationSending != nullptr && this->NotificationNameId.IsValid())
154  {
155  this->NotificationSending->UnregisterNotification(this->NotificationNameId);
156  this->NotificationNameId = NotificationNameIdType();
157  }
158 }
159 
160 
161 template<typename PayloadType>
162 template<typename... Args>
164 {
165  return this->SendNotificationWithTimestamp(DateTime::Now(), std::forward<Args>(args)...);
166 }
167 
168 
169 template<typename PayloadType>
170 template<typename... Args>
172  const DateTime& timestamp, Args&& ... args)
173 {
174  if (this->NotificationSending == nullptr)
175  {
176  return NotificationIdType();
177  }
178  if (!this->NotificationNameId.IsValid())
179  {
180  return NotificationIdType();
181  }
182  return this->NotificationSending->SendNotification(
183  this->NotificationNameId, timestamp, PayloadType{std::forward<Args>(args)...});
184 }
185 
186 
187 template<class PayloadType>
189 {
190  return this->NotificationNameId;
191 }
192 
193 
194 }}} // namespace Arp::System::Nm
NotificationNameIdType GetNotificationNameId() const override
Returns the NotificationNameId
Definition: NotificationRegistration.hpp:188
NotificationRegistration()=default
Creates an empty NotificationRegistration
IdType< Arp::uint64, struct NotificationIdTag > NotificationIdType
type for Notification ids
Definition: NotificationManagerTypes.hpp:19
void Dispose() noexcept
Unregisters the Notification and release all resources
Definition: NotificationRegistrationBase.hpp:78
virtual void UnregisterNotification(NotificationNameIdType notificationNameId)=0
Unregisters a Nototification
virtual NotificationNameIdType RegisterNotification(const String &notificationName, const String &senderName, Severity severity, PayloadTypeIdType payloadTypeId)=0
Registers a new Notification
The class contains date and time informations.
Definition: DateTime.hpp:44
constexpr bool IsValid() const noexcept
Returns true if this object represents a valid id (!= 0)
Definition: IdType.hpp:87
Interface for sending of notifications
Definition: INotificationSending.hpp:20
static DateTime Now(void)
Gets the current time as DateTime, expressed as the UTC time.
Severity
Enumeration of Severities for notifications
Definition: Severity.hpp:15
Root namespace for the PLCnext API
Base class with common behavior of NotificationRegistration and NonBlockingNotificationRegistration
Definition: NotificationRegistrationBase.hpp:19
Proxy object for a NotificationRegistration
Definition: NotificationRegistration.hpp:29
NotificationIdType SendNotificationWithTimestamp(const DateTime &timestamp, Args &&... args)
Sends a notification with a specified timestamp
Definition: NotificationRegistration.hpp:171
PayloadTypeIdType GetPayloadTypeId() const
Returns the PayloadTypeId
Definition: NotificationRegistrationBase.hpp:119
IdType< Arp::uint32, struct NotificationNameIdTag > NotificationNameIdType
type for Notification name ids
Definition: NotificationManagerTypes.hpp:22
System components used by the System, Device, Plc or Io domains.
NotificationIdType SendNotification(Args &&... args)
Sends a notification
Definition: NotificationRegistration.hpp:163
virtual NotificationIdType SendNotification(NotificationNameIdType notificationNameId, Arp::DateTime timestamp, const RawPayloadType &payload)=0
Sends a notification