Utility.ct
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 Utility non-inline template implementation */
16 
17 //#include "Utility.ih"
18 
19 // Custom includes
20 #include <sstream>
21 #include <limits>
22 #include <boost/format.hpp>
23 #include "Traits.hh"
24 
25 #define prefix_
26 //-/////////////////////////////////////////////////////////////////////////////////////////////////
27 
28 //-/////////////////////////////////////////////////////////////////////////////////////////////////
29 // senf::console::ArgumentTraits< CharAsString<CharT> >
30 
31 template <class CharT>
32 prefix_ void senf::console::ArgumentTraits< senf::console::CharAsString<CharT> >::
33 parse(ParseCommandInfo::TokensRange const & tokens, CharAsString<CharT> & out)
34 {
35  std::string v;
36  console::parse(tokens,v);
37  if (v.size() != 1)
38  throw SyntaxErrorException("Invalid size of character constant");
39  out.value = static_cast<CharT>(v[0]);
40 }
41 
42 template <class CharT>
43 prefix_ std::string
44 senf::console::ArgumentTraits< senf::console::CharAsString<CharT> >::description()
45 {
46  return std::numeric_limits<CharT>::is_signed ? "char" : "uchar";
47 }
48 
49 template <class CharT>
50 prefix_ std::string senf::console::ArgumentTraits< senf::console::CharAsString<CharT> >::
51 str(CharAsString<CharT> value)
52 {
53  return console::str(std::string(1,value.value));
54 }
55 
56 template <class CharT>
57 prefix_ void senf::console::ReturnValueTraits< senf::console::CharAsString<CharT> >::
58 format(CharAsString<CharT> value, std::ostream & os)
59 {
60  console::format(std::string(1,value.value),os);
61 }
62 
63 //-/////////////////////////////////////////////////////////////////////////////////////////////////
64 // senf::console::ArgumentTraits< senf::console::ValueRange<T> >
65 
66 template <class T>
67 prefix_ void senf::console::ArgumentTraits< senf::console::ValueRange<T> >::
68 parse(ParseCommandInfo::TokensRange const & tokens, type & out)
69 {
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(':'));
74  try {
75  if (i == std::string::npos)
76  out.lower = out.upper = boost::lexical_cast<T>(v);
77  else {
78  out.lower = boost::lexical_cast<T>(v.substr(0,i));
79  out.upper = boost::lexical_cast<T>(v.substr(i+1));
80  }
81  }
82  catch (std::bad_cast & ex) {
83  throw SyntaxErrorException("parameter syntax error");
84  }
85 }
86 
87 template <class T>
88 prefix_ std::string senf::console::ArgumentTraits< senf::console::ValueRange<T> >::
89 description()
90 {
91  return (boost::format("range<%s>") % ArgumentTraits<T>::description()).str();
92 }
93 
94 template <class T>
95 prefix_ std::string senf::console::ArgumentTraits< senf::console::ValueRange<T> >::
96 str(type const & value)
97 {
98  std::stringstream ss;
99  senf::console::format(value, ss);
100  return ss.str();
101 }
102 
103 //-/////////////////////////////////////////////////////////////////////////////////////////////////
104 // senf::console::ReturnValueTraits< senf::console::ValueRange<T> >
105 
106 template <class T>
107 prefix_ void senf::console::ReturnValueTraits< senf::console::ValueRange<T> >::
108 format(type const & value, std::ostream & os)
109 {
110  os << senf::console::str(value.lower);
111  if (value.lower != value.upper)
112  os << ':' << senf::console::str(value.upper);
113 }
114 
115 //-/////////////////////////////////////////////////////////////////////////////////////////////////
116 // senf::console::ArgumentTraits< senf::console::FlagCollection<Enum> >
117 
118 template <class Enum>
119 prefix_ void senf::console::ArgumentTraits< senf::console::FlagCollection<Enum> >::
120 parse(ParseCommandInfo::TokensRange const & tokens, type & out)
121 {
122  CheckedArgumentIteratorWrapper arg (tokens);
123  out.value = 0;
124  while (arg) {
125  Enum v;
126  console::parse( *(arg++), v);
127  out.value |= static_cast<typename type::underlying_type>(v);
128  }
129 }
130 
131 template <class Enum>
132 prefix_ std::string
133 senf::console::ArgumentTraits< senf::console::FlagCollection<Enum> >::description()
134 {
135  return ArgumentTraits<Enum>::description();
136 }
137 
138 template <class Enum>
139 prefix_ std::string
140 senf::console::ArgumentTraits< senf::console::FlagCollection<Enum> >::str(type const & value)
141 {
142  std::stringstream ss;
143  console::format(value, ss);
144  return ss.str();
145 }
146 
147 //-/////////////////////////////////////////////////////////////////////////////////////////////////
148 // senf::console::ReturnValueTraits< senf::console::FlagCollection<Enum> >
149 
150 template <class Enum>
151 prefix_ void senf::console::ReturnValueTraits< senf::console::FlagCollection<Enum> >::
152 format(type const & value, std::ostream & os)
153 {
154  unsigned n (0);
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) {
159  if (n++) ss << " ";
160  console::format(static_cast<Enum>(flag), ss);
161  }
162  }
163  os << "(" + ss.str() + ")";
164 // os << (n != 1 ? "(" + ss.str() + ")" : ss.str());
165 }
166 
167 //-/////////////////////////////////////////////////////////////////////////////////////////////////
168 #undef prefix_
169 
170 
171 // Local Variables:
172 // mode: c++
173 // fill-column: 100
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"
179 // End: