VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
goptionvalue.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2017 Graeme Walker
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ===
17 ///
18 /// \file goptionvalue.h
19 ///
20 
21 #ifndef G_OPTION_VALUE_H
22 #define G_OPTION_VALUE_H
23 
24 #include "gdef.h"
25 #include "gstr.h"
26 #include <string>
27 
28 namespace G
29 {
30  class OptionValue ;
31 }
32 
33 /// \class G::OptionValue
34 /// A simple structure encapsulating the value of a command-line option.
35 /// Unvalued options (eg. "--debug") can be be explicitly on (eg. "--debug=yes")
36 /// or off ("--debug=no"); the latter are typically ignored.
37 ///
39 {
40 public:
41  OptionValue() ;
42  ///< Default constructor for a valueless value.
43 
44  explicit OptionValue( const std::string & s ) ;
45  ///< Constructor for a valued value.
46  ///< Precondition: !s.empty()
47 
48  static OptionValue on() ;
49  ///< A factory function for an unvalued option-enabled option.
50 
51  static OptionValue off() ;
52  ///< A factory function for an unvalued option-disabled option.
53 
54  bool is_on() const ;
55  ///< Returns true if an unvalued enabled option.
56 
57  bool is_off() const ;
58  ///< Returns true if an unvalued disabled option.
59 
60  bool valued() const ;
61  ///< Returns true if constructed with the string overload,
62  ///< or false if constructed by on() or off() or the default
63  ///< constructor. Note that a non-valued() value can still
64  ///< have a non-empty value().
65 
66  std::string value() const ;
67  ///< Returns the value as a string.
68 
69  bool numeric() const ;
70  ///< Returns true if value() is an unsigned integer.
71 
72  unsigned int number( unsigned int default_ = 0U ) const ;
73  ///< Returns value() as an unsigned integer.
74  ///< Returns the default if not numeric().
75 
76 private:
77  bool m_valued ; // ie. valued
78  std::string m_value ;
79 } ;
80 
81 inline
83  m_valued(false) ,
84  m_value(G::Str::positive())
85 {
86 }
87 
88 inline
89 G::OptionValue::OptionValue( const std::string & s ) :
90  m_valued(true) ,
91  m_value(s)
92 {
93 }
94 
95 inline
97 {
98  OptionValue v ;
99  v.m_value = G::Str::positive() ;
100  return v ;
101 }
102 
103 inline
105 {
106  OptionValue v ;
107  v.m_value = G::Str::negative() ;
108  return v ;
109 }
110 
111 inline
113 {
114  return !m_valued && G::Str::isPositive(m_value) ;
115 }
116 
117 inline
119 {
120  return !m_valued && G::Str::isNegative(m_value) ;
121 }
122 
123 inline
125 {
126  return m_valued ;
127 }
128 
129 inline
130 std::string G::OptionValue::value() const
131 {
132  return m_value ;
133 }
134 
135 inline
137 {
138  return valued() && G::Str::isUInt(m_value) ;
139 }
140 
141 inline
142 unsigned int G::OptionValue::number( unsigned int default_ ) const
143 {
144  return numeric() ? G::Str::toUInt(m_value) : default_ ;
145 }
146 
147 #endif
A simple structure encapsulating the value of a command-line option.
Definition: goptionvalue.h:38
static std::string positive()
Returns a default positive string. See isPositive().
Definition: gstr.cpp:1075
static bool isPositive(const std::string &)
Returns true if the string has a positive meaning, such as "1", "true", "yes".
Definition: gstr.cpp:1085
bool numeric() const
Returns true if value() is an unsigned integer.
Definition: goptionvalue.h:136
bool is_on() const
Returns true if an unvalued enabled option.
Definition: goptionvalue.h:112
static OptionValue on()
A factory function for an unvalued option-enabled option.
Definition: goptionvalue.h:96
static OptionValue off()
A factory function for an unvalued option-disabled option.
Definition: goptionvalue.h:104
static unsigned int toUInt(const std::string &s)
Converts string 's' to an unsigned int.
Definition: gstr.cpp:450
OptionValue()
Default constructor for a valueless value.
Definition: goptionvalue.h:82
bool valued() const
Returns true if constructed with the string overload, or false if constructed by on() or off() or the...
Definition: goptionvalue.h:124
static bool isNegative(const std::string &)
Returns true if the string has a negative meaning, such as "0", "false", "no".
Definition: gstr.cpp:1091
std::string value() const
Returns the value as a string.
Definition: goptionvalue.h:130
bool is_off() const
Returns true if an unvalued disabled option.
Definition: goptionvalue.h:118
unsigned int number(unsigned int default_=0U) const
Returns value() as an unsigned integer.
Definition: goptionvalue.h:142
A static class which provides string helper functions.
Definition: gstr.h:42
static std::string negative()
Returns a default negative string. See isNegative().
Definition: gstr.cpp:1080
static bool isUInt(const std::string &s)
Returns true if the string can be converted into an unsigned integer without throwing an exception...
Definition: gstr.cpp:266