PLCnext API Documentation 26.6.0.38
RscStructWriter.hpp
1
2//
3// Copyright Phoenix Contact GmbH & Co. KG
4//
6#pragma once
8#include "Arp/Base/Rsc/Commons/RscVariant.hxx"
9#include "Arp/Base/Rsc/Commons/RscTypeInfo.hpp"
10#include "Arp/Base/Commons/Exceptions/InvalidOperationException.hpp"
11
12namespace Arp::Base::Rsc::Commons
13{
14
15// type imports
17
23class ARP_EXPORT RscStructWriter
24{
25public: // usings
27
28public: // construction
29 explicit RscStructWriter(const RscVariantBase& structVariant);
30
31public: // static operations
32 size_t GetFieldCount(void)const;
33
34public: // operations
35 template<class T> void WriteNextField(const T& value);
36 template<int N> void WriteNextField(const RscVariant<N>& value);
37 template<int N> void WriteNextField(const RscString<N>& value);
38 template<int N> void WriteNextField(const RscSecureString<N>& value);
39
40private: // methods
41 void WriteField(RscType valueType, const RscVariantBase& value);
42 void SetWriterTo(const RscVariantBase& value);
43
44private: // fields
45 RscStructInfo structInfo;
46 RscWriter* pWriter = nullptr;
47 WriteElementFunction writeFieldFunction;
48};
49
51// inline methods of class RscStructWriter
52
58template<class T>
59inline void RscStructWriter::WriteNextField(const T& value)
60{
61 RscType type = RscTypeDeduction::GetFrom(value);
63 {
64 throw InvalidOperationException("This operation is only valid for simple types");
65 }
66 // else
67 RscVariant<0> variant(value);
68 this->writeFieldFunction(type, variant);
69}
70
79template<int N>
81{
82 if(value.GetType() == RscType::Array)
83 {
84 this->writeFieldFunction(RscType::Array, value);
85 if(this->pWriter == nullptr)
86 {
87 value.SetWriteElementFunction(this->writeFieldFunction);
88 }
89 else
90 {
91 this->SetWriterTo(value);
92 }
93 }
94 else if (value.GetType() == RscType::Struct)
95 {
96 this->writeFieldFunction(RscType::Struct, value);
97 if (this->pWriter == nullptr)
98 {
99 value.SetWriteElementFunction(this->writeFieldFunction);
100 }
101 else
102 {
103 this->SetWriterTo(value);
104 }
105 }
106 else
107 {
108 this->writeFieldFunction(value.GetType(), value);
109 }
110}
111
117template<int N>
119{
120 RscVariantBase variantField(value);
121 this->writeFieldFunction(RscType::String, variantField);
122}
123
129template<int N>
131{
132 RscVariantBase variantField(value);
133 this->writeFieldFunction(RscType::SecureString, variantField);
134}
135
136} // end of namespace Arp::Base::Rsc::Commons
This exception is thrown when an operation cannot be executed because the related state is invalid.
Definition: InvalidOperationException.hpp:16
Specialized implementation of RscString for secure context.
Definition: RscSecureString.hxx:16
Contains a static string with string lentgh up to N characters. The string shall be null terminated.
Definition: RscString.hxx:24
Contains information to marshal structs.
Definition: RscStructInfo.hpp:17
Utility class to write dynamic structs.
Definition: RscStructWriter.hpp:24
RscVariantBase::WriteElementFunction WriteElementFunction
The prototype of the delegate to write a field.
Definition: RscStructWriter.hpp:26
void WriteNextField(const T &value)
Writes the next field of the struct. This operation is used if the field is an object,...
Definition: RscStructWriter.hpp:59
static constexpr RscType GetFrom(const T &)
Gets the RscType of the as argument passed parameter.
Definition: RscTypeDeduction.hpp:77
static bool IsSimpleType(RscType type)
Determines if the supplied RscType is a simple type.
Definition: RscTypeInfo.cpp:139
This class is a base class of template class RscVariant.
Definition: RscVariantBase.hpp:41
RscType GetType(void) const
Gets the RscType of the contained element
Definition: RscVariantBase.cpp:290
std::function< void(RscType elementType, const RscVariantBase &value)> WriteElementFunction
The write element delegate type.
Definition: RscVariantBase.hpp:48
void SetWriteElementFunction(WriteElementFunction &function) const
Sets the element write function.
Definition: RscVariantBase.cpp:768
Rsc class for variant data types like primitive data type, strings or information about arrays or str...
Definition: RscVariant.hxx:57
Writes marshalled data of RSC services.
Definition: RscWriter.hpp:34