Traits.cti
Go to the documentation of this file.
1 //
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
6 //
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
11 //
12 
13 
14 /** \file
15  \brief Traits inline template implementation */
16 
17 #include "Traits.ih"
18 
19 // Custom includes
20 #include <sstream>
21 #include <boost/format.hpp>
22 #include <senf/Utils/TypeInfo.hh>
23 
24 #define prefix_ inline
25 //-/////////////////////////////////////////////////////////////////////////////////////////////////
26 
27 //-/////////////////////////////////////////////////////////////////////////////////////////////////
28 // senf::console::detail::ReturnValueTraits<Type>
29 
30 template <class Type>
31 prefix_ void senf::console::ReturnValueTraits<Type>::format(Type const & value,
32  std::ostream & os)
33 {
34  senf_console_format_value(value, os);
35 }
36 
37 template <class Type>
38 prefix_ void senf::console::senf_console_format_value(Type const & value, std::ostream & os)
39 {
40  os << value;
41 }
42 
43 //-/////////////////////////////////////////////////////////////////////////////////////////////////
44 // senf::console::ArgumentTraits<Type>
45 
46 template <class Type>
47 prefix_ void senf::console::ArgumentTraits<Type>::
48 parse(ParseCommandInfo::TokensRange const & tokens, Type & out)
49 {
50  senf_console_parse_argument(tokens,out);
51 }
52 
53 template <class Type>
54 prefix_ void senf::console::parse(ParseCommandInfo::TokensRange const & tokens, Type & out)
55 {
56  ArgumentTraits<Type>::parse(tokens, out);
57 }
58 
59 template <class Type>
60 prefix_ std::string senf::console::str(Type const & value)
61 {
62  return ArgumentTraits<Type>::str(value);
63 }
64 
65 template <class Type>
66 prefix_ void senf::console::format(Type const & value, std::ostream & os)
67 {
68  ReturnValueTraits<Type>::format(value, os);
69 }
70 
71 template <class Type>
72 prefix_ std::string senf::console::ArgumentTraits<Type>::description()
73 {
74  return prettyBaseName(typeid(Type));
75 }
76 
77 template <class Type>
78 prefix_ std::string senf::console::ArgumentTraits<Type>::str(Type const & value)
79 {
80  std::stringstream ss;
81  senf::console::format(value, ss);
82  std::string rv (ss.str());
83 
84  if (rv.empty() || ! boost::algorithm::all(rv, CommandParser::isWordChar)) {
85  for (std::string::size_type i (0); i < rv.size(); ++i)
86  if (rv[i] == '"' || rv[i] == '\\')
87  rv.insert(i++,"\\");
88  else if (rv[i] < ' ' || rv[i] > 126) {
89  rv.insert(i+1, (boost::format("x%02x")
90  % unsigned(static_cast<unsigned char>(rv[i]))).str().c_str());
91  rv[i] = '\\';
92  i += 3;
93  }
94 
95  rv.insert(0,"\"");
96  rv.push_back('"');
97  return rv;
98  }
99 
100  return ss.str();
101 }
102 
103 //-/////////////////////////////////////////////////////////////////////////////////////////////////
104 // senf::console::detail::CharArgumentTraits<CharT>
105 
106 template <class CharT>
107 prefix_ void senf::console::detail::CharArgumentTraits<CharT>::
108 parse(ParseCommandInfo::TokensRange const & tokens, CharT & out)
109 {
110  typename base::type v;
111  base::parse(tokens,v);
112  out = v;
113 }
114 
115 template <class CharT>
116 prefix_ std::string senf::console::detail::CharArgumentTraits<CharT>::description()
117 {
118  return std::numeric_limits<CharT>::is_signed ? "byte" : "ubyte";
119 }
120 
121 //-/////////////////////////////////////////////////////////////////////////////////////////////////
122 #undef prefix_
123 
124 
125 // Local Variables:
126 // mode: c++
127 // fill-column: 100
128 // comment-column: 40
129 // c-file-style: "senf"
130 // indent-tabs-mode: nil
131 // ispell-local-dictionary: "american"
132 // compile-command: "scons -u test"
133 // End: