E-MailRelay
goptionmap.h
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2023 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 goptionmap.h
19///
20
21#ifndef G_OPTION_MAP_H
22#define G_OPTION_MAP_H
23
24#include "gdef.h"
25#include "goptionvalue.h"
26#include <string>
27#include <map>
28#include <algorithm>
29#include <functional> // std::less
30
31namespace G
32{
33 class OptionMap ;
34}
35
36//| \class G::OptionMap
37/// A multimap-like container for command-line options and their values.
38/// The values are G::OptionValue objects, so they can be valued with a
39/// string value or unvalued with an on/off status, and they can have
40/// a repeat-count. Normally populated by G::OptionParser.
41///
43{
44public:
45 using Map = std::multimap<std::string,OptionValue,std::less<std::string>> ; // NOLINT modernize-use-transparent-functors
46 using value_type = Map::value_type ;
47 using iterator = Map::iterator ;
48 using const_iterator = Map::const_iterator ;
49
50public:
51 void insert( const Map::value_type & ) ;
52 ///< Inserts the key/value pair into the map. The ordering of values
53 ///< in the map with the same key is in the order of insert()ion
54 ///< (as guaranteed by std::multimap since c++ 2011).
55
56 void replace( string_view key , const std::string & value ) ;
57 ///< Replaces all matching values with a single value.
58
59 void increment( string_view key ) ;
60 ///< Increments the repeat count for the given entry.
61
62 const_iterator begin() const ;
63 ///< Returns the begin iterator.
64
65 const_iterator cbegin() const ;
66 ///< Returns the begin iterator.
67
68 const_iterator end() const ;
69 ///< Returns the off-the-end iterator.
70
71 const_iterator cend() const ;
72 ///< Returns the off-the-end iterator.
73
74 const_iterator find( string_view ) const ;
75 ///< Finds the map entry with the given key.
76
77 void clear() ;
78 ///< Clears the map.
79
80 bool contains( string_view ) const ;
81 ///< Returns true if the map contains the given key, but ignoring 'off'
82 ///< option-values.
83
84 bool contains( const char * ) const ;
85 ///< Overload for c-string.
86
87 bool contains( const std::string & ) const ;
88 ///< Overload for std-string.
89
90 std::size_t count( string_view key ) const ;
91 ///< Returns the total repeat count for all matching entries.
92 ///< See G::OptionValue::count().
93
94 std::string value( string_view key , string_view default_ = {} ) const ;
95 ///< Returns the matching value, with concatentation into a comma-separated
96 ///< list if multivalued (with no escaping). If there are any on/off
97 ///< option-values matching the key then a single value is returned
98 ///< corresponding to the first one, being G::Str::positive() if 'on'
99 ///< or the supplied default if 'off'.
100
101 unsigned int number( string_view key , unsigned int default_ ) const ;
102 ///< Returns the matching value as a number.
103
104private:
105 using Range = std::pair<Map::const_iterator,Map::const_iterator> ;
106 Range findRange( string_view key ) const ;
107 Map::iterator findFirst( string_view key ) ;
108 std::string join( Map::const_iterator , Map::const_iterator , string_view ) const ;
109
110private:
111 Map m_map ;
112} ;
113
114#endif
A multimap-like container for command-line options and their values.
Definition: goptionmap.h:43
const_iterator cend() const
Returns the off-the-end iterator.
Definition: goptionmap.cpp:80
void replace(string_view key, const std::string &value)
Replaces all matching values with a single value.
Definition: goptionmap.cpp:47
void clear()
Clears the map.
Definition: goptionmap.cpp:86
std::size_t count(string_view key) const
Returns the total repeat count for all matching entries.
Definition: goptionmap.cpp:113
bool contains(string_view) const
Returns true if the map contains the given key, but ignoring 'off' option-values.
Definition: goptionmap.cpp:91
unsigned int number(string_view key, unsigned int default_) const
Returns the matching value as a number.
Definition: goptionmap.cpp:147
const_iterator cbegin() const
Returns the begin iterator.
Definition: goptionmap.cpp:68
const_iterator end() const
Returns the off-the-end iterator.
Definition: goptionmap.cpp:74
void insert(const Map::value_type &)
Inserts the key/value pair into the map.
Definition: goptionmap.cpp:26
void increment(string_view key)
Increments the repeat count for the given entry.
Definition: goptionmap.cpp:55
const_iterator find(string_view) const
Finds the map entry with the given key.
Definition: goptionmap.cpp:41
std::string value(string_view key, string_view default_={}) const
Returns the matching value, with concatentation into a comma-separated list if multivalued (with no e...
Definition: goptionmap.cpp:122
const_iterator begin() const
Returns the begin iterator.
Definition: goptionmap.cpp:62
A class like c++17's std::string_view.
Definition: gstringview.h:51
Low-level classes.
Definition: garg.h:30