PLCnext API Documentation 25.0.2.69
RscArrayReader.hpp
1
2//
3// Copyright Phoenix Contact GmbH & Co. KG
4//
6#pragma once
8#include "Arp/Base/Rsc/Commons/Rsc.hpp"
9#include "Arp/Base/Rsc/Commons/RscVariant.hxx"
10#include "Arp/Base/Rsc/Commons/RscTypeInfo.hpp"
11#include "Arp/Base/Rsc/Commons/RscStructReader.hpp"
12#include "Arp/Base/Commons/Exceptions/InvalidOperationException.hpp"
13
14namespace Arp::Base::Rsc::Commons
15{
16
21class ARP_EXPORT RscArrayReader
22{
23public: // usings
26
27public: // construction
28 template<int N>
29 explicit RscArrayReader(const RscVariant<N>& arrayVariant);
30 explicit RscArrayReader(const RscVariantBase& arrayVariant);
31 RscArrayReader(size_t size, RscType elementType, const ReadElementFunction& readElementFunction);
32 RscArrayReader(size_t size, RscType elementType, Reader& reader);
33
34public: // setter/getter operations
35 size_t GetSize(void)const;
36 RscType GetElementType(void)const;
37 size_t GetDimensions(void)const;
38 size_t GetFieldCount(void)const;
39 size_t GetPosition(void)const;
40
41public: // operations
42 template<class T> void ReadNext(T& current);
43 template<int N> void ReadNext(RscString<N>& current);
44 template<int N> void ReadNext(RscVariant<N>& current);
45
46 template<int N>
47 RscStructReader ReadNextStruct(void);
48 RscArrayReader ReadNextArray(void);
49
50private: // methods
51 void ReadNextSimple(RscVariant<>& value);
52 void ReadElement(RscType elementType, RscVariantBase& result);
53 void SetReaderTo(RscVariantBase& value);
54 void CheckReadArgument(RscType argumentType, size_t maxLength);
55
56private: // fields
57 RscArrayInfo arrayInfo;
58 size_t position = 0;
59 size_t maxStringSize = 0;
60 Reader* pReader = nullptr;
61 ReadElementFunction readElementFunction;
62};
63
65// inline methods of class RscArrayReader
66
73template<int N>
75 : RscArrayReader(static_cast<const RscVariantBase&>(arrayVariant))
76{
77}
78
85template<class T>
86inline void RscArrayReader::ReadNext(T& current)
87{
88 if(!RscTypeInfo::IsPrimitiveType(this->GetElementType()) && this->GetElementType() != RscType::DateTime)
89 {
90 throw InvalidOperationException("This method can only be used with primitive Types");
91 }
92 if(this->GetDimensions() > 1)
93 {
94 throw InvalidOperationException("This element contains subarrays, please use ReadNextArray instead");
95 }
96 RscVariant<> currentVariant(this->GetElementType());
97 this->ReadNextSimple(currentVariant);
98 currentVariant.CopyTo(current);
99}
100
106template<int N>
108{
109 this->CheckReadArgument(RscType::String, N);
110
111 RscVariantBase currentVariant(current);
112 this->readElementFunction(this->GetElementType(), currentVariant);
113 ++this->position;
114}
115
122template<int N>
124{
125 if(this->maxStringSize != N)
126 {
127 throw InvalidOperationException("Size of current doesn't fit to N");
128 }
129
130 // in case of array info data address starts wir ArrayInformation::size,
131 // in case of struct buffer starts with StructInformation::FieldCount
132 this->readElementFunction(this->GetElementType(), current);
133 if (current.IsComplexType())
134 {
135 this->SetReaderTo(current);
136 current.SetReadElementFunction(this->readElementFunction);
137 }
138 ++this->position;
139}
140
144template<int N>
146{
147 if(this->GetElementType() != RscType::Struct)
148 {
149 throw InvalidOperationException("Operation only valid for array of structs");
150 }
151 if(this->GetDimensions() != 1)
152 {
153 throw InvalidOperationException("Array Dimensions != 1, please read subarray information first");
154 }
155 RscVariant<N> structVariant;
156 this->ReadNext(structVariant);
157
158 structVariant.SetStructInfo(this->arrayInfo.FieldCount);
159
160 return RscStructReader(structVariant);
161}
162
163} // 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
Utility class to read an array of primitive types from RscVariant. This class uses the array informat...
Definition: RscArrayReader.hpp:22
void ReadNext(T &current)
Reads the next array value using the supplied callback function given by ArrayInformation of RscVaria...
Definition: RscArrayReader.hpp:86
RscVariantBase::ReadElementFunction ReadElementFunction
The read element delegate type.
Definition: RscArrayReader.hpp:25
RscArrayReader(const RscVariant< N > &arrayVariant)
Constructs an RscArray instance.
Definition: RscArrayReader.hpp:74
RscType GetElementType(void) const
Returns the value type of the array elements.
Definition: RscArrayReader.cpp:103
size_t GetDimensions(void) const
Gets the count of the array dimensions.
Definition: RscArrayReader.cpp:110
RscStructReader ReadNextStruct(void)
Reads the next struct element.
Definition: RscArrayReader.hpp:145
Contains a static string with string lentgh up to N characters. The string shall be null terminated.
Definition: RscString.hxx:24
Utility class to read dynamic structs.
Definition: RscStructReader.hpp:30
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
bool IsComplexType(void) const
Determines if this instance represents a complex type, i.e. an array or a struct.
Definition: RscVariantBase.cpp:243
std::function< void(RscType elementType, RscVariantBase &value)> ReadElementFunction
The read element delegate type.
Definition: RscVariantBase.hpp:46
void SetReadElementFunction(ReadElementFunction &function)
Sets the element read function.
Definition: RscVariantBase.cpp:741
void SetStructInfo(size_t fieldCount)
Sets the struct info of this instance.
Definition: RscVariantBase.cpp:688
void CopyTo(String &value) const
Copies the content of this variant to a string.
Definition: RscVariantBase.cpp:858
Rsc class for variant data types like primitive data type, strings or information about arrays or str...
Definition: RscVariant.hxx:57
Reads marshaled data of RSC services.
Definition: RscReader.hpp:34