4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at
9 // http://senf.berlios.de/license.html
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on,
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
15 // Software distributed under the License is distributed on an "AS IS" basis,
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
17 // for the specific language governing rights and limitations under the License.
19 // The Original Code is Fraunhofer FOKUS code.
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V.
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
26 // Stefan Bund <g0dil@berlios.de>
29 \brief Utility non-inline template implementation */
31 //#include "Utility.ih"
36 #include <boost/format.hpp>
40 //-/////////////////////////////////////////////////////////////////////////////////////////////////
42 //-/////////////////////////////////////////////////////////////////////////////////////////////////
43 // senf::console::ArgumentTraits< CharAsString<CharT> >
45 template <class CharT>
46 prefix_ void senf::console::ArgumentTraits< senf::console::CharAsString<CharT> >::
47 parse(ParseCommandInfo::TokensRange const & tokens, CharAsString<CharT> & out)
50 console::parse(tokens,v);
52 throw SyntaxErrorException("Invalid size of character constant");
53 out.value = static_cast<CharT>(v[0]);
56 template <class CharT>
58 senf::console::ArgumentTraits< senf::console::CharAsString<CharT> >::description()
60 return std::numeric_limits<CharT>::is_signed ? "char" : "uchar";
63 template <class CharT>
64 prefix_ std::string senf::console::ArgumentTraits< senf::console::CharAsString<CharT> >::
65 str(CharAsString<CharT> value)
67 return console::str(std::string(1,value.value));
70 template <class CharT>
71 prefix_ void senf::console::ReturnValueTraits< senf::console::CharAsString<CharT> >::
72 format(CharAsString<CharT> value, std::ostream & os)
74 console::format(std::string(1,value.value),os);
77 //-/////////////////////////////////////////////////////////////////////////////////////////////////
78 // senf::console::ArgumentTraits< senf::console::ValueRange<T> >
81 prefix_ void senf::console::ArgumentTraits< senf::console::ValueRange<T> >::
82 parse(ParseCommandInfo::TokensRange const & tokens, type & out)
84 if (tokens.size() != 1)
85 throw SyntaxErrorException("parameter syntax error");
86 std::string v (tokens.begin()[0].value());
87 std::string::size_type i (v.find(':'));
89 if (i == std::string::npos)
90 out.low = out.high = boost::lexical_cast<T>(v);
92 out.low = boost::lexical_cast<T>(v.substr(0,i));
93 out.high = boost::lexical_cast<T>(v.substr(i+1));
96 catch (std::bad_cast & ex) {
97 throw SyntaxErrorException("parameter syntax error");
102 prefix_ std::string senf::console::ArgumentTraits< senf::console::ValueRange<T> >::
105 return (boost::format("range<%s>") % ArgumentTraits<T>::description()).str();
109 prefix_ std::string senf::console::ArgumentTraits< senf::console::ValueRange<T> >::
110 str(type const & value)
112 std::stringstream ss;
113 senf::console::format(value, ss);
117 //-/////////////////////////////////////////////////////////////////////////////////////////////////
118 // senf::console::ReturnValueTraits< senf::console::ValueRange<T> >
121 prefix_ void senf::console::ReturnValueTraits< senf::console::ValueRange<T> >::
122 format(type const & value, std::ostream & os)
124 os << senf::console::str(value.low);
125 if (value.low != value.high)
126 os << ':' << senf::console::str(value.high);
129 //-/////////////////////////////////////////////////////////////////////////////////////////////////
130 // senf::console::ArgumentTraits< senf::console::FlagCollection<Enum> >
132 template <class Enum>
133 prefix_ void senf::console::ArgumentTraits< senf::console::FlagCollection<Enum> >::
134 parse(ParseCommandInfo::TokensRange const & tokens, type & out)
136 CheckedArgumentIteratorWrapper arg (tokens);
140 console::parse( *(arg++), v);
145 template <class Enum>
147 senf::console::ArgumentTraits< senf::console::FlagCollection<Enum> >::description()
149 return ArgumentTraits<Enum>::description();
152 template <class Enum>
154 senf::console::ArgumentTraits< senf::console::FlagCollection<Enum> >::str(type const & value)
156 std::stringstream ss;
157 console::format(value, ss);
161 //-/////////////////////////////////////////////////////////////////////////////////////////////////
162 // senf::console::ReturnValueTraits< senf::console::FlagCollection<Enum> >
164 template <class Enum>
165 prefix_ void senf::console::ReturnValueTraits< senf::console::FlagCollection<Enum> >::
166 format(type const & value, std::ostream & os)
169 std::stringstream ss;
170 unsigned long flag (1);
171 for (unsigned bit (0); bit<sizeof(value.value)*CHAR_BIT; ++bit, flag<<=1) {
172 if (value.value & flag) {
174 console::format(static_cast<Enum>(flag), ss);
177 os << (n != 1 ? "(" + ss.str() + ")" : ss.str());
180 //-/////////////////////////////////////////////////////////////////////////////////////////////////
187 // comment-column: 40
188 // c-file-style: "senf"
189 // indent-tabs-mode: nil
190 // ispell-local-dictionary: "american"
191 // compile-command: "scons -u test"