VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
goptionmap.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2017 Graeme Walker
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 <string>
26 #include <map>
27 #include <algorithm>
28 
29 namespace G
30 {
31  class OptionMap ;
32 }
33 
34 /// \class G::OptionMap
35 /// A map-like container for command-line options and their values.
36 /// Normally populated by G::OptionParser.
37 ///
39 {
40 public:
41  typedef std::multimap<std::string,OptionValue> Map ;
42  typedef Map::value_type value_type ;
43  typedef Map::iterator iterator ;
44  typedef Map::const_iterator const_iterator ;
45 
46 public:
47  OptionMap() ;
48  ///< Default constructor for an empty map.
49 
50  void insert( const Map::value_type & ) ;
51  ///< Inserts the key/value pair into the map. The ordering of values in the
52  ///< map with the same key is normally the order of insertion (but this
53  ///< depends on the underlying multimap implementation for c++98).
54 
55  const_iterator begin() const ;
56  ///< Returns the begin iterator.
57 
58  const_iterator end() const ;
59  ///< Returns the off-the-end iterator.
60 
61  const_iterator find( const std::string & ) const ;
62  ///< Finds the map entry with the given key.
63 
64  void clear() ;
65  ///< Clears the map.
66 
67  bool contains( const std::string & ) const ;
68  ///< Returns true if the map contains the given key, but ignoring un-valued()
69  ///< 'off' options.
70 
71  size_t count( const std::string & key ) const ;
72  ///< Returns the number of times the key appears in the multimap.
73 
74  std::string value( const std::string & key ) const ;
75  ///< Returns the value of the valued() option identified by the given key.
76  ///< Multiple matching values are concatenated with a comma separator
77  ///< (normally in the order of insertion).
78 
79 private:
80  std::string join( Map::const_iterator , Map::const_iterator ) const ;
81 
82 private:
83  Map m_map ;
84 } ;
85 
86 inline
88 {
89 }
90 
91 inline
92 void G::OptionMap::insert( const Map::value_type & value )
93 {
94  m_map.insert( value ) ;
95 }
96 
97 inline
98 G::OptionMap::const_iterator G::OptionMap::begin() const
99 {
100  return m_map.begin() ;
101 }
102 
103 inline
104 G::OptionMap::const_iterator G::OptionMap::end() const
105 {
106  return m_map.end() ;
107 }
108 
109 inline
110 G::OptionMap::const_iterator G::OptionMap::find( const std::string & key ) const
111 {
112  return m_map.find( key ) ;
113 }
114 
115 inline
117 {
118  m_map.clear() ;
119 }
120 
121 inline
122 bool G::OptionMap::contains( const std::string & key ) const
123 {
124  const Map::const_iterator end = m_map.end() ;
125  for( Map::const_iterator p = m_map.find(key) ; p != end && (*p).first == key ; ++p )
126  {
127  if( (*p).second.valued() || !(*p).second.is_off() )
128  return true ;
129  }
130  return false ;
131 }
132 
133 inline
134 size_t G::OptionMap::count( const std::string & key ) const
135 {
136  size_t n = 0U ;
137  const Map::const_iterator end = m_map.end() ;
138  for( Map::const_iterator p = m_map.find(key) ; p != end && (*p).first == key ; ++p )
139  n++ ;
140  return n ;
141 }
142 
143 inline
144 std::string G::OptionMap::value( const std::string & key ) const
145 {
146  Map::const_iterator p = m_map.find( key ) ;
147  if( p == m_map.end() || !(*p).second.valued() )
148  return std::string() ;
149  else if( count(key) == 1U )
150  return (*p).second.value() ;
151  else
152  return join( p , std::upper_bound(p,m_map.end(),*p,m_map.value_comp()) ) ;
153 }
154 
155 inline
156 std::string G::OptionMap::join( Map::const_iterator p , Map::const_iterator end ) const
157 {
158  std::string result ;
159  for( const char * sep = "" ; p != end ; ++p )
160  {
161  if( (*p).second.valued() )
162  {
163  result.append( sep ) ; sep = "," ;
164  result.append( (*p).second.value() ) ;
165  }
166  }
167  return result ;
168 }
169 
170 #endif
size_t count(const std::string &key) const
Returns the number of times the key appears in the multimap.
Definition: goptionmap.h:134
bool contains(const std::string &) const
Returns true if the map contains the given key, but ignoring un-valued() 'off' options.
Definition: goptionmap.h:122
const_iterator begin() const
Returns the begin iterator.
Definition: goptionmap.h:98
std::string value(const std::string &key) const
Returns the value of the valued() option identified by the given key.
Definition: goptionmap.h:144
A map-like container for command-line options and their values.
Definition: goptionmap.h:38
const_iterator end() const
Returns the off-the-end iterator.
Definition: goptionmap.h:104
void insert(const Map::value_type &)
Inserts the key/value pair into the map.
Definition: goptionmap.h:92
OptionMap()
Default constructor for an empty map.
Definition: goptionmap.h:87
void clear()
Clears the map.
Definition: goptionmap.h:116
const_iterator find(const std::string &) const
Finds the map entry with the given key.
Definition: goptionmap.h:110