PLCnext API Documentation  20.3.1.28622
SpecializedPayload.hpp
1 //
3 // Copyright PHOENIX CONTACT Electronics GmbH
4 //
6 
7 #pragma once
8 
9 #include "Arp/System/Core/TypeName.hxx"
10 #include "Arp/System/Commons/Exceptions/ArgumentException.hpp"
11 #include "Arp/System/Nm/GenericPayload.hpp"
12 #include "Arp/System/Nm/IPayload.hpp"
13 #include "Arp/System/Nm/Notification.hpp"
14 #include "Arp/System/Nm/NotificationManager.hpp"
15 #include "Arp/System/Nm/RscVariantHelpers.hpp"
16 #include <type_traits>
17 
18 namespace Arp { namespace System { namespace Nm
19 {
20 
21 
22 class LongStringPayloadField;
23 
24 
41 template<typename PayloadType>
43 {
44  friend class LongStringPayloadField;
45 
46 public:
47  using ValueType = RawPayloadType::value_type;
49 
52  explicit SpecializedPayload(const Notification& notification);
53 
56  explicit SpecializedPayload(const GenericPayload& payloadArg);
57 
58 protected:
61  explicit SpecializedPayload(const String& formatString);
62 
63 public:
64  virtual ~SpecializedPayload() = 0;
65 
69 
72  static const String GetPayloadTypeName();
73 
74  // Methods of IPayload
75  PayloadTypeIdType GetId() const final;
76  const String GetName() const final;
77  const RawPayloadType& GetAsRawPayloadType() const final;
78  const String ToString() const override;
79 
80  RawPayloadType&& MoveOutRawPayload();
81 
83  bool IsReadOnly() const;
84 
85 protected: // operations
89  void SetFormatString(const String& formatString);
90 
91 
93  static size_t FieldIndexToFormatStringIndex(size_t fieldIndex);
94 
95  // The second template parameter of the following templates is used to conditionally
96  // enable the corresponding member function based on the type T.
97  // There is one variant for Enums (handeled as strings) and other types.
98  // Since GetRscType<String>() != RscVariant(String()).GetType() there is a
99  // special case for AddField<String>.
100 
104  template < typename T, typename std::enable_if < !std::is_same<T, String>::value&&
105  !std::is_enum<T>::value >::type* = nullptr >
106  size_t AddField()
107  {
108  return this->payload.AddField(Arp::System::Rsc::Services::GetRscType<T>());
109  }
110 
111 
117  template < typename T, typename std::enable_if < std::is_same<T, String>::value ||
118  std::is_enum<T>::value >::type* = nullptr >
119  size_t AddField()
120  {
122  }
123 
124 
128  template < typename T, typename std::enable_if < !std::is_enum<T>::value >::type* = nullptr >
129  void SetFieldValue(size_t fieldIndex, const T& value)
130  {
131  this->payload.SetFieldValue(fieldIndex, ValueType(value));
132  }
133 
139  template<typename T, typename std::enable_if<std::is_enum<T>::value>::type* = nullptr>
140  void SetFieldValue(size_t fieldIndex, T value)
141  {
142  try
143  {
144  this->payload.SetFieldValue(fieldIndex, Enum<T>(value).ToString());
145  }
146  catch (const std::out_of_range&)
147  {
148  throw Arp::System::Commons::ArgumentException::Create("value", static_cast<int>(value),
149  String::Format(
150  "The supplied value is not a enumerated value of {}", TypeName<T>().Value)
151  .CStr());
152  }
153  }
154 
155 
159  template < typename T, typename std::enable_if < !std::is_enum<T>::value >::type* = nullptr >
160  const T GetFieldValueAs(size_t fieldIndex) const
161  {
162  const ValueType& value = this->payload.GetFieldValue(fieldIndex);
163  if (value.GetType() == Rsc::Services::RscType::None)
164  {
165  return T();
166  }
167  return rscvariant_cast<T>(value);
168  }
169 
175  template<typename T, typename std::enable_if<std::is_enum<T>::value>::type* = nullptr>
176  const T GetFieldValueAs(size_t fieldIndex) const
177  {
178  return Enum<T>::Parse(this->GetFieldValueAs<String>(fieldIndex));
179  }
180 
181 private:
182  GenericPayload payload;
183 };
184 
185 
188 {
189  static INotificationManagerInfo& GetINotificationManagerInfo();
190 
191  template<typename PayloadType>
192  friend class SpecializedPayload;
193 };
194 
195 
196 template<typename PayloadType>
198  : payload(GetId(), GetName())
199 {
200  this->payload.SetFieldValue(this->payload.GetFieldIndexFormatString(), formatString);
201 }
202 
203 
204 template<typename PayloadType>
206  : payload(GetId(), GetName(), notification)
207 {
208 }
209 
210 
211 template<typename PayloadType>
213  : payload(GetId(), GetName(), payloadArg.GetAsRawPayloadType())
214 {
215 }
216 
217 
218 template<typename PayloadType>
220 
221 
222 template<typename PayloadType>
224 {
225  static PayloadTypeIdType id;
226  if (!id.IsValid())
227  {
228  id = SpecializedPayloadHelper::GetINotificationManagerInfo().GetPayloadTypeId(
229  GetPayloadTypeName());
230  }
231  return id;
232 }
233 
234 
235 template<typename PayloadType>
237 {
238  return GetPayloadTypeId();
239 }
240 
241 
242 template<typename PayloadType>
244 {
245  return TypeName<PayloadType>().Value;
246 }
247 
248 
249 template<typename PayloadType>
251 {
252  return GetPayloadTypeName();
253 }
254 
255 
256 template<typename PayloadType>
258 {
259  return this->payload.GetAsRawPayloadType();
260 }
261 
262 
263 template<typename PayloadType>
265 {
266  return this->payload.ToString();
267 }
268 
269 template<typename PayloadType>
271 {
272  return this->payload.MoveOutRawPayload();
273 }
274 
275 template<typename PayloadType>
277 {
278  return this->payload.IsReadOnly();
279 }
280 
281 
282 template<typename PayloadType>
284 {
285  this->payload.SetFieldValue(this->payload.GetFieldIndexFormatString(), formatString);
286 }
287 
288 
289 template<typename PayloadType>
291 {
292  return fieldIndex - 1;
293 }
294 
295 
296 }}} // end of namespace Arp::System::Nm
static ArgumentException Create(const char *paramName, const T &paramValue)
Creates an ArgumentException instance using a default message text.
Definition: ArgumentException.hpp:112
const RawPayloadType & GetAsRawPayloadType() const final
Returns a reference to the raw payload object
Definition: SpecializedPayload.hpp:257
String with undefined format. Deprecated with remoting version 4 used by Rsc.
This (meta programming) class provides the C++ typename of the as template argument passed type...
Definition: TypeName.hxx:55
const String GetName() const final
Returns the PayloadTypeName
Definition: SpecializedPayload.hpp:250
Contains meta data and paylod of a Notification
Definition: Notification.hpp:21
void SetFieldValue(size_t fieldIndex, const T &value)
Sets the value of a payload field
Definition: SpecializedPayload.hpp:129
PayloadTypeIdType GetId() const final
Returns the PayloadTypeId
Definition: SpecializedPayload.hpp:236
Interface for Paylo objects
Definition: IPayload.hpp:18
static Enum Parse(const char *input)
Parses the given input string.
Definition: Enum.hxx:190
size_t AddField(FieldType type)
Adds a field with the given FieldType
void SetFieldValue(size_t fieldIndex, T value)
Sets the value of a payload field
Definition: SpecializedPayload.hpp:140
void SetFormatString(const String &formatString)
summary>Retuns the index in the format string for a given payload field
Definition: SpecializedPayload.hpp:283
SpecializedPayload(const Notification &notification)
Creates a view on a Notification object
Definition: SpecializedPayload.hpp:205
Adapter class for enums to make them loggable and parsable from e.g. XML files.
Definition: Enum.hxx:23
RscType
Datatypes supported by Rsc. Values are identical with CommonRemoting::RemotingMarshalType. Only supported types of RemotingMarshalType are included.
Definition: RscType.hpp:27
Generic access to a IPayload
Definition: GenericPayload.hpp:18
static PayloadTypeIdType GetPayloadTypeId()
Returns the PayloadTypeId
Definition: SpecializedPayload.hpp:223
Base class for custom Payload classes
Definition: SpecializedPayload.hpp:42
const T GetFieldValueAs(size_t fieldIndex) const
Gets a field value with the specified type
Definition: SpecializedPayload.hpp:160
std::vector< Arp::System::Rsc::Services::RscVariant< RawPayloadTypeLength > > RawPayloadType
type for the internally transferred payloads
Definition: NotificationManagerTypes.hpp:34
const String ToString() const override
Get a human readable string representation
Definition: SpecializedPayload.hpp:264
Interface for information about the Notification Manager
Definition: INotificationManagerInfo.hpp:20
Root namespace for the PLCnext API
const ValueType & GetFieldValue(size_t fieldIndex) const
Returns the raw value of a field
T rscvariant_cast(const Rsc::Services::RscVariant< N > &value)
Casts an RscVariant to T
Definition: RscVariantHelpers.hpp:56
bool IsReadOnly() const
Returns true if this object is a view on a Payload
Definition: SpecializedPayload.hpp:276
static const String GetPayloadTypeName()
Returns the PayloadType name
Definition: SpecializedPayload.hpp:243
System components used by the System, Device, Plc or Io domains.
Helper class to get access to INotificationManagerInfo
Definition: SpecializedPayload.hpp:187
size_t AddField()
Adds a payload field
Definition: SpecializedPayload.hpp:106
Definition: LongStringPayloadField.hpp:16