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