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 #ifndef PI_PROPERTIES_NICEHEADER_SERIALIZER
00039 #define PI_PROPERTIES_NICEHEADER_SERIALIZER
00040
00041 #include <rtt/Property.hpp>
00042 #include <rtt/marsh/StreamProcessor.hpp>
00043 #include <rtt/marsh/MarshallInterface.hpp>
00044
00045 namespace RTT
00046 {
00054 template<typename o_stream>
00055 class NiceHeaderMarshaller
00056 : public marsh::MarshallInterface, public marsh::StreamProcessor<o_stream>
00057 {
00058 bool did_comment;
00059 int nameless_counter;
00060 std::string prefix;
00061 public:
00062 typedef o_stream output_stream;
00063 typedef o_stream OutputStream;
00064
00065 NiceHeaderMarshaller(output_stream &os) :
00066 marsh::StreamProcessor<o_stream>(os),
00067 did_comment(false), nameless_counter(0)
00068 {
00069 }
00070
00071 virtual ~NiceHeaderMarshaller() {}
00072
00073 virtual void serialize(base::PropertyBase* v)
00074 {
00075 Property<PropertyBag>* bag = dynamic_cast< Property<PropertyBag>* >( v );
00076 if ( bag )
00077 this->serialize( *bag );
00078 else
00079 store( v->getName() );
00080 }
00081
00082
00083 virtual void serialize(const PropertyBag &v)
00084 {
00085
00086 if (did_comment == false )
00087 *this->s << "";
00088 did_comment = true;
00089
00090 for (
00091 PropertyBag::const_iterator i = v.getProperties().begin();
00092 i != v.getProperties().end();
00093 i++ )
00094 {
00095
00096 this->serialize(*i);
00097 }
00098 }
00099
00103 void store(const std::string& name)
00104 {
00105 if ( name.empty() )
00106 ++nameless_counter;
00107 else
00108 nameless_counter = 0;
00109
00110 if ( !prefix.empty() )
00111 *this->s << " " << prefix << ".";
00112 else
00113 *this->s << " ";
00114 if ( nameless_counter )
00115 *this->s << nameless_counter;
00116 else
00117 *this->s << name;
00118 }
00119
00120 virtual void serialize(const Property<PropertyBag> &v)
00121 {
00122 std::string oldpref = prefix;
00123 prefix += "." + v.getName();
00124
00125 serialize(v.get());
00126
00127 prefix = oldpref;
00128 nameless_counter = 0;
00129 }
00130
00131 virtual void flush()
00132 {
00133 did_comment = false;
00134 nameless_counter = 0;
00135 *this->s << std::endl;
00136 this->s->flush();
00137
00138 }
00139 };
00140 }
00141 #endif