PLCnext API Documentation 26.6.0.38
RscStructReader.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/RscString.hxx"
10#include "Arp/Base/Rsc/Commons/RscVariant.hxx"
11#include "Arp/Base/Rsc/Commons/RscTypeInfo.hpp"
12#include "Arp/Base/Commons/Exceptions/InvalidOperationException.hpp"
13#include <functional>
14
15namespace Arp::Base::Rsc::Commons
16{
17
19using Arp::Base::Rsc::Commons::Internal::RscVariantAccessor;
20
21// forwards
22class RscArrayReader;
23
29class ARP_EXPORT RscStructReader
30{
31public: // usings
33
34public: // construction
35 explicit RscStructReader(const RscVariantBase& structVariant);
36
37public: // static operations
38 size_t GetFieldCount(void)const;
39
40public: // operations
41 template<class T> void ReadNextField(T& result);
42 template<int N> void ReadNextField(RscVariant<N>& result);
43 template<int N> void ReadNextField(RscString<N>& result);
44 template<int N> void ReadNextField(RscSecureString<N>& result);
45
46private: // methods
47 void ReadField(RscType fieldType, RscVariantBase& result);
48 void SetReaderTo(RscVariantBase& value);
49
50private: // fields
51 RscStructInfo structInfo;
52 RscReader* pReader = nullptr;
53 ReadFieldFunction readFieldFunction;
54};
55
57// inline methods of class RscStructReader
58
63template<class T>
64inline void RscStructReader::ReadNextField(T& result)
65{
67
68 RscType fieldType = RscTypeDeduction::GetFrom(result);
69 if(!RscTypeInfo::IsSimpleType(fieldType))
70 {
71 throw InvalidOperationException::Create("This method works only for simple types");
72 }
73 RscVariant<> value(fieldType);
74 (void)this->readFieldFunction(fieldType, value);
75 value.CopyTo(result);
76}
77
86template<int N>
88{
89 this->readFieldFunction(RscType::Object, result);
90 if (result.IsComplexType())
91 {
92 result.ResetComplexTypeInfo();
93 if (this->pReader == nullptr)
94 {
95 result.SetReadElementFunction(this->readFieldFunction);
96 }
97 else
98 {
99 this->SetReaderTo(result);
100 }
101 }
102}
103
106template<int N>
108{
109 RscVariantBase value(result);
110 this->readFieldFunction(RscType::String, value);
111}
112
115template<int N>
117{
118 RscVariantBase value(result);
119 this->readFieldFunction(RscType::SecureString, value);
120}
121
122} // 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
static InvalidOperationException Create(const String &message)
Creates an InvalidOperationException using an additional message.
Definition: InvalidOperationException.cpp:96
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 read dynamic structs.
Definition: RscStructReader.hpp:30
void ReadNextField(T &result)
Reads the next primitive field of the struct.
Definition: RscStructReader.hpp:64
RscVariantBase::ReadFieldFunction ReadFieldFunction
The prototype of the delegate to read a field.
Definition: RscStructReader.hpp:32
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
bool IsComplexType(void) const
Determines if this instance represents a complex type, i.e. an array or a struct.
Definition: RscVariantBase.cpp:243
void SetReadElementFunction(ReadElementFunction &function)
Sets the element read function.
Definition: RscVariantBase.cpp:741
std::function< void(RscType fieldType, RscVariantBase &value)> ReadFieldFunction
The read field delegate type.
Definition: RscVariantBase.hpp:45
void CopyTo(String &value) const
Copies the content of this variant to a string.
Definition: RscVariantBase.cpp:857
void ResetComplexTypeInfo(void)
Clears reader, writer and read/write element functions.
Definition: RscVariantBase.cpp:273
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