PLCnext API Documentation 25.0.2.69
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(RscString<N>& result);
43 template<int N> void ReadNextField(RscVariant<N>& result);
44
45private: // methods
46 void ReadField(RscType fieldType, RscVariantBase& result);
47 void SetReaderTo(RscVariantBase& value);
48
49private: // fields
50 RscStructInfo structInfo;
51 RscReader* pReader = nullptr;
52 ReadFieldFunction readFieldFunction;
53};
54
56// inline methods of class RscStructReader
57
62template<class T>
63inline void RscStructReader::ReadNextField(T& result)
64{
66
67 RscType fieldType = RscTypeDeduction::GetFrom(result);
68 if(!RscTypeInfo::IsPrimitiveType(fieldType))
69 {
70 throw InvalidOperationException::Create("This method works only for primitive types");
71 }
72 RscVariant<0> value(fieldType);
73 (void)this->readFieldFunction(fieldType, value);
74 value.CopyTo(result);
75}
76
79template<int N>
81{
82 RscVariantBase value(result);
83 this->readFieldFunction(RscType::String, value);
84}
85
94template<int N>
96{
97 this->readFieldFunction(RscType::Object, result);
98 if (result.IsComplexType())
99 {
100 result.ResetComplexTypeInfo();
101 if (this->pReader == nullptr)
102 {
103 result.SetReadElementFunction(this->readFieldFunction);
104 }
105 else
106 {
107 this->SetReaderTo(result);
108 }
109 }
110}
111
112} // 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
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:63
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 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
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:44
void CopyTo(String &value) const
Copies the content of this variant to a string.
Definition: RscVariantBase.cpp:858
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