MQSerializationProtocol.hpp
Go to the documentation of this file.
00001 /***************************************************************************
00002   tag: Peter Soetens  Thu Oct 22 11:59:07 CEST 2009  MQSerializationProtocol.hpp
00003 
00004                         MQSerializationProtocol.hpp -  description
00005                            -------------------
00006     begin                : Thu October 22 2009
00007     copyright            : (C) 2009 Peter Soetens
00008     email                : peter@thesourcworks.com
00009 
00010  ***************************************************************************
00011  *   This library is free software; you can redistribute it and/or         *
00012  *   modify it under the terms of the GNU General Public                   *
00013  *   License as published by the Free Software Foundation;                 *
00014  *   version 2 of the License.                                             *
00015  *                                                                         *
00016  *   As a special exception, you may use this file as part of a free       *
00017  *   software library without restriction.  Specifically, if other files   *
00018  *   instantiate templates or use macros or inline functions from this     *
00019  *   file, or you compile this file and link it with other files to        *
00020  *   produce an executable, this file does not by itself cause the         *
00021  *   resulting executable to be covered by the GNU General Public          *
00022  *   License.  This exception does not however invalidate any other        *
00023  *   reasons why the executable file might be covered by the GNU General   *
00024  *   Public License.                                                       *
00025  *                                                                         *
00026  *   This library is distributed in the hope that it will be useful,       *
00027  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00028  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00029  *   Lesser General Public License for more details.                       *
00030  *                                                                         *
00031  *   You should have received a copy of the GNU General Public             *
00032  *   License along with this library; if not, write to the Free Software   *
00033  *   Foundation, Inc., 59 Temple Place,                                    *
00034  *   Suite 330, Boston, MA  02111-1307  USA                                *
00035  *                                                                         *
00036  ***************************************************************************/
00037 
00038 
00039 /*
00040  * MqueueSerializationProtocol.hpp
00041  *
00042  *  Created on: Sep 21, 2009
00043  *      Author: kaltan
00044  */
00045 
00046 #ifndef MQSERIALIZATIONPROTOCOL_HPP_
00047 #define MQSERIALIZATIONPROTOCOL_HPP_
00048 
00049 #include "MQTemplateProtocol.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::MQTemplateProtocol<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                     // we use the boost iostreams library for re-using the blob buffer in the stream object.
00074                     // and the serialization library to write the data into stream.
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                 //std::cout << "sample size is "<< tsample->rvalue().size() <<" archive is " << out.getArchiveSize() <<std::endl; //disable all types but std::vector<double> for this to compile
00110                 return out.getArchiveSize();
00111             }
00112         };
00113 
00114     }
00115 
00116 }
00117 
00118 #endif /* MQSERIALIZATIONPROTOCOL_HPP_ */


rtt
Author(s): RTT Developers
autogenerated on Mon Oct 6 2014 03:13:37