Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046 #ifndef MQSERIALIZATIONPROTOCOL_HPP_
00047 #define MQSERIALIZATIONPROTOCOL_HPP_
00048
00049 #include "MQTemplateProtocolBase.hpp"
00050 #include "binary_data_archive.hpp"
00051 #include <boost/iostreams/stream.hpp>
00052 #include <boost/iostreams/device/array.hpp>
00053 #include <iostream>
00054 namespace RTT
00055 {
00056
00057 namespace mqueue
00058 {
00059
00060 template<class T>
00061 class MQSerializationProtocol
00062 : public RTT::mqueue::MQTemplateProtocolBase<T>
00063 {
00064 public:
00065 MQSerializationProtocol() {
00066 }
00067
00068 virtual std::pair<void const*,int> fillBlob( base::DataSourceBase::shared_ptr source, void* blob, int size, void* cookie) const
00069 {
00070 namespace io = boost::iostreams;
00071 typename internal::DataSource<T>::shared_ptr d = boost::dynamic_pointer_cast< internal::DataSource<T> >( source );
00072 if ( d ) {
00073
00074
00075 io::stream<io::array_sink> outbuf( (char*)blob, size);
00076 binary_data_oarchive out( outbuf );
00077 out << d->rvalue();
00078 return std::make_pair( blob, out.getArchiveSize() );
00079 }
00080 return std::make_pair((void*)0,int(0));
00081 }
00082
00086 virtual bool updateFromBlob(const void* blob, int size, base::DataSourceBase::shared_ptr target, void* cookie) const {
00087 namespace io = boost::iostreams;
00088 typename internal::AssignableDataSource<T>::shared_ptr ad = internal::AssignableDataSource<T>::narrow( target.get() );
00089 if ( ad ) {
00090 io::stream<io::array_source> inbuf((const char*)blob, size);
00091 binary_data_iarchive in( inbuf );
00092 in >> ad->set();
00093 return true;
00094 }
00095 return false;
00096 }
00097
00098 virtual unsigned int getSampleSize(base::DataSourceBase::shared_ptr sample, void* cookie) const {
00099 typename internal::DataSource<T>::shared_ptr tsample = boost::dynamic_pointer_cast< internal::DataSource<T> >( sample );
00100 if ( ! tsample ) {
00101 log(Error) << "getSampleSize: sample has wrong type."<<endlog();
00102 return 0;
00103 }
00104 namespace io = boost::iostreams;
00105 char sink[1];
00106 io::stream<io::array_sink> outbuf(sink,1);
00107 binary_data_oarchive out( outbuf, false );
00108 out << tsample->get();
00109
00110 return out.getArchiveSize();
00111 }
00112 };
00113
00114 }
00115
00116 }
00117
00118 #endif