E-MailRelay
goptionmap.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 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
30namespace G
31{
32 class OptionMap ;
33}
34
35//| \class G::OptionMap
36/// A multimap-like container for command-line options and their values.
37/// The values are G::OptionValue objects, so they can be valued with a
38/// string value or unvalued with an on/off status, and they can have
39/// a repeat-count. Normally populated by G::OptionParser.
40///
42{
43public:
44 using Map = std::multimap<std::string,OptionValue> ;
45 using value_type = Map::value_type ;
46 using iterator = Map::iterator ;
47 using const_iterator = Map::const_iterator ;
48
49public:
50 void insert( const Map::value_type & ) ;
51 ///< Inserts the key/value pair into the map. The ordering of values in
52 ///< the map with the same key is in the order of insert()ion.
53
54 void replace( const std::string & key , const std::string & value ) ;
55 ///< Replaces all matching values with a single value.
56
57 void increment( const std::string & key ) ;
58 ///< Increments the repeat count for the given entry.
59
60 const_iterator begin() const ;
61 ///< Returns the begin iterator.
62
63 const_iterator cbegin() const ;
64 ///< Returns the begin iterator.
65
66 const_iterator end() const ;
67 ///< Returns the off-the-end iterator.
68
69 const_iterator cend() const ;
70 ///< Returns the off-the-end iterator.
71
72 const_iterator find( const std::string & ) const ;
73 ///< Finds the map entry with the given key.
74
75 void clear() ;
76 ///< Clears the map.
77
78 bool contains( const std::string & ) const ;
79 ///< Returns true if the map contains the given key, but ignoring 'off'
80 ///< option-values.
81
82 bool contains( const char * ) const ;
83 ///< Overload taking a c-string.
84
85 std::size_t count( const std::string & key ) const ;
86 ///< Returns the total repeat count for all matching entries.
87 ///< See G::OptionValue::count().
88
89 std::string value( const std::string & key , const std::string & default_ = std::string() ) const ;
90 ///< Returns the matching value, with concatentation into a comma-separated
91 ///< list if multivalued. If there are any on/off option-values matching
92 ///< the key then a single value is returned corresponding to the first
93 ///< one, being G::Str::positive() if 'on' or the supplied default
94 ///< if 'off'.
95
96 std::string value( const char * key , const char * default_ = nullptr ) const ;
97 ///< Overload taking c-strings.
98
99private:
100 std::string join( Map::const_iterator , Map::const_iterator , const std::string & ) const ;
101 static bool compare( const Map::value_type & , const Map::value_type & ) ;
102
103private:
104 Map m_map ;
105} ;
106
107#endif
A multimap-like container for command-line options and their values.
Definition: goptionmap.h:42
const_iterator cend() const
Returns the off-the-end iterator.
Definition: goptionmap.cpp:59
std::size_t count(const std::string &key) const
Returns the total repeat count for all matching entries.
Definition: goptionmap.cpp:91
void clear()
Clears the map.
Definition: goptionmap.cpp:69
const_iterator find(const std::string &) const
Finds the map entry with the given key.
Definition: goptionmap.cpp:64
std::string value(const std::string &key, const std::string &default_=std::string()) const
Returns the matching value, with concatentation into a comma-separated list if multivalued.
Definition: goptionmap.cpp:105
const_iterator cbegin() const
Returns the begin iterator.
Definition: goptionmap.cpp:49
const_iterator end() const
Returns the off-the-end iterator.
Definition: goptionmap.cpp:54
void increment(const std::string &key)
Increments the repeat count for the given entry.
Definition: goptionmap.cpp:37
void insert(const Map::value_type &)
Inserts the key/value pair into the map.
Definition: goptionmap.cpp:24
void replace(const std::string &key, const std::string &value)
Replaces all matching values with a single value.
Definition: goptionmap.cpp:29
bool contains(const std::string &) const
Returns true if the map contains the given key, but ignoring 'off' option-values.
Definition: goptionmap.cpp:79
const_iterator begin() const
Returns the begin iterator.
Definition: goptionmap.cpp:44
Low-level classes.
Definition: galign.h:28