PLCnext API Documentation 25.0.2.69
RscArrayWriter.hpp
1
2//
3// Copyright Phoenix Contact GmbH & Co. KG
4//
6#pragma once
8#include "Arp/Base/Commons/Exceptions/InvalidOperationException.hpp"
9#include "Arp/Base/Rsc/Commons/Rsc.hpp"
10#include "Arp/Base/Rsc/Commons/RscString.hxx"
11#include "Arp/Base/Rsc/Commons/RscVariant.hxx"
12#include "Arp/Base/Rsc/Commons/RscTypeInfo.hpp"
13#include <functional>
14
15namespace Arp::Base::Rsc::Commons
16{
17
22class ARP_EXPORT RscArrayWriter
23{
24public: // usings
27
28public: // construction
29 template<int N>
30 explicit RscArrayWriter(const RscVariant<N>& value);
31 explicit RscArrayWriter(const RscVariantBase& value);
32 RscArrayWriter(size_t size, RscType elementType, WriteElementFunction writeFunction);
33 RscArrayWriter(size_t size, RscType elementType, RscWriter& writer, size_t dimensions = 1);
34
35public: // setter/getter operations
36 size_t GetSize(void)const;
37 RscType GetElementType(void)const;
38 size_t GetPosition(void)const;
39
40public: // operations
41 template<class T> void WriteNext(const T& current);
42 template<int N> void WriteNext(const RscString<N>& current);
43 template<int N> void WriteNext(const RscVariant<N>& current);
44
45 template<int N>
46 RscVariant<N> WriteNextStruct();
47 RscArrayWriter WriteNextArray(size_t size);
48 void WriteNextSimple(const RscVariantBase& value);
49
50private: // methods
51 void WriteElement(RscType type, const RscVariantBase& value);
52 void SetWriterTo(const RscVariantBase& value);
53
54private: // fields
55 RscArrayInfo arrayInfo;
56 size_t position = 0;
57 RscWriter* pWriter = nullptr;
58 WriteElementFunction writeElementFunction;
59 size_t maxStringSize = 0;
60};
61
63// inline methods of class RscArrayWriter<T>
64
68template<int N>
70 : RscArrayWriter(static_cast<const RscVariantBase&>(value))
71{
72}
73
80template<class T>
81inline void RscArrayWriter::WriteNext(const T& current)
82{
83 if(!RscTypeInfo::IsPrimitiveType(this->GetElementType()) && this->GetElementType() != RscType::DateTime)
84 {
85 throw InvalidOperationException("This method can only be used with primitive Types");
86 }
87 RscVariant<> currentVariant(current);
88 this->WriteNextSimple(currentVariant);
89}
90
99template<int N>
100inline void RscArrayWriter::WriteNext(const RscString<N>& current)
101{
102 if(this->GetElementType() != RscType::String)
103 {
104 throw InvalidOperationException("This instance of RscArrayWriter is not for string operations");
105 }
106 if(N != this->maxStringSize)
107 {
108 throw InvalidOperationException("Size of current doesn't fit to maxStringSize");
109 }
110 RscVariantBase currentVariant(current);
111 this->writeElementFunction(this->GetElementType(), currentVariant);
112 ++this->position;
113}
114
115
123template<int N>
124inline void RscArrayWriter::WriteNext(const RscVariant<N>& current)
125{
126 if (this->GetElementType() != RscType::Object)
127 {
128 throw InvalidOperationException("This instance of RscArrayWriter is not hasBegunWriter to contain objects");
129 }
130 if (N != this->maxStringSize)
131 {
132 throw InvalidOperationException("Size of current value doesn't fit to maxStringSize");
133 }
134 this->writeElementFunction(current.GetType(), current);
135 if (current.IsComplexType())
136 {
137 this->SetWriterTo(current);
138 }
139 ++this->position;
140}
141
151template<int N>
153{
154 if(this->arrayInfo.ElementType != RscType::Struct)
155 {
156 throw InvalidOperationException("This method is only valid with struct element types");
157 }
158
159 RscVariant<N> structVariant;
160
161 if(this->pWriter == nullptr)
162 {
163 structVariant = RscVariant<N>::CreateStructVariant(this->arrayInfo.FieldCount);
164 structVariant.SetWriteElementFunction(this->writeElementFunction);
165 this->writeElementFunction(RscType::Struct, structVariant);
166 }
167 else
168 {
169 structVariant = RscVariant<N>::CreateStructVariant(this->arrayInfo.FieldCount);
170 this->SetWriterTo(structVariant);
171 }
172
173 ++this->position;
174 return structVariant;
175}
176
177} // 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 information to marshal dynamic arrays.
Definition: RscArrayInfo.hpp:14
size_t FieldCount
The count of Fields of struct element type, if the array consists of structs.
Definition: RscArrayInfo.hpp:22
RscType ElementType
The array element type.
Definition: RscArrayInfo.hpp:23
Helper class to write a dynamic array of primtive types from RscVariant. This class uses the array in...
Definition: RscArrayWriter.hpp:23
RscVariant<>::WriteElementFunction WriteElementFunction
The write element delegate type.
Definition: RscArrayWriter.hpp:26
RscType GetElementType(void) const
Gets the element type of the array values.
Definition: RscArrayWriter.cpp:93
void WriteNextSimple(const RscVariantBase &value)
For internal use only.
Definition: RscArrayWriter.cpp:131
RscArrayWriter(const RscVariant< N > &value)
Constructs an RscArray instance.
Definition: RscArrayWriter.hpp:69
void WriteNext(const T &current)
Writes the next single array element using the callback function given by array information of RscVar...
Definition: RscArrayWriter.hpp:81
RscVariant< N > WriteNextStruct()
Writes the next struct element.
Definition: RscArrayWriter.hpp:152
Contains a static string with string lentgh up to N characters. The string shall be null terminated.
Definition: RscString.hxx:24
static bool IsPrimitiveType(RscType type)
Determines if the supplied RscType is a primitive type.
Definition: RscTypeInfo.cpp:77
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
bool IsComplexType(void) const
Determines if this instance represents a complex type, i.e. an array or a struct.
Definition: RscVariantBase.cpp:243
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
static RscVariant CreateStructVariant(size_t fieldCount)
Creates a new RscVariant initialized with RscStructInfo
Definition: RscVariant.ipp:273
Writes marshalled data of RSC services.
Definition: RscWriter.hpp:34