Libosmium  2.1.0
Fast and flexible C++ library for working with OpenStreetMap data
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
options.hpp
Go to the documentation of this file.
1 #ifndef OSMIUM_UTIL_OPTIONS_HPP
2 #define OSMIUM_UTIL_OPTIONS_HPP
3 
4 /*
5 
6 This file is part of Osmium (http://osmcode.org/libosmium).
7 
8 Copyright 2013-2015 Jochen Topf <jochen@topf.org> and others (see README).
9 
10 Boost Software License - Version 1.0 - August 17th, 2003
11 
12 Permission is hereby granted, free of charge, to any person or organization
13 obtaining a copy of the software and accompanying documentation covered by
14 this license (the "Software") to use, reproduce, display, distribute,
15 execute, and transmit the Software, and to prepare derivative works of the
16 Software, and to permit third-parties to whom the Software is furnished to
17 do so, all subject to the following:
18 
19 The copyright notices in the Software and this entire statement, including
20 the above license grant, this restriction and the following disclaimer,
21 must be included in all copies of the Software, in whole or in part, and
22 all derivative works of the Software, unless such copies or derivative
23 works are solely in the form of machine-executable object code generated by
24 a source language processor.
25 
26 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32 DEALINGS IN THE SOFTWARE.
33 
34 */
35 
36 #include <initializer_list>
37 #include <map>
38 #include <string>
39 #include <utility>
40 
41 namespace osmium {
42 
43  namespace util {
44 
53  class Options {
54 
55  typedef std::map<std::string, std::string> option_map;
56  option_map m_options;
57 
58  public:
59 
60  typedef option_map::iterator iterator;
61  typedef option_map::const_iterator const_iterator;
62  typedef option_map::value_type value_type;
63 
64  Options() = default;
65 
66  explicit Options(const std::initializer_list<value_type>& values) :
67  m_options(values) {
68  }
69 
70  Options(const Options&) = default;
71  Options& operator=(const Options&) = default;
72 
73  Options(Options&&) = default;
74  Options& operator=(Options&&) = default;
75 
76  ~Options() = default;
77 
78  void set(const std::string& key, const std::string& value) {
79  m_options[key] = value;
80  }
81 
82  void set(const std::string& key, const char* value) {
83  m_options[key] = value;
84  }
85 
86  void set(const std::string& key, bool value) {
87  m_options[key] = value ? "true" : "false";
88  }
89 
90  void set(std::string data) {
91  size_t pos = data.find_first_of('=');
92  if (pos == std::string::npos) {
93  m_options[data] = "true";
94  } else {
95  std::string value = data.substr(pos+1);
96  data.erase(pos);
97  set(data, value);
98  }
99  }
100 
105  std::string get(const std::string& key, const std::string& default_value="") const noexcept {
106  auto it = m_options.find(key);
107  if (it == m_options.end()) {
108  return default_value;
109  }
110  return it->second;
111  }
112 
116  bool is_true(const std::string& key) const noexcept {
117  std::string value = get(key);
118  return (value == "true" || value == "yes");
119  }
120 
121  size_t size() const noexcept {
122  return m_options.size();
123  }
124 
125  iterator begin() noexcept {
126  return m_options.begin();
127  }
128 
129  iterator end() noexcept {
130  return m_options.end();
131  }
132 
133  const_iterator begin() const noexcept {
134  return m_options.cbegin();
135  }
136 
137  const_iterator end() const noexcept {
138  return m_options.cend();
139  }
140 
141  const_iterator cbegin() const noexcept {
142  return m_options.cbegin();
143  }
144 
145  const_iterator cend() const noexcept {
146  return m_options.cend();
147  }
148 
149  }; // class Options
150 
151  } // namespace util
152 
153 } // namespace osmium
154 
155 #endif // OSMIUM_UTIL_OPTIONS_HPP
void set(const std::string &key, bool value)
Definition: options.hpp:86
void set(std::string data)
Definition: options.hpp:90
std::map< std::string, std::string > option_map
Definition: options.hpp:55
const_iterator cbegin() const noexcept
Definition: options.hpp:141
option_map::iterator iterator
Definition: options.hpp:60
iterator end() noexcept
Definition: options.hpp:129
void set(const std::string &key, const char *value)
Definition: options.hpp:82
option_map m_options
Definition: options.hpp:56
Options & operator=(const Options &)=default
Options(const std::initializer_list< value_type > &values)
Definition: options.hpp:66
Definition: options.hpp:53
bool is_true(const std::string &key) const noexcept
Definition: options.hpp:116
Namespace for everything in the Osmium library.
Definition: assembler.hpp:55
const_iterator begin() const noexcept
Definition: options.hpp:133
option_map::const_iterator const_iterator
Definition: options.hpp:61
const_iterator cend() const noexcept
Definition: options.hpp:145
const_iterator end() const noexcept
Definition: options.hpp:137
size_t size() const noexcept
Definition: options.hpp:121
void set(const std::string &key, const std::string &value)
Definition: options.hpp:78
iterator begin() noexcept
Definition: options.hpp:125
option_map::value_type value_type
Definition: options.hpp:62