Traits.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_Traits_
18 #define HH_SENF_Utils_Console_Traits_ 1
19 
20 // Custom includes
21 #include <ostream>
22 #include <boost/type_traits/is_same.hpp>
23 #include <boost/typeof/typeof.hpp>
24 #include "Parse.hh"
25 
26 #include "Traits.ih"
27 //-/////////////////////////////////////////////////////////////////////////////////////////////////
28 
29 namespace senf {
30 namespace console {
31 
51  template <class Type>
52  struct ArgumentTraits
53  {
54  typedef Type type;
55 
56  static bool const singleToken =
57  boost::is_same< decltype(senf_console_parse_argument(
58  *static_cast<ParseCommandInfo::TokensRange const *>(0),
59  *static_cast<Type*>(0))),
60  bool >::value;
61 
62  static void parse(ParseCommandInfo::TokensRange const & tokens, Type & out);
64 
72  static std::string description();
73 
75  static std::string str(Type const & value);
76 
80  };
81 
88  template <class Type>
89  bool senf_console_parse_argument(ParseCommandInfo::TokensRange const & tokens, Type & out);
90 
91 
104  template <class Type>
105  struct ReturnValueTraits
106  {
107  typedef Type type;
108 
109  static void format(Type const & value, std::ostream & os);
111  };
112 
119  template <class Type>
120  void senf_console_format_value(Type const & value, std::ostream & os);
121 
122 
130  template <class Type>
131  void parse(ParseCommandInfo::TokensRange const & tokens, Type & out);
132 
139  template <class Type>
140  std::string str(Type const & value);
141 
149  template <class Type>
150  void format(Type const & value, std::ostream & os);
151 
152 
179 # define SENF_CONSOLE_REGISTER_ENUM(Type, Values) \
180  SENF_CONSOLE_REGISTER_ENUM_(BOOST_PP_EMPTY(), BOOST_PP_EMPTY(), Type, Values)
181 
182 # define SENF_CONSOLE_REGISTER_STRONG_ENUM(Type, Values) \
183  SENF_CONSOLE_REGISTER_ENUM_(BOOST_PP_EMPTY(), Type::, Type, Values)
184 
200 # define SENF_CONSOLE_REGISTER_ENUM_MEMBER(Class, Type, Values) \
201  SENF_CONSOLE_REGISTER_ENUM_(Class::, BOOST_PP_EMPTY(), Type, Values)
202 
203 # define SENF_CONSOLE_REGISTER_STRONG_ENUM_MEMBER(Class, Type, Values) \
204  SENF_CONSOLE_REGISTER_ENUM_(Class::, Type::, Type, Values)
205 
206 
208  void formatTrueFalse(bool value, std::ostream & os);
209 
211  void formatYesNo(bool value, std::ostream & os);
212 
214  void formatEnabledDisabled(bool value, std::ostream & os);
215 
217  void formatOnOff(bool value, std::ostream & os);
218 
220  void formatOneZero(bool value, std::ostream & os);
221 
222 
223 # define SENF_CONSOLE_PARSE_AS_TUPLE( Type, Members ) \
224  SENF_CONSOLE_PARSE_AS_TUPLE_(Type, Members)
225 
226 # define SENF_CONSOLE_FORMAT_AS_TUPLE( Type, Members ) \
227  SENF_CONSOLE_FORMAT_AS_TUPLE_(Type, Members)
228 
229 # define SENF_CONSOLE_PARSE_AND_FORMAT_AS_TUPLE( Type, Members ) \
230  SENF_CONSOLE_PARSE_AS_TUPLE_(Type, Members); \
231  SENF_CONSOLE_FORMAT_AS_TUPLE_(Type, Members);
232 
233 # define SENF_CONSOLE_PARSE_AS_MAP( Type, KeyMemberTuples ) \
234  SENF_CONSOLE_PARSE_AS_MAP_(Type, KeyMemberTuples)
235 
236 # define SENF_CONSOLE_FORMAT_AS_MAP( Type, KeyMemberTuples ) \
237  SENF_CONSOLE_FORMAT_AS_MAP_(Type, KeyMemberTuples)
238 
239 # define SENF_CONSOLE_PARSE_AND_FORMAT_AS_MAP( Type, KeyMemberTuples ) \
240  SENF_CONSOLE_PARSE_AS_MAP_(Type, KeyMemberTuples); \
241  SENF_CONSOLE_FORMAT_AS_MAP_(Type, KeyMemberTuples);
242 
243 #ifndef DOXYGEN
244 
245  // Parse bool: true/false, yes/no, enabled/disabled, 0/1
246  template <>
247  struct ArgumentTraits<bool>
248  {
249  typedef bool type;
250  static bool const singleToken = true;
251 
252  static void parse(ParseCommandInfo::TokensRange const & tokens, bool & out);
253  static std::string description();
254  static std::string str(bool value);
255  };
256 
257  template <>
258  struct ReturnValueTraits<bool>
259  {
260  typedef bool type;
261 
262  static void format(bool value, std::ostream & os);
263  };
264 
265  template <> struct ArgumentTraits<char> : public detail::CharArgumentTraits<char> {};
266  template <> struct ReturnValueTraits<char> : public detail::CharReturnValueTraits<char> {};
267  template <> struct ArgumentTraits<signed char> : public detail::CharArgumentTraits<signed char> {};
268  template <> struct ReturnValueTraits<signed char> : public detail::CharReturnValueTraits<signed char> {};
269  template <> struct ArgumentTraits<unsigned char> : public detail::CharArgumentTraits<unsigned char> {};
270  template <> struct ReturnValueTraits<unsigned char> : public detail::CharReturnValueTraits<unsigned char> {};
271 
272 #endif
273 
274 }}
275 
276 //-/////////////////////////////////////////////////////////////////////////////////////////////////
277 #include "Traits.cci"
278 #include "Traits.ct"
279 #include "Traits.cti"
280 #endif
281 
282 
283 // Local Variables:
284 // mode: c++
285 // fill-column: 100
286 // comment-column: 40
287 // c-file-style: "senf"
288 // indent-tabs-mode: nil
289 // ispell-local-dictionary: "american"
290 // compile-command: "scons -u test"
291 // End:
void senf_console_format_value(DirectoryNode::ptr value, std::ostream &os)
Definition: Executor.cc:473
bool senf_console_parse_argument(ParseCommandInfo::TokensRange const &tokens, Type &out)
Argument parser.
void formatTrueFalse(bool value, std::ostream &os)
Format boolean value as true / false.
Customize return value formating.
Definition: Parse.hh:203
Definition: Config.hh:28
static std::string str(Type const &value)
Stringify value.
void formatOnOff(bool value, std::ostream &os)
Format boolean value as on / off.
static void parse(ParseCommandInfo::TokensRange const &tokens, Type &out)
Parse token range into value.
static std::string description()
String description of type.
boost::iterator_range< token_iterator > TokensRange
Definition: Parse.hh:378
static bool const singleToken
Definition: Traits.hh:56
void formatOneZero(bool value, std::ostream &os)
Format boolean value as 1 / 0.
Parse public header.
void formatEnabledDisabled(bool value, std::ostream &os)
Format boolean value as enabled / disabled.
void formatYesNo(bool value, std::ostream &os)
Format boolean value as yes / no.
void format(Type const &value, std::ostream &os)
Format return value.