E-MailRelay

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

goptionvalue.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2001-2018 Graeme Walker <graeme_walker@users.sourceforge.net>
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 ///
38 class G::OptionValue
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
82 G::OptionValue::OptionValue() :
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
96 G::OptionValue G::OptionValue::on()
97 {
98 OptionValue v ;
99 v.m_value = G::Str::positive() ;
100 return v ;
101 }
102
103 inline
104 G::OptionValue G::OptionValue::off()
105 {
106 OptionValue v ;
107 v.m_value = G::Str::negative() ;
108 return v ;
109 }
110
111 inline
112 bool G::OptionValue::is_on() const
113 {
114 return !m_valued && G::Str::isPositive(m_value) ;
115 }
116
117 inline
118 bool G::OptionValue::is_off() const
119 {
120 return !m_valued && G::Str::isNegative(m_value) ;
121 }
122
123 inline
124 bool G::OptionValue::valued() const
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
136 bool G::OptionValue::numeric() const
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
G::OptionValue
A simple structure encapsulating the value of a command-line option.
Definition: goptionvalue.h:38
G::Str::positive
static std::string positive()
Returns a default positive string. See isPositive().
Definition: gstr.cpp:1172
G::Str::isPositive
static bool isPositive(const std::string &)
Returns true if the string has a positive meaning, such as "1", "true", "yes".
Definition: gstr.cpp:1182
G::OptionValue::numeric
bool numeric() const
Returns true if value() is an unsigned integer.
Definition: goptionvalue.h:136
G::OptionValue::is_on
bool is_on() const
Returns true if an unvalued enabled option.
Definition: goptionvalue.h:112
G::OptionValue::on
static OptionValue on()
A factory function for an unvalued option-enabled option.
Definition: goptionvalue.h:96
G::OptionValue::off
static OptionValue off()
A factory function for an unvalued option-disabled option.
Definition: goptionvalue.h:104
G::Str::toUInt
static unsigned int toUInt(const std::string &s)
Converts string 's' to an unsigned int.
Definition: gstr.cpp:488
gstr.h
G::OptionValue::OptionValue
OptionValue()
Default constructor for a valueless value.
Definition: goptionvalue.h:82
G::OptionValue::valued
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
G
Low-level classes.
Definition: garg.h:29
G::Str::isNegative
static bool isNegative(const std::string &)
Returns true if the string has a negative meaning, such as "0", "false", "no".
Definition: gstr.cpp:1188
G::OptionValue::value
std::string value() const
Returns the value as a string.
Definition: goptionvalue.h:130
G::OptionValue::is_off
bool is_off() const
Returns true if an unvalued disabled option.
Definition: goptionvalue.h:118
G::OptionValue::number
unsigned int number(unsigned int default_=0U) const
Returns value() as an unsigned integer.
Definition: goptionvalue.h:142
G::Str
A static class which provides string helper functions.
Definition: gstr.h:42
G::Str::negative
static std::string negative()
Returns a default negative string. See isNegative().
Definition: gstr.cpp:1177
G::Str::isUInt
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:304