PLCnext API Documentation 23.6.0.37
RscWriter.hpp
1
2//
3// Copyright PHOENIX CONTACT Electronics GmbH
4//
6#pragma once
8#include "Arp/System/Rsc/Services/Rsc.h"
9#include "Arp/System/Rsc/Services/RscValueAdapter.hxx"
10#include "Arp/System/Rsc/Services/RscStream.hpp"
11#include "Arp/System/Rsc/Services/RemotingWriter.hpp"
12#include <vector>
13#include <type_traits>
14
15// forwards
16namespace CommonRemoting
17{
18class BinaryWriter2;
19}
20
21namespace Arp { namespace System { namespace Rsc { namespace Services
22{
23
24// forwards
25class RscClientContext;
26class RscStreamAdapter;
27
32{
33public: // construction/destruction
35 RscWriter(BinaryWriter2& remotingWriter, RscClientContext* pClientContext = nullptr);
37 RscWriter(const RscWriter& arg) = default;
39 RscWriter& operator=(const RscWriter& arg) = default;
41 ~RscWriter(void) = default;
42
43public: // setter/getter operations
49
54 RemotingWriter& GetRemotingWriter(void);
55
56public: // write operations
61 void WriteConfirmation(bool flush = false);
62
68
69public: // generic write operations
75 template<class T> void Write(const T& value);
76
83 template<class T> void Write(const T& value, bool writeTag);
84
90 template<int N> void WriteString(const String& value);
91
97 template<int N> void WriteString(const char(&value)[N]);
98
104 template<class T> void WriteObject(const T& value);
105
111 template<int N> void WriteObjectString(const String& value);
112
118 template<int N> void WriteObjectString(const char(&value)[N]);
119
125 template<class T> void WriteArray(const std::vector<T>& values);
126
132 template<class T> void WriteArray(const std::vector<std::vector<T>>& values);
133
140 template<class T, size_t N> void WriteArray(const std::array<T, N>& values);
141
142private: // methods
148 template<class T> void BeginArray(size_t size);
149
150private: // fields
151 RemotingWriter remotingWriter;
152};
153
155// inline methods of class RscWriter
156
157inline RscWriter::RscWriter(BinaryWriter2& remotingWriter, RscClientContext* pClientContextArg)
158 : remotingWriter(remotingWriter, *this, pClientContextArg)
159{
160}
161
162inline RemotingWriter& RscWriter::GetRemotingWriter(void)
163{
164 return this->remotingWriter;
165}
166
167inline void RscWriter::WriteConfirmation(bool flush)
168{
169 this->remotingWriter.WriteConfirmation(flush);
170}
171
173{
174 return RscStream(this->remotingWriter);
175}
176
177template<class T>
178inline void RscWriter::Write(const T& value)
179{
180 RscValueAdapter<T> valueAdapter(value);
181 valueAdapter.Write(this->remotingWriter);
182}
183
184template<class T>
185inline void RscWriter::Write(const T& value, bool writetTag)
186{
187 RscValueAdapter<T> valueAdapter(value);
188 valueAdapter.Write(this->remotingWriter, writetTag);
189}
190
191template<int N>
192inline void RscWriter::WriteString(const String& value)
193{
194 this->remotingWriter.WriteStringInternal(value, N);
195}
196
197template<int N>
198inline void RscWriter::WriteString(const char(&value)[N])
199{
200 this->remotingWriter.WriteStringInternal(value, N);
201}
202
203template<class T>
204inline void RscWriter::WriteArray(const std::vector<T>& values)
205{
206 size_t length = values.size();
207 this->BeginArray<T>(length);
208 for (size_t i = 0; i < length; ++i)
209 {
210 const T& value = values[i];
211 RscValueAdapter<T> valueAdapter(value);
212 valueAdapter.Write(this->remotingWriter, false); // Tag has already been written
213 }
214}
215
216template<class T>
217inline void RscWriter::WriteArray(const std::vector<std::vector<T>>& values)
218{
219 this->remotingWriter.WriteTag(RscType::Array);
220 this->BeginArray<T>(values.size());
221 for(const std::vector<T>& innerArray : values)
222 {
223 this->remotingWriter.WriteArrayLength(innerArray.size());
224
225 for(const T& value : innerArray)
226 {
227 RscValueAdapter<T> valueAdapter(value);
228 valueAdapter.Write(this->remotingWriter, false); // Tag has already been written
229 }
230 }
231}
232
233template<class T, size_t N>
234inline void RscWriter::WriteArray(const std::array<T, N>& values)
235{
236 this->BeginArray<T>(N);
237 for(size_t i = 0; i < N; ++i)
238 {
239 const T& value = values[i];
240 RscValueAdapter<T> valueAdapter(value);
241 valueAdapter.Write(this->remotingWriter, false); // Tag has already been written
242 }
243}
244
245template<class T>
246inline void RscWriter::BeginArray(size_t size)
247{
248 RscType elementType = GetRscTypeFrom(T());
249 this->remotingWriter.WriteTag(RscType::Array);
250 if(elementType == RscType::Struct)
251 {
252 this->remotingWriter.WriteBeginStruct(StructInfo<T>().FieldCount);
253 }
254 else
255 {
256 this->remotingWriter.WriteTag(elementType);
257 }
258 this->remotingWriter.WriteArrayLength(size);
259}
260
261
262template<class T>
263inline void RscWriter::WriteObject(const T& value)
264{
265 this->remotingWriter.WriteObjectType(GetRscTypeFrom(value));
266 this->Write(value);
267}
268
269template<int N>
270inline void RscWriter::WriteObjectString(const String& value)
271{
272 this->remotingWriter.WriteObjectString(RscStringEncoding::Utf8, value);
273}
274
275template<int N>
276inline void RscWriter::WriteObjectString(const char(&value)[N])
277{
278 this->remotingWriter.WriteObjectString(RscStringEncoding::Utf8, value);
279}
280
281}}}} // end of namespace Arp::System::Rsc::Services
This class serves as adapter between Rsc streams and streams from Arp::System::Commons::Io,...
Definition: RscStreamAdapter.hpp:20
Enables Rsc services to marshal large data packets as stream.
Definition: RscStream.hpp:21
Writes data to Rsc.
Definition: RscWriter.hpp:32
void WriteStream(RscStreamAdapter &stream)
Writes the data from the supplied stream to remote stream.
void Write(const T &value)
Writes an element of T from Rsc. Datatag and format is determined deducted by type of T.
Definition: RscWriter.hpp:178
RscWriter & operator=(const RscWriter &arg)=default
Assignment operator.
void WriteArray(const std::vector< T > &values)
Writes an array to Rsc. The data to write has to be stored in a std::vector.
Definition: RscWriter.hpp:204
void WriteConfirmation(bool flush=false)
Sends a confirmation message to remote station.
Definition: RscWriter.hpp:167
RscWriter(const RscWriter &arg)=default
Copy constructor.
~RscWriter(void)=default
Destructs this instance and frees all resources.
RemotingWriter & GetRemotingWriter(void)
Returns reference to RemotingWriter
Definition: RscWriter.hpp:162
void WriteObject(const T &value)
Writes an object. Datatag and format is determined deducted by type of T. This method doesn't support...
Definition: RscWriter.hpp:263
RscStream GetStream()
Creates an instance of RscStream initialized to write stream data.
Definition: RscWriter.hpp:172
void WriteObjectString(const String &value)
Writes a string in object format as Utf8 string.
Definition: RscWriter.hpp:270
RscWriter(BinaryWriter2 &remotingWriter, RscClientContext *pClientContext=nullptr)
Constructs an RscWriter instance.
Definition: RscWriter.hpp:157
void WriteString(const String &value)
Writes a string in format Utf-8.
Definition: RscWriter.hpp:192
@ System
System components used by the System, Device, Plc or Io domains.
RscType
Data types supported by RSC.
Definition: RscType.hpp:36
@ Array
std::vector<T> or RSC enumerators
@ Struct
struct derived by IRscSerializable
Root namespace for the PLCnext API