"Fossies" - the Fresh Open Source Software Archive 
Member "gnash-0.8.10/libbase/accumulator.h" (19 Jan 2012, 3442 Bytes) of package /linux/www/old/gnash-0.8.10.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code 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/function.hpp>
26 #include <vector>
27 #include <string>
28
29 /// An accumulating option value to handle multiple incrementing options.
30 template<typename T>
31 class accumulator_type : public boost::program_options::value_semantic
32 {
33 public:
34
35 accumulator_type() : _interval(1), _default(0) {}
36
37 /// Set the notifier function.
38 accumulator_type* notifier(boost::function1<void, const T&> f) {
39 _notifier = f;
40 return this;
41 }
42
43 /// Set the default value for this option.
44 accumulator_type* default_value(const T& t) {
45 _default = t;
46 return this;
47 }
48
49 /// Set the implicit value for this option.
50 //
51 /// Unlike for program_options::value, this specifies a value
52 /// to be applied on each occurence of the option.
53 accumulator_type* implicit_value(const T& t) {
54 _interval = t;
55 return this;
56 }
57
58 virtual std::string name() const { return std::string(); }
59
60 /// There are no tokens for an accumulator_type
61 virtual unsigned min_tokens() const { return 0; }
62 virtual unsigned max_tokens() const { return 0; }
63
64 /// Accumulating from different sources is silly.
65 virtual bool is_composing() const { return false; }
66
67 /// Requiring one or more appearances is unlikely.
68 virtual bool is_required() const { return false; }
69
70 /// Every appearance of the option simply increments the value
71 //
72 /// There should never be any tokens.
73 virtual void parse(boost::any& value_store,
74 const std::vector<std::string>& new_tokens,
75 bool /*utf8*/) const
76 {
77 assert(new_tokens.empty());
78 if (value_store.empty()) value_store = T();
79 boost::any_cast<T&>(value_store) += _interval;
80 }
81
82 /// If the option doesn't appear, this is the default value.
83 virtual bool apply_default(boost::any& value_store) const {
84 value_store = _default;
85 return true;
86 }
87
88 /// Notify the user function with the value of the value store.
89 virtual void notify(const boost::any& value_store) const {
90 if (_notifier) _notifier(boost::any_cast<T>(value_store));
91 }
92
93 virtual ~accumulator_type() {}
94
95 private:
96 boost::function1<void, const T&> _notifier;
97 T _interval;
98 T _default;
99 };
100
101 template<typename T>
102 accumulator_type<T>* accumulator() {
103 return new accumulator_type<T>();
104 }
105
106 #endif