Fix test for bug #32625
[gnash.git] / libbase / accumulator.h
blob84ccee38018e408255092261b97dcfbeddfeb70a
1 // accumulator.h: accumulating value for boost program_options.
2 //
3 // Copyright (C) 2010, 2011 Free Software Foundation, Inc
4 // Copyright (C) 2010, 2011 benjaminwolsey.de
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #ifndef PROGRAM_OPTIONS_ACCUMULATOR_HPP
22 #define PROGRAM_OPTIONS_ACCUMULATOR_HPP
24 #include <boost/program_options/value_semantic.hpp>
25 #include <boost/any.hpp>
26 #include <boost/function.hpp>
27 #include <vector>
28 #include <string>
30 /// An accumulating option value to handle multiple incrementing options.
31 template<typename T>
32 class accumulator_type : public boost::program_options::value_semantic
34 public:
36 accumulator_type() : _interval(1), _default(0) {}
38 /// Set the notifier function.
39 accumulator_type* notifier(boost::function1<void, const T&> f) {
40 _notifier = f;
41 return this;
44 /// Set the default value for this option.
45 accumulator_type* default_value(const T& t) {
46 _default = t;
47 return this;
50 /// Set the implicit value for this option.
52 /// Unlike for program_options::value, this specifies a value
53 /// to be applied on each occurence of the option.
54 accumulator_type* implicit_value(const T& t) {
55 _interval = t;
56 return this;
59 virtual std::string name() const { return std::string(); }
61 /// There are no tokens for an accumulator_type
62 virtual unsigned min_tokens() const { return 0; }
63 virtual unsigned max_tokens() const { return 0; }
65 /// Accumulating from different sources is silly.
66 virtual bool is_composing() const { return false; }
68 /// Requiring one or more appearances is unlikely.
69 virtual bool is_required() const { return false; }
71 /// Every appearance of the option simply increments the value
73 /// There should never be any tokens.
74 virtual void parse(boost::any& value_store,
75 const std::vector<std::string>& new_tokens,
76 bool /*utf8*/) const
78 assert(new_tokens.empty());
79 if (value_store.empty()) value_store = T();
80 boost::any_cast<T&>(value_store) += _interval;
83 /// If the option doesn't appear, this is the default value.
84 virtual bool apply_default(boost::any& value_store) const {
85 value_store = _default;
86 return true;
89 /// Notify the user function with the value of the value store.
90 virtual void notify(const boost::any& value_store) const {
91 if (_notifier) _notifier(boost::any_cast<T>(value_store));
94 virtual ~accumulator_type() {}
96 private:
97 boost::function1<void, const T&> _notifier;
98 T _interval;
99 T _default;
102 template<typename T>
103 accumulator_type<T>* accumulator() {
104 return new accumulator_type<T>();
107 #endif