2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
15 \brief Utility non-inline template implementation */
17 //#include "Utility.ih"
22 #include <boost/format.hpp>
26 //-/////////////////////////////////////////////////////////////////////////////////////////////////
28 //-/////////////////////////////////////////////////////////////////////////////////////////////////
29 // senf::console::ArgumentTraits< CharAsString<CharT> >
31 template <class CharT>
32 prefix_ void senf::console::ArgumentTraits< senf::console::CharAsString<CharT> >::
33 parse(ParseCommandInfo::TokensRange const & tokens, CharAsString<CharT> & out)
36 console::parse(tokens,v);
38 throw SyntaxErrorException("Invalid size of character constant");
39 out.value = static_cast<CharT>(v[0]);
42 template <class CharT>
44 senf::console::ArgumentTraits< senf::console::CharAsString<CharT> >::description()
46 return std::numeric_limits<CharT>::is_signed ? "char" : "uchar";
49 template <class CharT>
50 prefix_ std::string senf::console::ArgumentTraits< senf::console::CharAsString<CharT> >::
51 str(CharAsString<CharT> value)
53 return console::str(std::string(1,value.value));
56 template <class CharT>
57 prefix_ void senf::console::ReturnValueTraits< senf::console::CharAsString<CharT> >::
58 format(CharAsString<CharT> value, std::ostream & os)
60 console::format(std::string(1,value.value),os);
63 //-/////////////////////////////////////////////////////////////////////////////////////////////////
64 // senf::console::ArgumentTraits< senf::console::ValueRange<T> >
67 prefix_ void senf::console::ArgumentTraits< senf::console::ValueRange<T> >::
68 parse(ParseCommandInfo::TokensRange const & tokens, type & out)
70 if (tokens.size() != 1)
71 throw SyntaxErrorException("parameter syntax error");
72 std::string v (tokens.begin()[0].value());
73 std::string::size_type i (v.find(':'));
75 if (i == std::string::npos)
76 out.lower = out.upper = boost::lexical_cast<T>(v);
78 out.lower = boost::lexical_cast<T>(v.substr(0,i));
79 out.upper = boost::lexical_cast<T>(v.substr(i+1));
82 catch (std::bad_cast & ex) {
83 throw SyntaxErrorException("parameter syntax error");
88 prefix_ std::string senf::console::ArgumentTraits< senf::console::ValueRange<T> >::
91 return (boost::format("range<%s>") % ArgumentTraits<T>::description()).str();
95 prefix_ std::string senf::console::ArgumentTraits< senf::console::ValueRange<T> >::
96 str(type const & value)
99 senf::console::format(value, ss);
103 //-/////////////////////////////////////////////////////////////////////////////////////////////////
104 // senf::console::ReturnValueTraits< senf::console::ValueRange<T> >
107 prefix_ void senf::console::ReturnValueTraits< senf::console::ValueRange<T> >::
108 format(type const & value, std::ostream & os)
110 os << senf::console::str(value.lower);
111 if (value.lower != value.upper)
112 os << ':' << senf::console::str(value.upper);
115 //-/////////////////////////////////////////////////////////////////////////////////////////////////
116 // senf::console::ArgumentTraits< senf::console::FlagCollection<Enum> >
118 template <class Enum>
119 prefix_ void senf::console::ArgumentTraits< senf::console::FlagCollection<Enum> >::
120 parse(ParseCommandInfo::TokensRange const & tokens, type & out)
122 CheckedArgumentIteratorWrapper arg (tokens);
126 console::parse( *(arg++), v);
127 out.value |= static_cast<typename type::underlying_type>(v);
131 template <class Enum>
133 senf::console::ArgumentTraits< senf::console::FlagCollection<Enum> >::description()
135 return ArgumentTraits<Enum>::description();
138 template <class Enum>
140 senf::console::ArgumentTraits< senf::console::FlagCollection<Enum> >::str(type const & value)
142 std::stringstream ss;
143 console::format(value, ss);
147 //-/////////////////////////////////////////////////////////////////////////////////////////////////
148 // senf::console::ReturnValueTraits< senf::console::FlagCollection<Enum> >
150 template <class Enum>
151 prefix_ void senf::console::ReturnValueTraits< senf::console::FlagCollection<Enum> >::
152 format(type const & value, std::ostream & os)
155 std::stringstream ss;
156 unsigned long flag (1);
157 for (unsigned bit (0); bit < sizeof(value.value)*CHAR_BIT; ++bit, flag<<=1) {
158 if (value.value & flag) {
160 console::format(static_cast<Enum>(flag), ss);
163 os << "(" + ss.str() + ")";
164 // os << (n != 1 ? "(" + ss.str() + ")" : ss.str());
167 //-/////////////////////////////////////////////////////////////////////////////////////////////////
174 // comment-column: 40
175 // c-file-style: "senf"
176 // indent-tabs-mode: nil
177 // ispell-local-dictionary: "american"
178 // compile-command: "scons -u test"