stream.h
Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 #ifndef __OPC_UA_MAPPING_SERIALIZE_H__
00014 #define __OPC_UA_MAPPING_SERIALIZE_H__
00015 
00016 #include <opc/ua/protocol/channel.h>
00017 #include <opc/ua/protocol/binary/common.h>
00018 
00019 #include <memory>
00020 #include <vector>
00021 
00022 #define OPCUA_DEFAULT_BUFFER_SIZE 4096
00023 
00024 namespace OpcUa
00025 {
00026   namespace Binary
00027   {
00028 
00029     class DataSerializer
00030     {
00031     public:
00032       explicit DataSerializer(std::size_t defBufferSize = OPCUA_DEFAULT_BUFFER_SIZE)
00033       {
00034         Buffer.reserve(defBufferSize);
00035       }
00036 
00037       template <typename T>
00038       DataSerializer& operator<<(const T& value)
00039       {
00040         Serialize<T>(value);
00041         return *this;
00042       }
00043 
00044       template <typename Acceptor>
00045       void Flush(Acceptor& aceptor)
00046       {
00047         aceptor.Send(&Buffer[0], Buffer.size());
00048         Buffer.clear();
00049       }
00050 
00051       template<typename T>
00052       void Serialize(const T& value);
00053 
00054     private:
00055       std::vector<char> Buffer;
00056     };
00057 
00058     class DataSupplier
00059     {
00060     public:
00061       virtual size_t Read(char* buffer, size_t size) = 0;
00062     };
00063 
00064     class DataDeserializer
00065     {
00066     public:
00067       DataDeserializer(DataSupplier& supplier)
00068         : In(supplier)
00069       {
00070       }
00071 
00072       virtual ~DataDeserializer()
00073       {
00074       }
00075 
00076       template <typename T>
00077       DataDeserializer& operator>>(T& value)
00078       {
00079         Deserialize<T>(value);
00080         return *this;
00081       }
00082 
00083       template <typename T>
00084       void Deserialize(T&);
00085 
00086     private:
00087       DataSupplier& In;
00088     };
00089 
00090 
00091     struct RawMessage
00092     {
00093       const char* Data;
00094       std::size_t Size;
00095 
00096       RawMessage(const char* data, std::size_t size)
00097         : Data(data)
00098         , Size(size)
00099       {
00100       }
00101     };
00102 
00103 
00104     template <typename OutputChannelType>
00105     class OStream
00106     {
00107     public:
00108       explicit OStream(std::shared_ptr<OutputChannelType> channel)
00109         : Out(*channel)
00110         , Holder(channel)
00111       {
00112       }
00113 
00114       explicit OStream(OutputChannelType& channel)
00115         : Out(channel)
00116       {
00117       }
00118 
00119       virtual ~OStream()
00120       {
00121       }
00122 
00123       template <typename T>
00124       OStream& operator<<(const T& value)
00125       {
00126         Serializer << value;
00127         return *this;
00128       }
00129 
00130       OStream& operator<< (OStream& (*pf)(OStream& out))
00131       {
00132         return pf(*this);
00133       }
00134 
00135       void Flush()
00136       {
00137         Serializer.Flush(Out);
00138       }
00139 
00140     private:
00141       OutputChannelType& Out;
00142       std::shared_ptr<OutputChannelType> Holder;
00143       DataSerializer Serializer;
00144     };
00145 
00146     template <typename ChannelType>
00147     inline OStream<ChannelType>& flush(OStream<ChannelType>& os)
00148     {
00149       os.Flush();
00150       return os;
00151     }
00152 
00153     struct RawBuffer
00154     {
00155       char* Data;
00156       std::size_t Size;
00157 
00158       RawBuffer(char* data, std::size_t size)
00159         : Data(data)
00160         , Size(size)
00161       {
00162       }
00163     };
00164 
00165 
00166     template <typename InputChannelType>
00167     class IStream : private DataSupplier
00168     {
00169     public:
00170       explicit IStream(std::shared_ptr<InputChannelType> channel)
00171         : In(*channel)
00172         , Holder(channel)
00173         , Deserializer(*this)
00174       {
00175       }
00176 
00177       explicit IStream(InputChannelType& channel)
00178         : In(channel)
00179         , Deserializer(*this)
00180       {
00181       }
00182 
00183       virtual ~IStream()
00184       {
00185       }
00186 
00187       template <typename T>
00188       IStream& operator>>(T& value)
00189       {
00190         Deserializer >> value;
00191         return *this;
00192       }
00193 
00194     private:
00195       virtual size_t Read(char* buffer, size_t size)
00196       {
00197         return In.Receive(buffer, size);
00198       }
00199 
00200     private:
00201       InputChannelType& In;
00202       std::shared_ptr<InputChannelType> Holder;
00203       DataDeserializer Deserializer;
00204     };
00205 
00206 
00207     template <typename IOChannelType>
00208     class IOStream : public IStream<IOChannelType>, public OStream<IOChannelType>
00209     {
00210     public:
00211       IOStream(std::shared_ptr<IOChannelType> channel)
00212         : IStream<IOChannelType>(channel)
00213         , OStream<IOChannelType>(channel)
00214       {
00215       }
00216     };
00217 
00218     template <typename InputChannelType, typename OutputChannelType>
00219     class InputOutputStream : public IStream<InputChannelType>, public OStream<OutputChannelType>
00220     {
00221     public:
00222       InputOutputStream(std::shared_ptr<InputChannelType> input, std::shared_ptr<OutputChannelType> output)
00223         : IStream<InputChannelType>(input)
00224         , OStream<OutputChannelType>(output)
00225       {
00226       }
00227     };
00228 
00229     typedef OpcUa::Binary::IOStream<OpcUa::IOChannel> IOStreamBinary;
00230     typedef OpcUa::Binary::IStream<OpcUa::InputChannel> IStreamBinary;
00231     typedef OpcUa::Binary::OStream<OpcUa::OutputChannel> OStreamBinary;
00232 
00233   } // namespace Binary
00234 } // namespace OpcUa
00235 
00236 #endif // __OPC_UA_MAPPING_SERIALIZE_H__
00237 


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Sat Jun 8 2019 18:24:57