OrocosComponentLibrary  2.8.3
NiceHeaderMarshaller.hpp
1 /***************************************************************************
2  tag: Peter Soetens Mon Jan 19 14:11:20 CET 2004 NiceHeaderMarshaller.hpp
3 
4  NiceHeaderMarshaller.hpp - description
5  -------------------
6  begin : Mon January 19 2004
7  copyright : (C) 2004 Peter Soetens
8  email : peter.soetens@mech.kuleuven.ac.be
9 
10  ***************************************************************************
11  * This library is free software; you can redistribute it and/or *
12  * modify it under the terms of the GNU General Public *
13  * License as published by the Free Software Foundation; *
14  * version 2 of the License. *
15  * *
16  * As a special exception, you may use this file as part of a free *
17  * software library without restriction. Specifically, if other files *
18  * instantiate templates or use macros or inline functions from this *
19  * file, or you compile this file and link it with other files to *
20  * produce an execunice, this file does not by itself cause the *
21  * resulting execunice to be covered by the GNU General Public *
22  * License. This exception does not however invalidate any other *
23  * reasons why the execunice file might be covered by the GNU General *
24  * Public License. *
25  * *
26  * This library is distributed in the hope that it will be useful, *
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
29  * Lesser General Public License for more details. *
30  * *
31  * You should have received a copy of the GNU General Public *
32  * License along with this library; if not, write to the Free Software *
33  * Foundation, Inc., 59 Temple Place, *
34  * Suite 330, Boston, MA 02111-1307 USA *
35  * *
36  ***************************************************************************/
37 
38 #ifndef PI_PROPERTIES_NICEHEADER_SERIALIZER
39 #define PI_PROPERTIES_NICEHEADER_SERIALIZER
40 
41 #include <rtt/Property.hpp>
42 #include <rtt/marsh/StreamProcessor.hpp>
43 #include <rtt/marsh/MarshallInterface.hpp>
44 
45 namespace RTT
46 {
54  template<typename o_stream>
56  : public marsh::MarshallInterface, public marsh::StreamProcessor<o_stream>
57  {
58  bool did_comment;
59  int nameless_counter;
60  std::string prefix;
61  public:
62  typedef o_stream output_stream;
63  typedef o_stream OutputStream;
64 
65  NiceHeaderMarshaller(output_stream &os) :
66  marsh::StreamProcessor<o_stream>(os),
67  did_comment(false), nameless_counter(0)
68  {
69  }
70 
71  virtual ~NiceHeaderMarshaller() {}
72 
73  virtual void serialize(base::PropertyBase* v)
74  {
75  Property<PropertyBag>* bag = dynamic_cast< Property<PropertyBag>* >( v );
76  if ( bag )
77  this->serialize( *bag );
78  else
79  store( v->getName() );
80  }
81 
82 
83  virtual void serialize(const PropertyBag &v)
84  {
85  // start with a comment sign.
86  if (did_comment == false )
87  *this->s << "";
88  did_comment = true;
89 
90  for (
91  PropertyBag::const_iterator i = v.getProperties().begin();
92  i != v.getProperties().end();
93  i++ )
94  {
95  this->serialize(*i);
96  }
97  }
98 
102  void store(const std::string& name)
103  {
104  if ( name.empty() )
105  ++nameless_counter;
106  else
107  nameless_counter = 0;
108  if ( !prefix.empty() )
109  *this->s << ' ' << prefix << '.';
110  else
111  *this->s << ' ';
112  if ( nameless_counter )
113  *this->s << nameless_counter;
114  else
115  *this->s << name;
116  }
117 
118  virtual void serialize(const Property<PropertyBag> &v)
119  {
120  if ( v.rvalue().empty() )
121  store( v.getName() + "[0]" );
122  else {
123  std::string oldpref = prefix;
124  if ( prefix.empty() )
125  prefix = v.getName();
126  else
127  prefix += '.' + v.getName();
128 
129  serialize(v.rvalue());
130 
131  prefix = oldpref;
132  nameless_counter = 0;
133  }
134  }
135 
136  virtual void flush()
137  {
138  did_comment = false;
139  nameless_counter = 0;
140  *this->s << std::endl;
141  this->s->flush();
142 
143  }
144  };
145 }
146 #endif
void store(const std::string &name)
A marsh::MarshallInterface for generating headers usable for interpretation by plot programs...
Definition: Category.hpp:10