E-MailRelay

goptionvalue.h
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2024 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 "gstringview.h"
27#include <string>
28
29 namespace G
30{
31 class OptionValue ;
32}
33
34//| \class G::OptionValue
35/// A simple structure encapsulating the value of a command-line option.
36/// Unvalued options (eg. "--debug") can be be explicitly on (eg. "--debug=yes")
37/// or off ("--debug=no"); the latter are typically ignored.
38///
39 class G::OptionValue
40{
41 public:
42 OptionValue() ;
43 ///< Default constructor for a valueless value.
44
45 explicit OptionValue( const std::string & s , std::size_t count = 1U ) ;
46 ///< Constructor for a valued value.
47 ///< Precondition: !s.empty()
48
49 static OptionValue on() ;
50 ///< A factory function for an unvalued option-enabled option.
51
52 static OptionValue off() ;
53 ///< A factory function for an unvalued option-disabled option.
54
55 bool isOn() const noexcept ;
56 ///< Returns true if on().
57
58 bool isOff() const noexcept ;
59 ///< Returns true if off().
60
61 std::string value() const ;
62 ///< Returns the value as a string.
63
64 std::string_view valueref() const noexcept ;
65 ///< Exposes the value as a string view.
66
67 bool operator==( std::string_view ) const noexcept ;
68 ///< Returns true if the given string matches value().
69
70 bool numeric() const noexcept ;
71 ///< Returns true if value() is an unsigned integer.
72
73 unsigned int number( unsigned int default_ = 0U ) const ;
74 ///< Returns value() as an unsigned integer.
75 ///< Returns the default if not numeric().
76
77 size_t count() const noexcept ;
78 ///< Returns an instance count that is one by default.
79
80 void increment() noexcept ;
81 ///< Increments the instance count().
82
83 private:
84 bool m_on_off {false} ;
85 std::size_t m_count {1U} ;
86 std::string m_value ;
87} ;
88
89 inline
90 G::OptionValue::OptionValue() :
91 m_on_off(true) ,
92 m_value(G::Str::positive())
93{
94}
95
96 inline
97 G::OptionValue::OptionValue( const std::string & s , std::size_t count ) :
98 m_count(count) ,
99 m_value(s)
100{
101}
102
103 inline
104 G::OptionValue G::OptionValue::on()
105{
106 return {} ;
107}
108
109 inline
110 G::OptionValue G::OptionValue::off()
111{
112 OptionValue v ;
113 v.m_value = G::Str::negative() ;
114 return v ;
115}
116
117 inline
118 bool G::OptionValue::isOn() const noexcept
119{
120 return m_on_off && G::Str::isPositive(m_value) ;
121}
122
123 inline
124 bool G::OptionValue::isOff() const noexcept
125{
126 return m_on_off && G::Str::isNegative(m_value) ;
127}
128
129 inline
130std::string G::OptionValue::value() const
131{
132 return m_value ;
133}
134
135 inline
136std::string_view G::OptionValue::valueref() const noexcept
137{
138 return {m_value} ;
139}
140
141 inline
142 bool G::OptionValue::operator==( std::string_view s ) const noexcept
143{
144 return s == m_value ;
145}
146
147 inline
148 bool G::OptionValue::numeric() const noexcept
149{
150 return !m_on_off && G::Str::isUInt(m_value) ;
151}
152
153 inline
154 unsigned int G::OptionValue::number( unsigned int default_ ) const
155{
156 return numeric() ? G::Str::toUInt(m_value) : default_ ;
157}
158
159 inline
160std::size_t G::OptionValue::count() const noexcept
161{
162 return m_count ;
163}
164
165 inline
166 void G::OptionValue::increment() noexcept
167{
168 m_count++ ;
169}
170
171#endif
G::OptionValue
A simple structure encapsulating the value of a command-line option.
Definition: goptionvalue.h:40
G::OptionValue::OptionValue
OptionValue()
Default constructor for a valueless value.
Definition: goptionvalue.h:90
G::OptionValue::valueref
std::string_view valueref() const noexcept
Exposes the value as a string view.
Definition: goptionvalue.h:136
G::OptionValue::on
static OptionValue on()
A factory function for an unvalued option-enabled option.
Definition: goptionvalue.h:104
G::OptionValue::isOff
bool isOff() const noexcept
Returns true if off().
Definition: goptionvalue.h:124
G::OptionValue::count
size_t count() const noexcept
Returns an instance count that is one by default.
Definition: goptionvalue.h:160
G::OptionValue::off
static OptionValue off()
A factory function for an unvalued option-disabled option.
Definition: goptionvalue.h:110
G::OptionValue::numeric
bool numeric() const noexcept
Returns true if value() is an unsigned integer.
Definition: goptionvalue.h:148
G::OptionValue::isOn
bool isOn() const noexcept
Returns true if on().
Definition: goptionvalue.h:118
G::OptionValue::value
std::string value() const
Returns the value as a string.
Definition: goptionvalue.h:130
G::OptionValue::number
unsigned int number(unsigned int default_=0U) const
Returns value() as an unsigned integer.
Definition: goptionvalue.h:154
G::OptionValue::increment
void increment() noexcept
Increments the instance count().
Definition: goptionvalue.h:166
G::OptionValue::operator==
bool operator==(std::string_view) const noexcept
Returns true if the given string matches value().
Definition: goptionvalue.h:142
G::Str
A static class which provides string helper functions.
Definition: gstr.h:48
G::Str::isUInt
static bool isUInt(std::string_view s) noexcept
Returns true if the string can be converted into an unsigned integer without throwing an exception.
Definition: gstr.cpp:446
G::Str::isPositive
static bool isPositive(std::string_view) noexcept
Returns true if the string has a positive meaning, such as "1", "true", "yes".
Definition: gstr.cpp:1376
G::Str::negative
static std::string negative()
Returns a default negative string. See isNegative().
Definition: gstr.cpp:1371
G::Str::toUInt
static unsigned int toUInt(std::string_view s)
Converts string 's' to an unsigned int.
Definition: gstr.cpp:648
G::Str::isNegative
static bool isNegative(std::string_view) noexcept
Returns true if the string has a negative meaning, such as "0", "false", "no".
Definition: gstr.cpp:1384
gstr.h
gstringview.h
G
Low-level classes.
Definition: garg.h:36