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