Gnash  0.8.11dev
accumulator.h
Go to the documentation of this file.
1 // accumulator.h: accumulating value for boost program_options.
2 //
3 // Copyright (C) 2010, 2011, 2012 Free Software Foundation, Inc
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 //
19 
20 #ifndef PROGRAM_OPTIONS_ACCUMULATOR_HPP
21 #define PROGRAM_OPTIONS_ACCUMULATOR_HPP
22 
23 #include <boost/program_options/value_semantic.hpp>
24 #include <boost/any.hpp>
25 #include <boost/version.hpp>
26 #include <functional>
27 #include <vector>
28 #include <string>
29 
31 template<typename T>
32 class accumulator_type : public boost::program_options::value_semantic
33 {
34 public:
35 
36  accumulator_type() : _interval(1), _default(0) {}
37 
39  accumulator_type* notifier(std::function<void(const T&)> f) {
40  _notifier = f;
41  return this;
42  }
43 
46  _default = t;
47  return this;
48  }
49 
51  //
55  _interval = t;
56  return this;
57  }
58 
59  virtual std::string name() const { return std::string(); }
60 
62  virtual unsigned min_tokens() const { return 0; }
63  virtual unsigned max_tokens() const { return 0; }
64 
65 #if BOOST_VERSION >= 105900
66  virtual bool adjacent_tokens_only() const { return false; }
68 #endif
69 
71  virtual bool is_composing() const { return false; }
72 
74  virtual bool is_required() const { return false; }
75 
77  //
79  virtual void parse(boost::any& value_store,
80  const std::vector<std::string>& new_tokens,
81  bool /*utf8*/) const
82  {
83  assert(new_tokens.empty());
84  if (value_store.empty()) value_store = T();
85  boost::any_cast<T&>(value_store) += _interval;
86  }
87 
89  virtual bool apply_default(boost::any& value_store) const {
90  value_store = _default;
91  return true;
92  }
93 
95  virtual void notify(const boost::any& value_store) const {
96  if (_notifier) _notifier(boost::any_cast<T>(value_store));
97  }
98 
99  virtual ~accumulator_type() {}
100 
101 private:
102  std::function<void(const T&)> _notifier;
103  T _interval;
104  T _default;
105 };
106 
107 template<typename T>
109  return new accumulator_type<T>();
110 }
111 
112 #endif
virtual unsigned max_tokens() const
Definition: accumulator.h:63
virtual void parse(boost::any &value_store, const std::vector< std::string > &new_tokens, bool) const
Every appearance of the option simply increments the value.
Definition: accumulator.h:79
virtual unsigned min_tokens() const
There are no tokens for an accumulator_type.
Definition: accumulator.h:62
accumulator_type * default_value(const T &t)
Set the default value for this option.
Definition: accumulator.h:45
accumulator_type()
Definition: accumulator.h:36
virtual std::string name() const
Definition: accumulator.h:59
Definition: GnashKey.h:152
accumulator_type * notifier(std::function< void(const T &)> f)
Set the notifier function.
Definition: accumulator.h:39
Definition: GnashKey.h:166
virtual bool is_composing() const
Accumulating from different sources is silly.
Definition: accumulator.h:71
An accumulating option value to handle multiple incrementing options.
Definition: accumulator.h:32
virtual bool apply_default(boost::any &value_store) const
If the option doesn&#39;t appear, this is the default value.
Definition: accumulator.h:89
accumulator_type< T > * accumulator()
Definition: accumulator.h:108
Definition: GnashKey.h:132
accumulator_type * implicit_value(const T &t)
Set the implicit value for this option.
Definition: accumulator.h:54
virtual ~accumulator_type()
Definition: accumulator.h:99
virtual void notify(const boost::any &value_store) const
Notify the user function with the value of the value store.
Definition: accumulator.h:95
virtual bool is_required() const
Requiring one or more appearances is unlikely.
Definition: accumulator.h:74