PLCnext API Documentation 25.0.2.69
RscReadEnumerator.hxx
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/RscArrayReader.hpp"
10#include "Arp/Base/Rsc/Commons/IRscReadEnumerator.hxx"
11#include "Arp/Base/Rsc/Commons/Services/RscReader.hpp"
12
13namespace Arp::Base::Rsc::Commons::Services
14{
15
17template<class T>
19{
20public: // construction
21 RscReadEnumerator(RscReader& rscReader, bool isArrayEnumerator);
22
23 // canonical construction/assignment/destruction
28 ~RscReadEnumerator(void)override;
29
30public: // operations of IRscReadEnumerator
31 size_t BeginRead(void)override;
32 bool ReadNext(T& current)override;
33 bool ReadNext(RscArrayReader& current)override;
34 void EndRead(void)override;
35
36private: // fields
37 RscReader& rscReader;
38 size_t arrayLength = UndefinedArrayLength;
39 bool isArrayEnumerator;
40 bool hasEnded = false;
41 RscType rscType = RscTypeDeduction().Get<T>();
42 size_t fieldCount = StructInfo<T>().FieldCount;
43 bool isConcreteType = RscTypeInfo::IsConcreteType(this->rscType);
44
45public: // static fields
47 static constexpr size_t UndefinedArrayLength = std::numeric_limits<size_t>::max();
48};
49
51// inline methods of class RscEnumerator
52
56template<class T>
58
62template<class T>
64
69template<class T>
71
76template<class T>
78
81template<class T>
83
88template<class T>
89inline RscReadEnumerator<T>::RscReadEnumerator(RscReader& rscReader, bool isArrayEnumerator)
90 : rscReader(rscReader)
91 , isArrayEnumerator(isArrayEnumerator)
92{
93}
94
98template<class T>
100{
101 if(this->isArrayEnumerator)
102 {
103 this->rscReader.ReadTag(RscType::Array);
104 if(this->rscType == RscType::Struct)
105 {
106 this->rscReader.ReadBeginStruct(this->fieldCount);
107 }
108 else
109 {
110 this->rscReader.ReadTag(this->rscType);
111 }
112 this->arrayLength = this->rscReader.ReadArrayLength();
113 }
114 else // use standard enumerator
115 {
116 this->rscReader.ReadTag(RscType::Enumerator);
117 }
118 return this->arrayLength;
119}
120
125template<class T>
126inline bool RscReadEnumerator<T>::ReadNext(T& current)
127{
128 if(this->isArrayEnumerator)
129 {
130 if(this->arrayLength == 0)
131 {
132 return false; // end of array reached
133 }
134 // else
135 --this->arrayLength;
136 this->rscReader.Read(current, this->isConcreteType); // omit tag if element type is not object
137 }
138 else // use standard enumerator
139 {
140 if(this->hasEnded)
141 {
142 return false;
143 }
144 this->rscReader.ReadEnumeratorTag(this->rscType, this->hasEnded);
145 if(this->hasEnded)
146 {
147 return false; // end of sequence
148 }
149 if(this->rscType == RscType::Struct)
150 {
151 size_t readFieldCount = this->rscReader.ReadFieldCount();
152 if(readFieldCount != this->fieldCount)
153 {
154 throw RscException(
155 RscError::ProtocolViolation,
156 "Read invalid struct field count: expected '{}', received '{}'",
157 this->fieldCount, readFieldCount);
158 }
159 }
160 this->rscReader.Read(current, true);
161 }
162 return true;
163}
164
169template<class T>
171{
172 if (this->arrayLength == 0)
173 {
174 return false; // end of array reached
175 }
176 // else
177 this->rscReader.ReadTag(RscType::Array);
178 RscType elementType = this->rscReader.ReadTag();
179 size_t arraySize = this->rscReader.ReadArrayLength();
180 current = RscArrayReader(arraySize, elementType, this->rscReader);
181 return true;
182}
183
186template<class T>
188{
189 if(this->isArrayEnumerator)
190 {
191 if(this->arrayLength != 0)
192 {
193 throw InvalidOperationException("End of array enumerator was not reached yet");
194 }
195 // else
196 this->hasEnded = true;
197 }
198 // else standard enumeration was read
199}
200
201} // end of namespace Arp::Base::Rsc::Commons::Services
This exception is thrown when an operation cannot be executed because the related state is invalid.
Definition: InvalidOperationException.hpp:16
Interface for reading a arrays or enumerations.
Definition: IRscReadEnumerator.hxx:19
Utility class to read an array of primitive types from RscVariant. This class uses the array informat...
Definition: RscArrayReader.hpp:22
This exception class is used by RSC if any exception is thrown.
Definition: RscException.hpp:24
This class is used to deduct RSC types automatically by compilation.
Definition: RscTypeDeduction.hpp:24
constexpr RscType Get(void)
Gets the RscType of the as argument passed template parameter.
Definition: RscTypeDeduction.hpp:47
static bool IsConcreteType(RscType type)
Determines if the supplied type specifies a concrete type, i.e not unspecified and not object.
Definition: RscTypeInfo.cpp:242
Implementation of IRscReadEnumerator used on client side.
Definition: RscReadEnumerator.hxx:19
RscReadEnumerator & operator=(const RscReadEnumerator &arg)
The default copy-assignment operator.
bool ReadNext(T &current) override
Reads the next value of the enumeration.
Definition: RscReadEnumerator.hxx:126
size_t BeginRead(void) override
This operation is called at the begin of reading the enumeration.
Definition: RscReadEnumerator.hxx:99
RscReadEnumerator(RscReadEnumerator &&arg) noexcept
The default move constructor.
RscReadEnumerator & operator=(RscReadEnumerator &&arg) noexcept
The default move-assignment operator.
RscReadEnumerator(RscReader &rscReader, bool isArrayEnumerator)
Constructs an RscReadEnumerator instance.
Definition: RscReadEnumerator.hxx:89
static constexpr size_t UndefinedArrayLength
This constant is used to determine that an enumeration is not an array enumeration and hence has an u...
Definition: RscReadEnumerator.hxx:47
~RscReadEnumerator(void) override
Destructs this instance and checks if enumerations has ended.
void EndRead(void) override
This operation has to be called when the enumerating has finished.
Definition: RscReadEnumerator.hxx:187
RscReadEnumerator(const RscReadEnumerator &arg)
The default copy constructor.
Reads marshaled data of RSC services.
Definition: RscReader.hpp:34
This class provides the number of fields of a concrete struct. This class is specialized for concrete...
Definition: StructInfo.hxx:26
size_t FieldCount
Specifies the field count of a struct.
Definition: StructInfo.hxx:27