00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00026
00027
00028
00029 #include <sstream>
00030 #include <limits>
00031 #include <boost/format.hpp>
00032 #include "Traits.hh"
00033
00034 #define prefix_
00035
00036
00037
00038
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
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
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
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
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
00180
00181
00182
00183
00184
00185
00186
00187