PLCnext API Documentation 25.0.2.69
RscStructWriter.hpp
1
2//
3// Copyright Phoenix Contact GmbH & Co. KG
4//
6#pragma once
8#include "Arp/Base/Rsc/Commons/RscTypeInfo.hpp"
9#include "Arp/Base/Rsc/Commons/RscVariantBase.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 RscString<N>& value);
37 template<int N> void WriteNextField(const RscVariant<N>& value);
38
39private: // methods
40 void WriteField(RscType valueType, const RscVariantBase& value);
41 void SetWriterTo(const RscVariantBase& value);
42
43private: // fields
44 RscStructInfo structInfo;
45 WriteElementFunction writeFieldFunction;
46 RscWriter* pWriter = nullptr;;
47};
48
50// inline methods of class RscStructWriter
51
57template<class T>
58inline void RscStructWriter::WriteNextField(const T& value)
59{
60 RscType type = RscTypeDeduction::GetFrom(value);
62 {
63 throw InvalidOperationException("This operation is only valid for simple types");
64 }
65 // else
66 RscVariant<0> currentVariant(value);
67 this->writeFieldFunction(type, currentVariant);
68}
69
78template<int N>
80{
81 if(value.GetType() == RscType::Array)
82 {
83 this->writeFieldFunction(RscType::Array, value);
84 if(this->pWriter == nullptr)
85 {
86 value.SetWriteElementFunction(this->writeFieldFunction);
87 }
88 else
89 {
90 this->SetWriterTo(value);
91 }
92 }
93 else if (value.GetType() == RscType::Struct)
94 {
95 this->writeFieldFunction(RscType::Struct, value);
96 if (this->pWriter == nullptr)
97 {
98 value.SetWriteElementFunction(this->writeFieldFunction);
99 }
100 else
101 {
102 this->SetWriterTo(value);
103 }
104 }
105 else
106 {
107 this->writeFieldFunction(value.GetType(), value);
108 }
109}
110
116template<int N>
118{
119 RscVariantBase variantField(value);
120 this->writeFieldFunction(RscType::String, variantField);
121}
122
123} // 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
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:58
static constexpr RscType GetFrom(const T &)
Gets the RscType of the as argument passed parameter.
Definition: RscTypeDeduction.hpp:77
static bool IsStringType(RscType type)
Determines if the supplied RscType is a string type.
Definition: RscTypeInfo.cpp:184
static bool IsSimpleType(RscType type)
Determines if the supplied RscType is a simple type.
Definition: RscTypeInfo.cpp:141
This class is a base class of template class RscVariant.
Definition: RscVariantBase.hpp:40
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:47
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