From: g0dil Date: Fri, 13 Nov 2009 15:29:24 +0000 (+0000) Subject: Utils/Console: Add CharAsString utility X-Git-Url: http://g0dil.de/git?a=commitdiff_plain;h=ea5f02fb88dfab3932368bbafdc863b8ad585cc1;p=senf.git Utils/Console: Add CharAsString utility git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@1527 270642c3-0616-0410-b53a-bc976706d245 --- diff --git a/senf/Utils/Console/Traits.cti b/senf/Utils/Console/Traits.cti index f6dd892..2d9c65a 100644 --- a/senf/Utils/Console/Traits.cti +++ b/senf/Utils/Console/Traits.cti @@ -106,7 +106,7 @@ parse(ParseCommandInfo::TokensRange const & tokens, CharT & out) template prefix_ std::string senf::console::detail::CharArgumentTraits::description() { - return std::numeric_limits::is_signed ? "byte" : "unsigned byte"; + return std::numeric_limits::is_signed ? "byte" : "ubyte"; } ///////////////////////////////cti.e/////////////////////////////////////// diff --git a/senf/Utils/Console/Utility.ct b/senf/Utils/Console/Utility.ct index 2f12892..747a6be 100644 --- a/senf/Utils/Console/Utility.ct +++ b/senf/Utils/Console/Utility.ct @@ -27,12 +27,49 @@ // Custom includes #include +#include #include +#include "Traits.hh" #define prefix_ ///////////////////////////////ct.p//////////////////////////////////////// /////////////////////////////////////////////////////////////////////////// +// senf::console::ArgumentTraits< CharAsString > + +template +prefix_ void senf::console::ArgumentTraits< senf::console::CharAsString >:: +parse(ParseCommandInfo::TokensRange const & tokens, CharAsString & out) +{ + std::string v; + senf::console::parse(tokens,v); + if (v.size() != 1) + throw SyntaxErrorException("Invalid size of character constant"); + out.value = static_cast(v[0]); +} + +template +prefix_ std::string +senf::console::ArgumentTraits< senf::console::CharAsString >::description() +{ + return std::numeric_limits::is_signed ? "char" : "uchar"; +} + +template +prefix_ std::string senf::console::ArgumentTraits< senf::console::CharAsString >:: +str(CharAsString value) +{ + return senf::console::str(std::string(1,value.value)); +} + +template +prefix_ void senf::console::ReturnValueTraits< senf::console::CharAsString >:: +format(CharAsString value, std::ostream & os) +{ + return senf::console::format(std::string(1,value.value),os); +} + +/////////////////////////////////////////////////////////////////////////// // senf::console::ArgumentTraits< senf::console::ValueRange > template diff --git a/senf/Utils/Console/Utility.cti b/senf/Utils/Console/Utility.cti new file mode 100644 index 0000000..52fedd1 --- /dev/null +++ b/senf/Utils/Console/Utility.cti @@ -0,0 +1,62 @@ +// $Id$ +// +// Copyright (C) 2009 +// Fraunhofer Institute for Open Communication Systems (FOKUS) +// Competence Center NETwork research (NET), St. Augustin, GERMANY +// Stefan Bund +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the +// Free Software Foundation, Inc., +// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +/** \file + \brief Utility inline template implementation */ + +//#include "Utility.ih" + +// Custom includes + +#define prefix_ inline +///////////////////////////////cti.p/////////////////////////////////////// + +template +prefix_ senf::console::CharAsString::CharAsString() + : value () +{} + +template +prefix_ senf::console::CharAsString::CharAsString(CharT value_) + : value (value_) +{} + +template +prefix_ senf::console::CharAsString::operator CharT () + const +{ + return value; +} + +///////////////////////////////cti.e/////////////////////////////////////// +#undef prefix_ + + +// Local Variables: +// mode: c++ +// fill-column: 100 +// comment-column: 40 +// c-file-style: "senf" +// indent-tabs-mode: nil +// ispell-local-dictionary: "american" +// compile-command: "scons -u test" +// End: diff --git a/senf/Utils/Console/Utility.hh b/senf/Utils/Console/Utility.hh index 6ccc579..5c2b263 100644 --- a/senf/Utils/Console/Utility.hh +++ b/senf/Utils/Console/Utility.hh @@ -36,6 +36,38 @@ namespace senf { namespace console { + template + struct CharAsString + { + CharAsString(); + CharAsString(CharT value_); + operator CharT () const; + CharT value; + }; + +#ifndef DOXYGEN + + template + struct ArgumentTraits< CharAsString > + { + typedef CharAsString type; + static bool const singleToken = true; + + static void parse(ParseCommandInfo::TokensRange const & tokens, CharAsString & out); + static std::string description(); + static std::string str(CharAsString value); + }; + + template + struct ReturnValueTraits< CharAsString > + { + typedef CharAsString type; + + static void format(CharAsString value, std::ostream & os); + }; + +#endif + /** \brief Value range A value range may be represented in the console either by a single value (setting both \a @@ -147,7 +179,7 @@ namespace console { ///////////////////////////////hh.e//////////////////////////////////////// //#include "Utility.cci" #include "Utility.ct" -//#include "Utility.cti" +#include "Utility.cti" #endif diff --git a/senf/Utils/Console/Utility.test.cc b/senf/Utils/Console/Utility.test.cc index f5ff939..2ec5ab7 100644 --- a/senf/Utils/Console/Utility.test.cc +++ b/senf/Utils/Console/Utility.test.cc @@ -42,6 +42,8 @@ namespace { + char charTest(char value) { return value; } + enum TestEnum { Foo=1, Bar=2, FooBar=4 }; SENF_CONSOLE_REGISTER_ENUM( TestEnum, (Foo)(Bar)(FooBar) ); @@ -50,6 +52,24 @@ namespace { } +BOOST_AUTO_UNIT_TEST(charAsString) +{ + senf::console::Executor executor; + senf::console::CommandParser parser; + senf::console::ScopedDirectory<> dir; + senf::console::root().add("test", dir); + std::stringstream ss; + + dir.add("test", boost::function< + senf::console::CharAsString (senf::console::CharAsString)>(&charTest)); + + ss.str(""); + SENF_CHECK_NO_THROW( + parser.parse("test/test \"\\x01\"", + boost::bind( boost::ref(executor), boost::ref(ss), _1 )) ); + BOOST_CHECK_EQUAL( ss.str(), "\x01\n" ); +} + BOOST_AUTO_UNIT_TEST(flagCollection) { senf::console::Executor executor;