PLCnext API Documentation  20.6.0.30321
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  NotificationNameId()
96 {
97  this->Initialize();
98 }
99 
100 
101 template<typename PayloadType>
104  : base_type(std::move(other)),
105  NotificationSending(std::move(other.NotificationSending)),
106  NotificationNameId(std::move(other.NotificationNameId))
107 {
108  other.NotificationSending = nullptr;
109  other.severity = Severity::Default;
110 }
111 
112 
113 template<typename PayloadType>
115 {
116  this->Dispose();
117 }
118 
119 
120 template<typename PayloadType>
123 {
124  if (this == &other)
125  {
126  return *this;
127  }
128 
129  base_type::operator=(std::move(other));
130 
131  this->NotificationSending = std::move(other.NotificationSending);
132  other.NotificationSending = nullptr;
133 
134  this->NotificationNameId = std::move(other.NotificationNameId);
135 
136  return *this;
137 }
138 
139 
140 template<typename PayloadType>
142 {
143  if (this->NotificationSending != nullptr && !this->NotificationNameId.IsValid())
144  {
145  this->NotificationNameId = this->NotificationSending->RegisterNotification(
146  this->NotificationName, this->SenderName, this->severity, this->GetPayloadTypeId());
147  }
148 }
149 
150 
151 template<typename PayloadType>
153 {
154  if (this->NotificationSending != nullptr && this->NotificationNameId.IsValid())
155  {
156  this->NotificationSending->UnregisterNotification(this->NotificationNameId);
157  this->NotificationNameId = NotificationNameIdType();
158  }
159 }
160 
161 
162 template<typename PayloadType>
163 template<typename... Args>
165 {
166  return this->SendNotificationWithTimestamp(DateTime::Now(), std::forward<Args>(args)...);
167 }
168 
169 
170 template<typename PayloadType>
171 template<typename... Args>
173  const DateTime& timestamp, Args&& ... args)
174 {
175  if (this->NotificationSending == nullptr)
176  {
177  return NotificationIdType();
178  }
179  if (!this->NotificationNameId.IsValid())
180  {
181  return NotificationIdType();
182  }
183  return this->NotificationSending->SendNotification(
184  this->NotificationNameId, timestamp, PayloadType{std::forward<Args>(args)...});
185 }
186 
187 
188 template<class PayloadType>
190 {
191  return this->NotificationNameId;
192 }
193 
194 
195 }}} // namespace Arp::System::Nm
NotificationNameIdType GetNotificationNameId() const override
Returns the NotificationNameId
Definition: NotificationRegistration.hpp:189
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:43
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:172
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:164
virtual NotificationIdType SendNotification(NotificationNameIdType notificationNameId, Arp::DateTime timestamp, const RawPayloadType &payload)=0
Sends a notification