E-MailRelay
goptionvalue.h
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2021 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
28namespace 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{
40public:
41 OptionValue() ;
42 ///< Default constructor for a valueless value.
43
44 explicit OptionValue( const std::string & s , std::size_t count = 1U ) ;
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 isOn() const ;
55 ///< Returns true if on().
56
57 bool isOff() const ;
58 ///< Returns true if off().
59
60 std::string value() const ;
61 ///< Returns the value as a string.
62
63 bool numeric() const ;
64 ///< Returns true if value() is an unsigned integer.
65
66 unsigned int number( unsigned int default_ = 0U ) const ;
67 ///< Returns value() as an unsigned integer.
68 ///< Returns the default if not numeric().
69
70 size_t count() const ;
71 ///< Returns an instance count that is one by default.
72
73 void increment() ;
74 ///< Increments the instance count().
75
76private:
77 bool m_on_off{false} ;
78 std::size_t m_count{1U} ;
79 std::string m_value ;
80} ;
81
82inline
84 m_on_off(true) ,
85 m_value(G::Str::positive())
86{
87}
88
89inline
90G::OptionValue::OptionValue( const std::string & s , std::size_t count ) :
91 m_count(count) ,
92 m_value(s)
93{
94}
95
96inline
98{
99 return OptionValue() ;
100}
101
102inline
104{
105 OptionValue v ;
106 v.m_value = G::Str::negative() ;
107 return v ;
108}
109
110inline
112{
113 return m_on_off && G::Str::isPositive(m_value) ;
114}
115
116inline
118{
119 return m_on_off && G::Str::isNegative(m_value) ;
120}
121
122inline
123std::string G::OptionValue::value() const
124{
125 return m_value ;
126}
127
128inline
130{
131 return !m_on_off && G::Str::isUInt(m_value) ;
132}
133
134inline
135unsigned int G::OptionValue::number( unsigned int default_ ) const
136{
137 return numeric() ? G::Str::toUInt(m_value) : default_ ;
138}
139
140inline
141std::size_t G::OptionValue::count() const
142{
143 return m_count ;
144}
145
146inline
148{
149 m_count++ ;
150}
151
152#endif
A simple structure encapsulating the value of a command-line option.
Definition: goptionvalue.h:39
size_t count() const
Returns an instance count that is one by default.
Definition: goptionvalue.h:141
OptionValue()
Default constructor for a valueless value.
Definition: goptionvalue.h:83
bool numeric() const
Returns true if value() is an unsigned integer.
Definition: goptionvalue.h:129
static OptionValue on()
A factory function for an unvalued option-enabled option.
Definition: goptionvalue.h:97
static OptionValue off()
A factory function for an unvalued option-disabled option.
Definition: goptionvalue.h:103
void increment()
Increments the instance count().
Definition: goptionvalue.h:147
std::string value() const
Returns the value as a string.
Definition: goptionvalue.h:123
bool isOff() const
Returns true if off().
Definition: goptionvalue.h:117
unsigned int number(unsigned int default_=0U) const
Returns value() as an unsigned integer.
Definition: goptionvalue.h:135
bool isOn() const
Returns true if on().
Definition: goptionvalue.h:111
A static class which provides string helper functions.
Definition: gstr.h:46
static bool isNegative(const std::string &)
Returns true if the string has a negative meaning, such as "0", "false", "no".
Definition: gstr.cpp:1362
static bool isPositive(const std::string &)
Returns true if the string has a positive meaning, such as "1", "true", "yes".
Definition: gstr.cpp:1356
static std::string negative()
Returns a default negative string. See isNegative().
Definition: gstr.cpp:1351
static unsigned int toUInt(const std::string &s)
Converts string 's' to an unsigned int.
Definition: gstr.cpp:604
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:444
Low-level classes.
Definition: galign.h:28