SimpleMarshaller.hpp

00001 /***************************************************************************
00002   tag: Peter Soetens  Mon Jan 19 14:11:20 CET 2004  SimpleMarshaller.hpp
00003 
00004                         SimpleMarshaller.hpp -  description
00005                            -------------------
00006     begin                : Mon January 19 2004
00007     copyright            : (C) 2004 Peter Soetens
00008     email                : peter.soetens@mech.kuleuven.ac.be
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 #ifndef PI_PROPERTIES_SIMPLE_MARSHALLER
00039 #define PI_PROPERTIES_SIMPLE_MARSHALLER
00040 
00041 #include "../Property.hpp"
00042 #include "../PropertyIntrospection.hpp"
00043 
00044 #include <iostream>
00045 #include <vector>
00046 #include <map>
00047 #include <string>
00048 #include <sstream>
00049 
00050 namespace RTT
00051 {
00052 
00082     template<typename o_stream>
00083     class SimpleMarshaller
00084         : public Marshaller,
00085           protected PropertyIntrospection
00086     {
00087         static const char TYPECODE_BOOL = 'B';
00088         static const char TYPECODE_CHAR = 'C';
00089         static const char TYPECODE_INT  = 'I';
00090         static const char TYPECODE_UINT  = 'U';
00091         static const char TYPECODE_DOUBLE = 'D';
00092         static const char TYPECODE_STRING = 'S';
00093 
00094         template< class T >
00095         void writeOut(const char tc, const Property<T> &v)
00096         {
00097             std::stringstream buffer;
00098             buffer << v.get();
00099             _os << tc;
00100             _os.put(v.getName().size());
00101             _os << v.getName() << ":";
00102             _os.put( buffer.str().size() );
00103             _os << buffer.str() << ";";
00104         }
00105 
00106         virtual void introspect(PropertyBase* pb)
00107         {
00108             PropertyIntrospection::introspect( pb );
00109         }
00110 
00111         virtual void introspect(Property<bool> &v)
00112         {
00113             writeOut(TYPECODE_BOOL, v);
00114         }
00115 
00116         virtual void introspect(Property<char> &v)
00117         {
00118             writeOut(TYPECODE_CHAR, v);
00119         }
00120 
00121         virtual void introspect(Property<int> &v)
00122         {
00123             writeOut(TYPECODE_INT, v);
00124         }
00125 
00126         virtual void introspect(Property<unsigned int> &v)
00127         {
00128             writeOut(TYPECODE_UINT, v);
00129         }
00130 
00131         virtual void introspect(Property<double> &v)
00132         {
00133             writeOut(TYPECODE_DOUBLE, v);
00134         }
00135 
00136         virtual void introspect(Property<std::string> &v)
00137         {
00138             writeOut(TYPECODE_STRING, v);
00139         }
00140 
00141         virtual void introspect(Property<PropertyBag> &v)
00142         {
00143             serialize(v.get());
00144         }
00145         typedef o_stream output_stream;
00146         typedef o_stream OutputStream;
00147         output_stream &_os;
00148     public:
00149 
00150         SimpleMarshaller(output_stream &os) :
00151             _os(os)
00152         {}
00153 
00154         virtual void flush()
00155         {}
00156 
00157         virtual void serialize(PropertyBase* p)
00158         {
00159             p->identify(this);
00160         }
00161 
00162         virtual void serialize(const PropertyBag &v)
00163         {
00164             v.identify(this);
00165         }
00166     };
00167 }
00168 #endif
Generated on Thu Dec 23 13:22:38 2010 for Orocos Real-Time Toolkit by  doxygen 1.6.3