Utility.hh
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 
17 #ifndef HH_SENF_Utils_Console_Utility_
18 #define HH_SENF_Utils_Console_Utility_ 1
19 
20 // Custom includes
21 #include "Parse.hh"
22 
23 //#include "Utility.mpp"
24 //-/////////////////////////////////////////////////////////////////////////////////////////////////
25 
32 namespace senf {
33 namespace console {
34 
54  template <class CharT>
55  struct CharAsString
56  {
57  CharAsString();
58  CharAsString(CharT value_);
59  operator CharT () const;
60  CharT value;
61  };
62 
63 #ifndef DOXYGEN
64 
65  template <class CharT>
66  struct ArgumentTraits< CharAsString<CharT> >
67  {
68  typedef CharAsString<CharT> type;
69  static bool const singleToken = true;
70 
71  static void parse(ParseCommandInfo::TokensRange const & tokens, CharAsString<CharT> & out);
72  static std::string description();
73  static std::string str(CharAsString<CharT> value);
74  };
75 
76  template <class CharT>
77  struct ReturnValueTraits< CharAsString<CharT> >
78  {
79  typedef CharAsString<CharT> type;
80 
81  static void format(CharAsString<CharT> value, std::ostream & os);
82  };
83 
84 #endif
85 
93  template <class T>
94  struct ValueRange
95  {
96  T lower;
97  T upper;
98  };
99 
100 #ifndef DOXYGEN
101 
102  template <class T>
103  struct ArgumentTraits< ValueRange<T> >
104  {
105  typedef ValueRange<T> type;
106  static bool const singleToken = true;
107 
108  static void parse(ParseCommandInfo::TokensRange const & tokens, type & out);
109  static std::string description();
110  static std::string str(type const & value);
111  };
112 
113  template <class T>
114  struct ReturnValueTraits< ValueRange<T> >
115  {
116  typedef ValueRange<T> type;
117 
118  static void format(type const & value, std::ostream & os);
119  };
120 
121  namespace detail {
122  template <typename FlagType>
123  struct UnderlyingFlagType
124  {
125 # ifdef SENF_CXX11_ENABLED
126  typedef typename std::underlying_type<FlagType>::type type;
127 # else
128  typedef unsigned long type;
129 # endif
130  };
131  }
132 
133 #endif
134 
177  template <typename Enum>
179  {
180  typedef typename detail::UnderlyingFlagType<Enum>::type underlying_type;
181 
182  operator underlying_type() const { return value; }
183 
185  FlagCollection(underlying_type v) : value (v) {}
186  FlagCollection(Enum v) : value (static_cast<underlying_type>(v)) {}
187 
188  underlying_type value;
189  };
190 
191 #ifndef DOXYGEN
192 
193  template <class Enum>
194  struct ArgumentTraits< FlagCollection<Enum> >
195  {
196  typedef FlagCollection<Enum> type;
197  static bool const singleToken = false;
198  static void parse(ParseCommandInfo::TokensRange const & tokens, type & out);
199  static std::string description();
200  static std::string str(type const & value);
201  };
202 
203  template <class Enum>
204  struct ReturnValueTraits< FlagCollection<Enum> >
205  {
206  typedef FlagCollection<Enum> type;
207  static void format(type const & value, std::ostream & os);
208  };
209 
210 #endif
211 
212 }}
213 
214 //-/////////////////////////////////////////////////////////////////////////////////////////////////
215 //#include "Utility.cci"
216 #include "Utility.ct"
217 #include "Utility.cti"
218 #endif
219 
220 
221 // Local Variables:
222 // mode: c++
223 // fill-column: 100
224 // comment-column: 40
225 // c-file-style: "senf"
226 // indent-tabs-mode: nil
227 // ispell-local-dictionary: "american"
228 // compile-command: "scons -u test"
229 // End:
Bit-mask flag argument type.
Definition: Utility.hh:178
Parse character value as single-char string.
Definition: Utility.hh:55
u8 type
std::string str(Type const &value)
Format value.
FlagCollection(underlying_type v)
Definition: Utility.hh:185
Customize return value formating.
Definition: Parse.hh:203
Definition: Config.hh:28
void parse(ParseCommandInfo::TokensRange const &tokens, Type &out)
Parse token range.
unspecified_keyword_type description
One-line argument description.
boost::iterator_range< token_iterator > TokensRange
Definition: Parse.hh:378
Parse public header.
Customize argument parsing.
Definition: Parse.hh:201
detail::UnderlyingFlagType< Enum >::type underlying_type
Definition: Utility.hh:180
void format(Type const &value, std::ostream &os)
Format return value.