All Classes Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
actions.hpp
1 // The MIT License (MIT)
2 
3 // Copyright (c) 2012-2014 Danny Y., Rapptz
4 
5 // Permission is hereby granted, free of charge, to any person obtaining a copy of
6 // this software and associated documentation files (the "Software"), to deal in
7 // the Software without restriction, including without limitation the rights to
8 // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 // the Software, and to permit persons to whom the Software is furnished to do so,
10 // subject to the following conditions:
11 
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 
22 #ifndef GEARS_OPTPAESE_ACTIONS_HPP
23 #define GEARS_OPTPAESE_ACTIONS_HPP
24 
25 #include "../string/lexical_cast.hpp"
26 #include <functional>
27 #include <iterator>
28 
29 namespace gears {
30 namespace optparse {
67 template<typename T>
68 struct store {
82  T operator()(const std::string&, const std::string& value) const {
83  return string::lexical_cast<T>(value);
84  }
85 };
86 
87 template<>
88 struct store<bool> {
89  bool operator()(const std::string& key, const std::string& value) const {
90  if(value == "1" || value == "true") {
91  return true;
92  }
93  else if(value == "0" || value == "false") {
94  return false;
95  }
96 
97  throw std::runtime_error("invalid boolean argument passed to '" + key + '\'');
98  }
99 };
100 
111 template<typename T>
112 struct store_const {
113 private:
114  T value;
115 public:
119  store_const(T val): value(std::move(val)) {}
120 
129  T operator()(const std::string&, const std::string&) const {
130  return value;
131  }
132 };
133 
145 template<typename Container, typename Action = store<typename Container::value_type>>
146 struct store_list {
147  static_assert(std::is_convertible<decltype(std::declval<Action>()("","")), typename Container::value_type>::value,
148  "The action must return a type convertible to the container's value type");
149 private:
150  Action action;
151 public:
155  store_list(Action action): action(std::move(action)) {}
167  Container operator()(const std::string& key, const std::string& value) const {
168  Container result;
169  std::istringstream ss(value);
170  std::insert_iterator<Container> it(result, result.end());
171  for(std::string str; std::getline(ss, str); ) {
172  *it = action(key, str);
173  }
174  return result;
175  }
176 };
177 
178 
189 template<typename Container>
190 struct append {
191 private:
192  using value_type = typename Container::value_type;
193  std::function<value_type(const std::string&, const std::string&)> action;
194  Container cont;
195 public:
196  template<typename Action>
197  append(Action action): action(std::move(action)) {}
198 
212  Container operator()(const std::string& key, const std::string& value) {
213  cont.insert(cont.end(), action(key, value));
214  return cont;
215  }
216 };
217 } // optparse
218 } // gears
219 
220 #endif // GEARS_OPTPAESE_ACTIONS_HPP