PLCnext API Documentation  21.0.0.35466
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 
16 namespace CommonRemoting
17 {
18 class BinaryWriter2;
19 }
20 
21 using namespace CommonRemoting;
22 
23 namespace Arp { namespace System { namespace Rsc { namespace Services
24 {
25 
26 // forwards
27 class RscClientContext;
28 
32 class RscWriter
33 {
34 public: // construction/destruction
36  RscWriter(BinaryWriter2& remotingWriter, RscClientContext* pClientContext = nullptr);
38  RscWriter(const RscWriter& arg) = default;
40  RscWriter& operator=(const RscWriter& arg) = default;
42  ~RscWriter(void) = default;
43 
44 public: // setter/getter operations
49  RscStream GetStream();
50 
55  RemotingWriter& GetRemotingWriter(void);
56 
57 public: // write operations
62  void WriteConfirmation(bool flush = false);
63 
64 public: // generic write operations
70  template<class T> void Write(const T& value);
71 
78  template<class T> void Write(const T& value, bool writeTag);
79 
85  template<int N> void WriteString(const String& value);
86 
92  template<int N> void WriteString(const char(&value)[N]);
93 
99  template<class T> void WriteObject(const T& value);
100 
106  template<int N> void WriteObjectString(const String& value);
107 
113  template<int N> void WriteObjectString(const char(&value)[N]);
114 
120  template<class T> void WriteArray(const std::vector<T>& values);
121 
127  template<class T> void WriteArray(const std::vector<std::vector<T>>& values);
128 
135  template<class T, size_t N> void WriteArray(const std::array<T, N>& values);
136 
137 private: // methods
143  template<class T> void BeginArray(size_t size);
144 
145 private: // fields
146  RemotingWriter remotingWriter;
147 };
148 
150 // inline methods of class RscWriter
151 
152 inline RscWriter::RscWriter(BinaryWriter2& remotingWriter, RscClientContext* pClientContextArg)
153  : remotingWriter(remotingWriter, *this, pClientContextArg)
154 {
155 }
156 
157 inline RemotingWriter& RscWriter::GetRemotingWriter(void)
158 {
159  return this->remotingWriter;
160 }
161 
162 inline void RscWriter::WriteConfirmation(bool flush)
163 {
164  this->remotingWriter.WriteConfirmation(flush);
165 }
166 
168 {
169  return RscStream(this->remotingWriter);
170 }
171 
172 template<class T>
173 inline void RscWriter::Write(const T& value)
174 {
175  RscValueAdapter<T> valueAdapter(value);
176  valueAdapter.Write(this->remotingWriter);
177 }
178 
179 template<class T>
180 inline void RscWriter::Write(const T& value, bool writetTag)
181 {
182  RscValueAdapter<T> valueAdapter(value);
183  valueAdapter.Write(this->remotingWriter, writetTag);
184 }
185 
186 template<int N>
187 inline void RscWriter::WriteString(const String& value)
188 {
189  this->remotingWriter.WriteStringInternal(value, N);
190 }
191 
192 template<int N>
193 inline void RscWriter::WriteString(const char(&value)[N])
194 {
195  this->remotingWriter.WriteStringInternal(value, N);
196 }
197 
198 template<class T>
199 inline void RscWriter::WriteArray(const std::vector<T>& values)
200 {
201  size_t length = values.size();
202  this->BeginArray<T>(length);
203  for (size_t i = 0; i < length; ++i)
204  {
205  const T& value = values[i];
206  RscValueAdapter<T> valueAdapter(value);
207  valueAdapter.Write(this->remotingWriter, false); // Tag has already been written
208  }
209 }
210 
211 template<class T>
212 inline void RscWriter::WriteArray(const std::vector<std::vector<T>>& values)
213 {
214  this->remotingWriter.WriteTag(RscType::Array);
215  this->BeginArray<T>(values.size());
216  for(const std::vector<T>& innerArray : values)
217  {
218  this->remotingWriter.WriteArrayLength(innerArray.size());
219 
220  for(const T& value : innerArray)
221  {
222  RscValueAdapter<T> valueAdapter(value);
223  valueAdapter.Write(this->remotingWriter, false); // Tag has already been written
224  }
225  }
226 }
227 
228 template<class T, size_t N>
229 inline void RscWriter::WriteArray(const std::array<T, N>& values)
230 {
231  this->BeginArray<T>(N);
232  for(size_t i = 0; i < N; ++i)
233  {
234  const T& value = values[i];
235  RscValueAdapter<T> valueAdapter(value);
236  valueAdapter.Write(this->remotingWriter, false); // Tag has already been written
237  }
238 }
239 
240 template<class T>
241 inline void RscWriter::BeginArray(size_t size)
242 {
243  RscType elementType = GetRscTypeFrom(T());
244  this->remotingWriter.WriteTag(RscType::Array);
245  if(elementType == RscType::Struct)
246  {
247  this->remotingWriter.WriteBeginStruct(StructInfo<T>().FieldCount);
248  }
249  else
250  {
251  this->remotingWriter.WriteTag(elementType);
252  }
253  this->remotingWriter.WriteArrayLength(size);
254 }
255 
256 
257 template<class T>
258 inline void RscWriter::WriteObject(const T& value)
259 {
260  this->remotingWriter.WriteObjectType(GetRscTypeFrom(value));
261  this->Write(value);
262 }
263 
264 template<int N>
265 inline void RscWriter::WriteObjectString(const String& value)
266 {
267  this->remotingWriter.WriteObjectString(RscStringEncoding::Utf8, value);
268 }
269 
270 template<int N>
271 inline void RscWriter::WriteObjectString(const char(&value)[N])
272 {
273  this->remotingWriter.WriteObjectString(RscStringEncoding::Utf8, value);
274 }
275 
276 }}}} // end of namespace Arp::System::Rsc::Services
void WriteString(const String &value)
Writes a string in format Utf-8.
Definition: RscWriter.hpp:187
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:173
Complex datatype with implements IRscSerializable
Writes data to Rsc.
Definition: RscWriter.hpp:32
RemotingWriter & GetRemotingWriter(void)
Returns reference to RemotingWriter
Definition: RscWriter.hpp:157
void WriteConfirmation(bool flush=false)
Sends a confirmation message to remote station.
Definition: RscWriter.hpp:162
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:199
RscType
Datatypes supported by Rsc. Values are identical with CommonRemoting::RemotingMarshalType. Only supported types of RemotingMarshalType are included.
Definition: RscType.hpp:27
Definition: RemotingReader.hpp:10
Enables Rsc services to marshal large data packets as stream.
Definition: RscStream.hpp:17
Root namespace for the PLCnext API
void WriteObject(const T &value)
Writes an object. Datatag and format is determined deducted by type of T. This method doesn&#39;t support...
Definition: RscWriter.hpp:258
RscStream GetStream()
Creates an instance of RscStream initialized to write stream data.
Definition: RscWriter.hpp:167
System components used by the System, Device, Plc or Io domains.
void WriteObjectString(const String &value)
Writes a string in object format as Utf8 string.
Definition: RscWriter.hpp:265