00001 /*************************************************************************** 00002 tag: Peter Soetens Mon Jan 19 14:11:20 CET 2004 TableHeaderMarshaller.hpp 00003 00004 TableHeaderMarshaller.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_TABLEHEADER_SERIALIZER 00039 #define PI_PROPERTIES_TABLEHEADER_SERIALIZER 00040 00041 #include <rtt/Property.hpp> 00042 #include <rtt/base/PropertyIntrospection.hpp> 00043 #include <rtt/marsh/StreamProcessor.hpp> 00044 00045 namespace RTT 00046 { 00054 template<typename o_stream> 00055 class TableHeaderMarshaller 00056 : public marsh::MarshallInterface, public marsh::StreamProcessor<o_stream> 00057 { 00058 int level; 00059 int line; 00060 std::vector<std::string> header; 00061 public: 00062 typedef o_stream output_stream; 00063 typedef o_stream OutputStream; 00064 00065 TableHeaderMarshaller(output_stream &os) : 00066 marsh::StreamProcessor<o_stream>(os), level(0), line(1) 00067 { 00068 // sits ready for storing next column aligning. 00069 header.push_back(std::string("")); 00070 } 00071 00072 virtual ~TableHeaderMarshaller() {} 00073 00074 virtual void serialize(base::PropertyBase* v) 00075 { 00076 Property<PropertyBag>* bag = dynamic_cast< Property<PropertyBag>* >( v ); 00077 if ( bag ) 00078 this->serialize( *bag ); 00079 else 00080 store( v->getName() ); 00081 } 00082 00083 00084 virtual void serialize(const PropertyBag &v) 00085 { 00086 // A Bag has no name 00087 // 00088 00089 //*s <<"| Data Set <"<<v.getType()<<"> containing :"<< std::endl <<"| "; 00090 //++line; 00091 /* 00092 for ( 00093 PropertyBag::const_iterator i = v.getProperties().begin(); 00094 i != v.getProperties().end(); 00095 i++ ) 00096 { 00097 // *s << (*i)->getName() <<" | "; 00098 store( (*i)->getName() ); 00099 } 00100 */ 00101 ++level; 00102 //++line; 00103 //*s << " |"<<std::endl; 00104 for ( 00105 PropertyBag::const_iterator i = v.getProperties().begin(); 00106 i != v.getProperties().end(); 00107 i++ ) 00108 { 00109 00110 this->serialize(*i); 00111 } 00112 --level; 00113 //*s << " |"<<std::endl; 00114 } 00115 00119 int store(const std::string& s) 00120 { 00121 if ( line == int(header.size()) ) 00122 { 00123 // next line 00124 header.push_back(std::string("")); 00125 } 00126 header[line-1] += std::string(" | ") + s; 00127 00128 return header[line-1].length(); 00129 } 00130 00131 virtual void serialize(const Property<PropertyBag> &v) 00132 { 00133 if ( line == int(header.size() ) ) 00134 header.push_back(std::string("")); 00138 if ( int(header[line-1].length()) - int(header[line].length()) > 0 ) 00139 { 00140 // add separator 00141 header[line] += std::string(" | "); 00142 // pad further if necessary. 00143 if ( int(header[line-1].length()) - int(header[line].length()) > 0 ) 00144 header[line] += std::string( header[line-1].length() - header[line].length() ,' '); 00145 } 00146 00151 std::string name = v.getName(); 00152 if ( v.value().getType() != "type_less") 00153 name+= std::string(" <") + v.value().getType() + std::string(">"); 00154 store( name ) ; 00155 00159 line++; 00160 if ( v.value().getProperties().empty() ) 00161 store( std::string("<empty>") ); 00162 else 00163 serialize(v.value()); 00164 line--; 00165 00169 if ( int(header[line].length()) - int(header[line -1].length()) > 0) 00170 header[line-1] += std::string( header[line].length() - header[line-1].length(), ' '); 00171 } 00172 00173 virtual void flush() 00174 { 00175 for (std::vector<std::string>::iterator it = header.begin(); it != header.end(); ++it) 00176 if ( !it->empty()) 00177 *this->s << *it <<std::string(" |")<<std::endl; 00178 // TODO : buffer for formatting and flush here. 00179 level = 0; 00180 line = 1; 00181 header.clear(); 00182 header.push_back(std::string("")); 00183 } 00184 }; 00185 } 00186 #endif