22 #ifndef GEARS_OPTPARSE_VALUE_HPP
23 #define GEARS_OPTPARSE_VALUE_HPP
25 #include "actions.hpp"
26 #include "../utility/helpers.hpp"
34 virtual ~value_base() =
default;
35 virtual void parse(
const std::string&,
const std::string&) = 0;
36 virtual bool is_active() const noexcept = 0;
37 virtual std::unique_ptr<value_base> clone() const = 0;
53 T* reference =
nullptr;
54 std::unique_ptr<T> value;
55 std::function<T(const std::string&, const std::string&)> action_;
58 void parse(
const std::string& key,
const std::string& val)
override {
59 auto&& result = action_(key, val);
61 if(reference !=
nullptr) {
62 *reference = std::move(result);
65 value.reset(
new T(std::move(result)));
69 std::unique_ptr<value_base> clone()
const override {
70 auto&& ptr = utility::make_unique<typed_value<T>>(action_);
71 ptr->reference = reference;
74 ptr->metavar = metavar;
75 return std::unique_ptr<value_base>{std::move(ptr)};
81 template<
typename Action>
118 template<
typename Action>
120 action_ = std::forward<Action>(action);
133 const T&
get()
const {
135 throw std::runtime_error(
"value has not been parsed");
138 if(reference !=
nullptr) {
152 const T&
get_or(
const T& def)
const noexcept {
154 if(reference !=
nullptr) {
176 template<
typename Container,
typename Action = store<
typename Container::value_type>>
177 inline std::unique_ptr<value_base>
compose(std::string metavar =
"", Action action = Action{}) {
178 auto&& ptr = utility::make_unique<typed_value<Container>>(
append<Container>{action});
179 ptr->metavar = std::move(metavar);
180 return std::unique_ptr<value_base>{std::move(ptr)};
203 template<
typename T,
typename Action = store<T>>
204 inline std::unique_ptr<value_base>
bind_to(T& t, std::string metavar =
"", Action action = Action{}) {
205 auto&& ptr = utility::make_unique<typed_value<T>>(t);
206 ptr->action(std::move(action));
207 ptr->metavar = std::move(metavar);
208 return std::unique_ptr<value_base>{std::move(ptr)};
224 inline std::unique_ptr<value_base>
constant(
const T& t) {
225 auto&& ptr = utility::make_unique<typed_value<T>>(
store_const<T>{t});
227 return std::unique_ptr<value_base>{std::move(ptr)};
243 template<
typename T,
typename Action = store<T>>
244 inline std::unique_ptr<value_base>
value(std::string metavar =
"", Action action = Action{}) {
245 auto&& ptr = utility::make_unique<typed_value<T>>(action);
246 ptr->metavar = std::move(metavar);
247 return std::unique_ptr<value_base>{std::move(ptr)};
264 template<
typename T,
typename Action>
265 inline std::unique_ptr<value_base>
custom(Action action, std::string metavar =
"") {
266 auto&& ptr = utility::make_unique<typed_value<T>>(action);
267 ptr->metavar = std::move(metavar);
268 return std::unique_ptr<value_base>{std::move(ptr)};
282 template<
typename Container,
typename Action = store<
typename Container::value_type>>
283 inline std::unique_ptr<value_base>
list(
size_t arguments, std::string metavar =
"", Action action = Action{}) {
284 static_assert(std::is_convertible<decltype(action(
"",
"")),
typename Container::value_type>::value,
285 "The action must return a type convertible to the container's value type");
287 ptr->nargs = arguments;
288 ptr->metavar = std::move(metavar);
289 return std::unique_ptr<value_base>{std::move(ptr)};
294 #endif // GEARS_OPTPARSE_VALUE_HPP