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

Utility.ct

Go to the documentation of this file.
00001 // $Id: Utility.ct 1781 2011-04-11 12:10:19Z tho $
00002 //
00003 // Copyright (C) 2009
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 "Utility.ih"
00027 
00028 // Custom includes
00029 #include <sstream>
00030 #include <limits>
00031 #include <boost/format.hpp>
00032 #include "Traits.hh"
00033 
00034 #define prefix_
00035 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00036 
00037 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00038 // senf::console::ArgumentTraits< CharAsString<CharT> >
00039 
00040 template <class CharT>
00041 prefix_ void senf::console::ArgumentTraits< senf::console::CharAsString<CharT> >::
00042 parse(ParseCommandInfo::TokensRange const & tokens, CharAsString<CharT> & out)
00043 {
00044     std::string v;
00045     console::parse(tokens,v);
00046     if (v.size() != 1)
00047         throw SyntaxErrorException("Invalid size of character constant");
00048     out.value = static_cast<CharT>(v[0]);
00049 }
00050 
00051 template <class CharT>
00052 prefix_ std::string
00053 senf::console::ArgumentTraits< senf::console::CharAsString<CharT> >::description()
00054 {
00055     return std::numeric_limits<CharT>::is_signed ? "char" : "uchar";
00056 }
00057 
00058 template <class CharT>
00059 prefix_ std::string senf::console::ArgumentTraits< senf::console::CharAsString<CharT> >::
00060 str(CharAsString<CharT> value)
00061 {
00062     return console::str(std::string(1,value.value));
00063 }
00064 
00065 template <class CharT>
00066 prefix_ void senf::console::ReturnValueTraits< senf::console::CharAsString<CharT> >::
00067 format(CharAsString<CharT> value, std::ostream & os)
00068 {
00069     console::format(std::string(1,value.value),os);
00070 }
00071 
00072 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00073 // senf::console::ArgumentTraits< senf::console::ValueRange<T> >
00074 
00075 template <class T>
00076 prefix_ void senf::console::ArgumentTraits< senf::console::ValueRange<T> >::
00077 parse(ParseCommandInfo::TokensRange const & tokens, type & out)
00078 {
00079     if (tokens.size() != 1)
00080         throw SyntaxErrorException("parameter syntax error");
00081     std::string v (tokens.begin()[0].value());
00082     std::string::size_type i (v.find(':'));
00083     try {
00084         if (i == std::string::npos)
00085             out.low = out.high = boost::lexical_cast<T>(v);
00086         else {
00087             out.low = boost::lexical_cast<T>(v.substr(0,i));
00088             out.high = boost::lexical_cast<T>(v.substr(i+1));
00089         }
00090     }
00091     catch (std::bad_cast & ex) {
00092         throw SyntaxErrorException("parameter syntax error");
00093     }
00094 }
00095 
00096 template <class T>
00097 prefix_ std::string senf::console::ArgumentTraits< senf::console::ValueRange<T> >::
00098 description()
00099 {
00100     return (boost::format("range<%s>") % ArgumentTraits<T>::description()).str();
00101 }
00102 
00103 template <class T>
00104 prefix_ std::string senf::console::ArgumentTraits< senf::console::ValueRange<T> >::
00105 str(type const & value)
00106 {
00107     std::stringstream ss;
00108     senf::console::format(value, ss);
00109     return ss.str();
00110 }
00111 
00112 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00113 // senf::console::ReturnValueTraits< senf::console::ValueRange<T> >
00114 
00115 template <class T>
00116 prefix_ void senf::console::ReturnValueTraits< senf::console::ValueRange<T> >::
00117 format(type const & value, std::ostream & os)
00118 {
00119     os << senf::console::str(value.low);
00120     if (value.low != value.high)
00121         os << ':' << senf::console::str(value.high);
00122 }
00123 
00124 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00125 // senf::console::ArgumentTraits< senf::console::FlagCollection<Enum> >
00126 
00127 template <class Enum>
00128 prefix_ void senf::console::ArgumentTraits< senf::console::FlagCollection<Enum> >::
00129 parse(ParseCommandInfo::TokensRange const & tokens, type & out)
00130 {
00131     CheckedArgumentIteratorWrapper arg (tokens);
00132     out.value = 0;
00133     while (arg) {
00134         Enum v;
00135         console::parse( *(arg++), v);
00136         out.value |= v;
00137     }
00138 }
00139 
00140 template <class Enum>
00141 prefix_ std::string
00142 senf::console::ArgumentTraits< senf::console::FlagCollection<Enum> >::description()
00143 {
00144     return ArgumentTraits<Enum>::description();
00145 }
00146 
00147 template <class Enum>
00148 prefix_ std::string
00149 senf::console::ArgumentTraits< senf::console::FlagCollection<Enum> >::str(type const & value)
00150 {
00151     std::stringstream ss;
00152     console::format(value, ss);
00153     return ss.str();
00154 }
00155 
00156 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00157 // senf::console::ReturnValueTraits< senf::console::FlagCollection<Enum> >
00158 
00159 template <class Enum>
00160 prefix_ void senf::console::ReturnValueTraits< senf::console::FlagCollection<Enum> >::
00161 format(type const & value, std::ostream & os)
00162 {
00163     unsigned n (0);
00164     std::stringstream ss;
00165     unsigned long flag (1);
00166     for (unsigned bit (0); bit<sizeof(value.value)*CHAR_BIT; ++bit, flag<<=1) {
00167         if (value.value & flag) {
00168             if (n++) ss << " ";
00169             console::format(static_cast<Enum>(flag), ss);
00170         }
00171     }
00172     os << (n != 1 ? "(" + ss.str() + ")" : ss.str());
00173 }
00174 
00175 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00176 #undef prefix_
00177 
00178 
00179 // Local Variables:
00180 // mode: c++
00181 // fill-column: 100
00182 // comment-column: 40
00183 // c-file-style: "senf"
00184 // indent-tabs-mode: nil
00185 // ispell-local-dictionary: "american"
00186 // compile-command: "scons -u test"
00187 // End:

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