4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 \brief Utility non-inline template implementation */
26 //#include "Utility.ih"
31 #include <boost/format.hpp>
35 ///////////////////////////////ct.p////////////////////////////////////////
37 ///////////////////////////////////////////////////////////////////////////
38 // senf::console::ArgumentTraits< CharAsString<CharT> >
40 template <class CharT>
41 prefix_ void senf::console::ArgumentTraits< senf::console::CharAsString<CharT> >::
42 parse(ParseCommandInfo::TokensRange const & tokens, CharAsString<CharT> & out)
45 senf::console::parse(tokens,v);
47 throw SyntaxErrorException("Invalid size of character constant");
48 out.value = static_cast<CharT>(v[0]);
51 template <class CharT>
53 senf::console::ArgumentTraits< senf::console::CharAsString<CharT> >::description()
55 return std::numeric_limits<CharT>::is_signed ? "char" : "uchar";
58 template <class CharT>
59 prefix_ std::string senf::console::ArgumentTraits< senf::console::CharAsString<CharT> >::
60 str(CharAsString<CharT> value)
62 return senf::console::str(std::string(1,value.value));
65 template <class CharT>
66 prefix_ void senf::console::ReturnValueTraits< senf::console::CharAsString<CharT> >::
67 format(CharAsString<CharT> value, std::ostream & os)
69 return senf::console::format(std::string(1,value.value),os);
72 ///////////////////////////////////////////////////////////////////////////
73 // senf::console::ArgumentTraits< senf::console::ValueRange<T> >
76 prefix_ void senf::console::ArgumentTraits< senf::console::ValueRange<T> >::
77 parse(ParseCommandInfo::TokensRange const & tokens, type & out)
79 if (tokens.size() != 1)
80 throw senf::console::SyntaxErrorException("parameter syntax error");
81 std::string v (tokens.begin()[0].value());
82 std::string::size_type i (v.find(':'));
84 if (i == std::string::npos)
85 out.low = out.high = boost::lexical_cast<T>(v);
87 out.low = boost::lexical_cast<T>(v.substr(0,i));
88 out.high = boost::lexical_cast<T>(v.substr(i+1));
91 catch (std::bad_cast & ex) {
92 throw senf::console::SyntaxErrorException("parameter syntax error");
97 prefix_ std::string senf::console::ArgumentTraits< senf::console::ValueRange<T> >::
100 return (boost::format("range<%s>") % ArgumentTraits<T>::description()).str();
104 prefix_ std::string senf::console::ArgumentTraits< senf::console::ValueRange<T> >::
105 str(type const & value)
107 std::stringstream ss;
108 senf::console::format(value, ss);
112 ///////////////////////////////////////////////////////////////////////////
113 // senf::console::ReturnValueTraits< senf::console::ValueRange<T> >
116 prefix_ void senf::console::ReturnValueTraits< senf::console::ValueRange<T> >::
117 format(type const & value, std::ostream & os)
119 os << senf::console::str(value.low);
120 if (value.low != value.high)
121 os << ':' << senf::console::str(value.high);
124 ///////////////////////////////////////////////////////////////////////////
125 // senf::console::ArgumentTraits< senf::console::FlagCollection<Enum> >
127 template <class Enum>
128 prefix_ void senf::console::ArgumentTraits< senf::console::FlagCollection<Enum> >::
129 parse(ParseCommandInfo::TokensRange const & tokens, type & out)
131 CheckedArgumentIteratorWrapper arg (tokens);
135 senf::console::parse( *(arg++), v);
140 template <class Enum>
142 senf::console::ArgumentTraits< senf::console::FlagCollection<Enum> >::description()
144 return ArgumentTraits<Enum>::description();
147 template <class Enum>
149 senf::console::ArgumentTraits< senf::console::FlagCollection<Enum> >::str(type const & value)
151 std::stringstream ss;
152 senf::console::format(value, ss);
156 ///////////////////////////////////////////////////////////////////////////
157 // senf::console::ReturnValueTraits< senf::console::FlagCollection<Enum> >
159 template <class Enum>
160 prefix_ void senf::console::ReturnValueTraits< senf::console::FlagCollection<Enum> >::
161 format(type const & value, std::ostream & os)
164 std::stringstream ss;
165 unsigned long flag (1);
166 for (unsigned bit (0); bit<sizeof(value.value)*CHAR_BIT; ++bit, flag<<=1) {
167 if (value.value & flag) {
169 senf::console::format(static_cast<Enum>(flag), ss);
172 os << (n != 1 ? "(" + ss.str() + ")" : ss.str());
175 ///////////////////////////////ct.e////////////////////////////////////////
182 // comment-column: 40
183 // c-file-style: "senf"
184 // indent-tabs-mode: nil
185 // ispell-local-dictionary: "american"
186 // compile-command: "scons -u test"