Search:

SENF Extensible Network Framework

  • Home
  • Download
  • Wiki
  • BerliOS
  • ChangeLog
  • Browse SVN
  • Bug Tracker
  • Overview
  • Examples
  • HowTos
  • Glossary
  • PPI
  • Packets
  • Scheduler
  • Socket
  • Utils
  • Console
  • Daemon
  • Logger
  • Termlib
  • Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

Traits.cti

Go to the documentation of this file.
00001 // $Id: Traits.cti 1781 2011-04-11 12:10:19Z tho $
00002 //
00003 // Copyright (C) 2008
00004 // Fraunhofer (FOKUS)
00005 // Competence Center NETwork research (NET), St. Augustin, GERMANY
00006 //     Stefan Bund <g0dil@berlios.de>
00007 //
00008 // This program is free software; you can redistribute it and/or modify
00009 // it under the terms of the GNU General Public License as published by
00010 // the Free Software Foundation; either version 2 of the License, or
00011 // (at your option) any later version.
00012 //
00013 // This program is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU General Public License
00019 // along with this program; if not, write to the
00020 // Free Software Foundation, Inc.,
00021 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00022 
00026 #include "Traits.ih"
00027 
00028 // Custom includes
00029 #include <sstream>
00030 #include <boost/format.hpp>
00031 #include <senf/Utils/TypeInfo.hh>
00032 
00033 #define prefix_ inline
00034 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00035 
00036 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00037 // senf::console::detail::ReturnValueTraits<Type>
00038 
00039 template <class Type>
00040 prefix_ void senf::console::ReturnValueTraits<Type>::format(Type const & value,
00041                                                             std::ostream & os)
00042 {
00043     senf_console_format_value(value, os);
00044 }
00045 
00046 template <class Type>
00047 prefix_ void senf::console::senf_console_format_value(Type const & value, std::ostream & os)
00048 {
00049     os << value;
00050 }
00051 
00052 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00053 // senf::console::ArgumentTraits<Type>
00054 
00055 template <class Type>
00056 prefix_ void senf::console::ArgumentTraits<Type>::
00057 parse(ParseCommandInfo::TokensRange const & tokens, Type & out)
00058 {
00059     senf_console_parse_argument(tokens,out);
00060 }
00061 
00062 template <class Type>
00063 prefix_ void senf::console::parse(ParseCommandInfo::TokensRange const & tokens, Type & out)
00064 {
00065     ArgumentTraits<Type>::parse(tokens, out);
00066 }
00067 
00068 template <class Type>
00069 prefix_ std::string senf::console::str(Type const & value)
00070 {
00071     return ArgumentTraits<Type>::str(value);
00072 }
00073 
00074 template <class Type>
00075 prefix_ void senf::console::format(Type const & value, std::ostream & os)
00076 {
00077     ReturnValueTraits<Type>::format(value, os);
00078 }
00079 
00080 template <class Type>
00081 prefix_ std::string senf::console::ArgumentTraits<Type>::description()
00082 {
00083     return prettyBaseName(typeid(Type));
00084 }
00085 
00086 template <class Type>
00087 prefix_ std::string senf::console::ArgumentTraits<Type>::str(Type const & value)
00088 {
00089     std::stringstream ss;
00090     senf::console::format(value, ss);
00091     std::string rv (ss.str());
00092 
00093     if (rv.empty() || ! boost::algorithm::all(rv, CommandParser::isWordChar)) {
00094         for (std::string::size_type i (0); i < rv.size(); ++i)
00095             if (rv[i] == '"' || rv[i] == '\\')
00096                 rv.insert(i++,"\\");
00097             else if (rv[i] < ' ' || rv[i] > 126) {
00098                 rv.insert(i+1, (boost::format("x%02x")
00099                                 % unsigned(static_cast<unsigned char>(rv[i]))).str().c_str());
00100                 rv[i] = '\\';
00101                 i += 3;
00102             }
00103 
00104         rv.insert(0,"\"");
00105         rv.push_back('"');
00106         return rv;
00107     }
00108 
00109     return ss.str();
00110 }
00111 
00112 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00113 // senf::console::detail::CharArgumentTraits<CharT>
00114 
00115 template <class CharT>
00116 prefix_ void senf::console::detail::CharArgumentTraits<CharT>::
00117 parse(ParseCommandInfo::TokensRange const & tokens, CharT & out)
00118 {
00119     typename base::type v;
00120     base::parse(tokens,v);
00121     out = v;
00122 }
00123 
00124 template <class CharT>
00125 prefix_ std::string senf::console::detail::CharArgumentTraits<CharT>::description()
00126 {
00127     return std::numeric_limits<CharT>::is_signed ? "byte" : "ubyte";
00128 }
00129 
00130 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00131 #undef prefix_
00132 
00133 
00134 // Local Variables:
00135 // mode: c++
00136 // fill-column: 100
00137 // comment-column: 40
00138 // c-file-style: "senf"
00139 // indent-tabs-mode: nil
00140 // ispell-local-dictionary: "american"
00141 // compile-command: "scons -u test"
00142 // End:

Contact: senf-dev@lists.berlios.de | © 2006-2010 Fraunhofer Institute for Open Communication Systems, Network Research