Aktualizr
C++ SOTA Client
All Classes Namespaces Files Functions Variables Enumerations Enumerator Pages
accumulator.h
1 // accumulator.hpp header file
2 //
3 // (C) Copyright benjaminwolsey.de 2010-2011. Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 
7 #ifndef PROGRAM_OPTIONS_ACCUMULATOR_HPP
8 #define PROGRAM_OPTIONS_ACCUMULATOR_HPP
9 
10 #include <functional>
11 #include <string>
12 #include <vector>
13 
14 #include <boost/any.hpp>
15 #include <boost/program_options/value_semantic.hpp>
16 
17 /// An accumulating option value to handle multiple incrementing options.
18 template <typename T>
19 class accumulator_type : public boost::program_options::value_semantic {
20  public:
21  explicit accumulator_type(T* store) : _store(store), _interval(1), _default(0) {}
22 
23  /// Set the notifier function.
24  accumulator_type* notifier(std::function<void(const T&)> f) {
25  _notifier = f;
26  return this;
27  }
28 
29  /// Set the default value for this option.
31  _default = t;
32  return this;
33  }
34 
35  /// Set the implicit value for this option.
36  //
37  /// Unlike for program_options::value, this specifies a value
38  /// to be applied on each occurrence of the option.
40  _interval = t;
41  return this;
42  }
43 
44  virtual std::string name() const { return std::string(); } // NOLINT
45 
46  /// There are no tokens for an accumulator_type
47  virtual unsigned min_tokens() const { return 0; } // NOLINT
48  virtual unsigned max_tokens() const { return 0; } // NOLINT
49 
50  virtual bool adjacent_tokens_only() const { return false; } // NOLINT
51 
52  /// Accumulating from different sources is silly.
53  virtual bool is_composing() const { return false; } // NOLINT
54 
55  /// Requiring one or more appearances is unlikely.
56  virtual bool is_required() const { return false; } // NOLINT
57 
58  /// Every appearance of the option simply increments the value
59  //
60  /// There should never be any tokens.
61  virtual void parse(boost::any& value_store, const std::vector<std::string>&, bool /*utf8*/) const { // NOLINT
62  if (value_store.empty()) {
63  value_store = T();
64  }
65  boost::any_cast<T&>(value_store) += _interval;
66  }
67 
68  /// If the option doesn't appear, this is the default value.
69  virtual bool apply_default(boost::any& value_store) const { // NOLINT
70  value_store = _default;
71  return true;
72  }
73 
74  /// Notify the user function with the value of the value store.
75  virtual void notify(const boost::any& value_store) const { // NOLINT
76  const auto* val = boost::any_cast<T>(&value_store);
77  if (_store) {
78  *_store = *val;
79  }
80  if (_notifier) {
81  _notifier(*val);
82  }
83  }
84 
85  virtual ~accumulator_type() {} // NOLINT
86 
87  private:
88  T* _store;
89  std::function<void(const T&)> _notifier;
90  T _interval;
91  T _default;
92 };
93 
94 template <typename T>
95 accumulator_type<T>* accumulator() {
96  return new accumulator_type<T>(0);
97 }
98 
99 template <typename T>
100 accumulator_type<T>* accumulator(T* store) {
101  return new accumulator_type<T>(store);
102 }
103 
104 #endif
accumulator_type::notifier
accumulator_type * notifier(std::function< void(const T &)> f)
Set the notifier function.
Definition: accumulator.h:24
accumulator_type::is_required
virtual bool is_required() const
Requiring one or more appearances is unlikely.
Definition: accumulator.h:56
accumulator_type::is_composing
virtual bool is_composing() const
Accumulating from different sources is silly.
Definition: accumulator.h:53
accumulator_type::parse
virtual void parse(boost::any &value_store, const std::vector< std::string > &, bool) const
Every appearance of the option simply increments the value.
Definition: accumulator.h:61
accumulator_type
An accumulating option value to handle multiple incrementing options.
Definition: accumulator.h:19
accumulator_type::notify
virtual void notify(const boost::any &value_store) const
Notify the user function with the value of the value store.
Definition: accumulator.h:75
accumulator_type::apply_default
virtual bool apply_default(boost::any &value_store) const
If the option doesn't appear, this is the default value.
Definition: accumulator.h:69
accumulator_type::implicit_value
accumulator_type * implicit_value(const T &t)
Set the implicit value for this option.
Definition: accumulator.h:39
accumulator_type::min_tokens
virtual unsigned min_tokens() const
There are no tokens for an accumulator_type.
Definition: accumulator.h:47
accumulator_type::default_value
accumulator_type * default_value(const T &t)
Set the default value for this option.
Definition: accumulator.h:30