stream.h
Go to the documentation of this file.
1 
12 
13 #ifndef __OPC_UA_MAPPING_SERIALIZE_H__
14 #define __OPC_UA_MAPPING_SERIALIZE_H__
15 
18 
19 #include <memory>
20 #include <vector>
21 
22 #define OPCUA_DEFAULT_BUFFER_SIZE 4096
23 
24 namespace OpcUa
25 {
26 namespace Binary
27 {
28 
30 {
31 public:
32  explicit DataSerializer(std::size_t defBufferSize = OPCUA_DEFAULT_BUFFER_SIZE)
33  {
34  Buffer.reserve(defBufferSize);
35  }
36 
37  template <typename T>
38  DataSerializer & operator<<(const T & value)
39  {
40  Serialize<T>(value);
41  return *this;
42  }
43 
44  template <typename Acceptor>
45  void Flush(Acceptor & aceptor)
46  {
47  aceptor.Send(&Buffer[0], Buffer.size());
48  Buffer.clear();
49  }
50 
51  template<typename T>
52  void Serialize(const T & value);
53 
54 private:
55  std::vector<char> Buffer;
56 };
57 
59 {
60 public:
61  virtual size_t Read(char * buffer, size_t size) = 0;
62 };
63 
65 {
66 public:
68  : In(supplier)
69  {
70  }
71 
73  {
74  }
75 
76  template <typename T>
78  {
79  Deserialize<T>(value);
80  return *this;
81  }
82 
83  template <typename T>
84  void Deserialize(T &);
85 
86 private:
88 };
89 
90 
91 struct RawMessage
92 {
93  const char * Data;
94  std::size_t Size;
95 
96  RawMessage(const char * data, std::size_t size)
97  : Data(data)
98  , Size(size)
99  {
100  }
101 };
102 
103 
104 template <typename OutputChannelType>
105 class OStream
106 {
107 public:
108  explicit OStream(std::shared_ptr<OutputChannelType> channel)
109  : Out(*channel)
110  , Holder(channel)
111  {
112  }
113 
114  explicit OStream(OutputChannelType & channel)
115  : Out(channel)
116  {
117  }
118 
119  virtual ~OStream()
120  {
121  }
122 
123  template <typename T>
124  OStream & operator<<(const T & value)
125  {
126  Serializer << value;
127  return *this;
128  }
129 
130  OStream & operator<< (OStream & (*pf)(OStream & out))
131  {
132  return pf(*this);
133  }
134 
135  void Flush()
136  {
137  Serializer.Flush(Out);
138  }
139 
140 private:
141  OutputChannelType & Out;
142  std::shared_ptr<OutputChannelType> Holder;
144 };
145 
146 template <typename ChannelType>
148 {
149  os.Flush();
150  return os;
151 }
152 
153 struct RawBuffer
154 {
155  char * Data;
156  std::size_t Size;
157 
158  RawBuffer(char * data, std::size_t size)
159  : Data(data)
160  , Size(size)
161  {
162  }
163 };
164 
165 
166 template <typename InputChannelType>
167 class IStream : private DataSupplier
168 {
169 public:
170  explicit IStream(std::shared_ptr<InputChannelType> channel)
171  : In(*channel)
172  , Holder(channel)
173  , Deserializer(*this)
174  {
175  }
176 
177  explicit IStream(InputChannelType & channel)
178  : In(channel)
179  , Deserializer(*this)
180  {
181  }
182 
183  virtual ~IStream()
184  {
185  }
186 
187  template <typename T>
188  IStream & operator>>(T & value)
189  {
190  Deserializer >> value;
191  return *this;
192  }
193 
194 private:
195  virtual size_t Read(char * buffer, size_t size)
196  {
197  return In.Receive(buffer, size);
198  }
199 
200 private:
201  InputChannelType & In;
202  std::shared_ptr<InputChannelType> Holder;
204 };
205 
206 
207 template <typename IOChannelType>
208 class IOStream : public IStream<IOChannelType>, public OStream<IOChannelType>
209 {
210 public:
211  IOStream(std::shared_ptr<IOChannelType> channel)
212  : IStream<IOChannelType>(channel)
213  , OStream<IOChannelType>(channel)
214  {
215  }
216 };
217 
218 template <typename InputChannelType, typename OutputChannelType>
219 class InputOutputStream : public IStream<InputChannelType>, public OStream<OutputChannelType>
220 {
221 public:
222  InputOutputStream(std::shared_ptr<InputChannelType> input, std::shared_ptr<OutputChannelType> output)
223  : IStream<InputChannelType>(input)
224  , OStream<OutputChannelType>(output)
225  {
226  }
227 };
228 
232 
233 } // namespace Binary
234 } // namespace OpcUa
235 
236 #endif // __OPC_UA_MAPPING_SERIALIZE_H__
237 
OStream(std::shared_ptr< OutputChannelType > channel)
Definition: stream.h:108
IStream & operator>>(T &value)
Definition: stream.h:188
std::shared_ptr< InputChannelType > Holder
Definition: stream.h:202
IStream(InputChannelType &channel)
Definition: stream.h:177
OpcUa::Binary::OStream< OpcUa::OutputChannel > OStreamBinary
Definition: stream.h:231
IOStream(std::shared_ptr< IOChannelType > channel)
Definition: stream.h:211
std::vector< char > Buffer
Definition: stream.h:55
RawBuffer(char *data, std::size_t size)
Definition: stream.h:158
DataDeserializer & operator>>(T &value)
Definition: stream.h:77
DataDeserializer(DataSupplier &supplier)
Definition: stream.h:67
OutputChannelType & Out
Definition: stream.h:141
OStream & operator<<(const T &value)
Definition: stream.h:124
DataSerializer Serializer
Definition: stream.h:143
OStream< ChannelType > & flush(OStream< ChannelType > &os)
Definition: stream.h:147
virtual size_t Read(char *buffer, size_t size)
Definition: stream.h:195
std::shared_ptr< OutputChannelType > Holder
Definition: stream.h:142
InputChannelType & In
Definition: stream.h:201
OStream(OutputChannelType &channel)
Definition: stream.h:114
void Serialize(const T &value)
void Flush(Acceptor &aceptor)
Definition: stream.h:45
DataSerializer(std::size_t defBufferSize=OPCUA_DEFAULT_BUFFER_SIZE)
Definition: stream.h:32
std::size_t Size
Definition: stream.h:156
OPC UA Address space part. GNU LGPL.
#define OPCUA_DEFAULT_BUFFER_SIZE
Opc binary stream. Stream classes perform Serialization/Deserialization of opc ua structures...
Definition: stream.h:22
const char * Binary(const char *input, short n)
DataSerializer & operator<<(const T &value)
Definition: stream.h:38
InputOutputStream(std::shared_ptr< InputChannelType > input, std::shared_ptr< OutputChannelType > output)
Definition: stream.h:222
virtual ~IStream()
Definition: stream.h:183
OpcUa::Binary::IStream< OpcUa::InputChannel > IStreamBinary
Definition: stream.h:230
virtual ~OStream()
Definition: stream.h:119
const char * Data
Definition: stream.h:93
IStream(std::shared_ptr< InputChannelType > channel)
Definition: stream.h:170
OpcUa::Binary::IOStream< OpcUa::IOChannel > IOStreamBinary
Definition: stream.h:229
RawMessage(const char *data, std::size_t size)
Definition: stream.h:96
std::size_t Size
Definition: stream.h:94
DataDeserializer Deserializer
Definition: stream.h:203


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Tue Jan 19 2021 03:12:08