PLCnext API Documentation  22.9.0.33
RscWriter.hpp
1 //
3 // Copyright PHOENIX CONTACT Electronics GmbH
4 //
6 #pragma once
7 #include "Arp/System/Rsc/Services/Rsc.h"
8 #include "Arp/System/Rsc/Services/RscValueAdapter.hxx"
9 #include "Arp/System/Rsc/Services/RscStream.hpp"
10 #include "Arp/System/Rsc/Services/RemotingWriter.hpp"
11 #include <vector>
12 #include <type_traits>
13 
14 // forwards
15 namespace CommonRemoting
16 {
17 class BinaryWriter2;
18 }
19 
20 namespace Arp { namespace System { namespace Rsc { namespace Services
21 {
22 
23 // forwards
24 class RscClientContext;
25 class RscStreamAdapter;
26 
30 class RscWriter
31 {
32 public: // construction/destruction
34  RscWriter(BinaryWriter2& remotingWriter, RscClientContext* pClientContext = nullptr);
36  RscWriter(const RscWriter& arg) = default;
38  RscWriter& operator=(const RscWriter& arg) = default;
40  ~RscWriter(void) = default;
41 
42 public: // setter/getter operations
48 
53  RemotingWriter& GetRemotingWriter(void);
54 
55 public: // write operations
60  void WriteConfirmation(bool flush = false);
61 
67 
68 public: // generic write operations
74  template<class T> void Write(const T& value);
75 
82  template<class T> void Write(const T& value, bool writeTag);
83 
89  template<int N> void WriteString(const String& value);
90 
96  template<int N> void WriteString(const char(&value)[N]);
97 
103  template<class T> void WriteObject(const T& value);
104 
110  template<int N> void WriteObjectString(const String& value);
111 
117  template<int N> void WriteObjectString(const char(&value)[N]);
118 
124  template<class T> void WriteArray(const std::vector<T>& values);
125 
131  template<class T> void WriteArray(const std::vector<std::vector<T>>& values);
132 
139  template<class T, size_t N> void WriteArray(const std::array<T, N>& values);
140 
141 private: // methods
147  template<class T> void BeginArray(size_t size);
148 
149 private: // fields
150  RemotingWriter remotingWriter;
151 };
152 
154 // inline methods of class RscWriter
155 
156 inline RscWriter::RscWriter(BinaryWriter2& remotingWriter, RscClientContext* pClientContextArg)
157  : remotingWriter(remotingWriter, *this, pClientContextArg)
158 {
159 }
160 
161 inline RemotingWriter& RscWriter::GetRemotingWriter(void)
162 {
163  return this->remotingWriter;
164 }
165 
166 inline void RscWriter::WriteConfirmation(bool flush)
167 {
168  this->remotingWriter.WriteConfirmation(flush);
169 }
170 
172 {
173  return RscStream(this->remotingWriter);
174 }
175 
176 template<class T>
177 inline void RscWriter::Write(const T& value)
178 {
179  RscValueAdapter<T> valueAdapter(value);
180  valueAdapter.Write(this->remotingWriter);
181 }
182 
183 template<class T>
184 inline void RscWriter::Write(const T& value, bool writetTag)
185 {
186  RscValueAdapter<T> valueAdapter(value);
187  valueAdapter.Write(this->remotingWriter, writetTag);
188 }
189 
190 template<int N>
191 inline void RscWriter::WriteString(const String& value)
192 {
193  this->remotingWriter.WriteStringInternal(value, N);
194 }
195 
196 template<int N>
197 inline void RscWriter::WriteString(const char(&value)[N])
198 {
199  this->remotingWriter.WriteStringInternal(value, N);
200 }
201 
202 template<class T>
203 inline void RscWriter::WriteArray(const std::vector<T>& values)
204 {
205  size_t length = values.size();
206  this->BeginArray<T>(length);
207  for (size_t i = 0; i < length; ++i)
208  {
209  const T& value = values[i];
210  RscValueAdapter<T> valueAdapter(value);
211  valueAdapter.Write(this->remotingWriter, false); // Tag has already been written
212  }
213 }
214 
215 template<class T>
216 inline void RscWriter::WriteArray(const std::vector<std::vector<T>>& values)
217 {
218  this->remotingWriter.WriteTag(RscType::Array);
219  this->BeginArray<T>(values.size());
220  for(const std::vector<T>& innerArray : values)
221  {
222  this->remotingWriter.WriteArrayLength(innerArray.size());
223 
224  for(const T& value : innerArray)
225  {
226  RscValueAdapter<T> valueAdapter(value);
227  valueAdapter.Write(this->remotingWriter, false); // Tag has already been written
228  }
229  }
230 }
231 
232 template<class T, size_t N>
233 inline void RscWriter::WriteArray(const std::array<T, N>& values)
234 {
235  this->BeginArray<T>(N);
236  for(size_t i = 0; i < N; ++i)
237  {
238  const T& value = values[i];
239  RscValueAdapter<T> valueAdapter(value);
240  valueAdapter.Write(this->remotingWriter, false); // Tag has already been written
241  }
242 }
243 
244 template<class T>
245 inline void RscWriter::BeginArray(size_t size)
246 {
247  RscType elementType = GetRscTypeFrom(T());
248  this->remotingWriter.WriteTag(RscType::Array);
249  if(elementType == RscType::Struct)
250  {
251  this->remotingWriter.WriteBeginStruct(StructInfo<T>().FieldCount);
252  }
253  else
254  {
255  this->remotingWriter.WriteTag(elementType);
256  }
257  this->remotingWriter.WriteArrayLength(size);
258 }
259 
260 
261 template<class T>
262 inline void RscWriter::WriteObject(const T& value)
263 {
264  this->remotingWriter.WriteObjectType(GetRscTypeFrom(value));
265  this->Write(value);
266 }
267 
268 template<int N>
269 inline void RscWriter::WriteObjectString(const String& value)
270 {
271  this->remotingWriter.WriteObjectString(RscStringEncoding::Utf8, value);
272 }
273 
274 template<int N>
275 inline void RscWriter::WriteObjectString(const char(&value)[N])
276 {
277  this->remotingWriter.WriteObjectString(RscStringEncoding::Utf8, value);
278 }
279 
280 }}}} // 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:20
Writes data to Rsc.
Definition: RscWriter.hpp:31
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:177
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:203
RscWriter & operator=(const RscWriter &arg)=default
Assignment operator.
void WriteConfirmation(bool flush=false)
Sends a confirmation message to remote station.
Definition: RscWriter.hpp:166
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:161
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:262
RscStream GetStream()
Creates an instance of RscStream initialized to write stream data.
Definition: RscWriter.hpp:171
void WriteObjectString(const String &value)
Writes a string in object format as Utf8 string.
Definition: RscWriter.hpp:269
RscWriter(BinaryWriter2 &remotingWriter, RscClientContext *pClientContext=nullptr)
Constructs an RscWriter instance.
Definition: RscWriter.hpp:156
void WriteString(const String &value)
Writes a string in format Utf-8.
Definition: RscWriter.hpp:191
@ System
System components used by the System, Device, Plc or Io domains.
RscType
Datatypes supported by Rsc. Values are identical with CommonRemoting::RemotingMarshalType....
Definition: RscType.hpp:37
@ Struct
Complex datatype with implements IRscSerializable
Root namespace for the PLCnext API