Traits.cci
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 non-template implementation */
16 
17 #include "Traits.ih"
18 
19 // Custom includes
20 #include <boost/algorithm/string/predicate.hpp>
21 
22 #define prefix_ inline
23 //-/////////////////////////////////////////////////////////////////////////////////////////////////
24 
25 //-/////////////////////////////////////////////////////////////////////////////////////////////////
26 // senf::console::ArgumentTraits<bool>
27 
28 prefix_ void
29 senf::console::ArgumentTraits<bool>::parse(ParseCommandInfo::TokensRange const & tokens,
30  bool & out)
31 {
32  if (tokens.size() != 1)
33  throw SyntaxErrorException("argument syntax error");
34 
35  if ( boost::istarts_with(std::string("true"), tokens.begin()->value())
36  || boost::istarts_with(std::string("enabled"), tokens.begin()->value())
37  || boost::istarts_with(std::string("yes"), tokens.begin()->value())
38  || boost::iequals(std::string("on"), tokens.begin()->value()) )
39  out = true;
40  else if (boost::istarts_with(std::string("false"), tokens.begin()->value())
41  || boost::istarts_with(std::string("disabled"), tokens.begin()->value())
42  || boost::istarts_with(std::string("no"), tokens.begin()->value())
43  || (boost::istarts_with(std::string("off"), tokens.begin()->value())
44  && tokens.begin()->value().size() >= 2) )
45  out = false;
46  else {
47  int v (0);
48  senf::console::parse(tokens, v);
49  out = v;
50  }
51 }
52 
53 prefix_ std::string senf::console::ArgumentTraits<bool>::description()
54 {
55  return "bool";
56 }
57 
58 prefix_ std::string senf::console::ArgumentTraits<bool>::str(bool value)
59 {
60  return value ? "true" : "false";
61 }
62 
63 //-/////////////////////////////////////////////////////////////////////////////////////////////////
64 // senf::console::ReturnValueTraits<bool>
65 
66 prefix_ void senf::console::ReturnValueTraits<bool>::format(bool value, std::ostream & os)
67 {
68  formatTrueFalse(value, os);
69 }
70 
71 //-/////////////////////////////////////////////////////////////////////////////////////////////////
72 
73 prefix_ void senf::console::formatTrueFalse(bool value, std::ostream & os)
74 {
75  os << (value ? "true" : "false");
76 }
77 
78 prefix_ void senf::console::formatYesNo(bool value, std::ostream & os)
79 {
80  os << (value ? "yes" : "no");
81 }
82 
83 prefix_ void senf::console::formatEnabledDisabled(bool value, std::ostream & os)
84 {
85  os << (value ? "enabled" : "disabled");
86 }
87 
88 prefix_ void senf::console::formatOnOff(bool value, std::ostream & os)
89 {
90  os << (value ? "on" : "off");
91 }
92 
93 prefix_ void senf::console::formatOneZero(bool value, std::ostream & os)
94 {
95  os << (value ? "0" : "1");
96 }
97 
98 //-/////////////////////////////////////////////////////////////////////////////////////////////////
99 #undef prefix_
100 
101 
102 // Local Variables:
103 // mode: c++
104 // fill-column: 100
105 // comment-column: 40
106 // c-file-style: "senf"
107 // indent-tabs-mode: nil
108 // ispell-local-dictionary: "american"
109 // compile-command: "scons -u test"
110 // End: