Gnash  0.8.10
accumulator.h
Go to the documentation of this file.
00001 // accumulator.h: accumulating value for boost program_options.
00002 // 
00003 //   Copyright (C) 2010, 2011, 2012 Free Software Foundation, Inc
00004 // 
00005 // This program is free software; you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation; either version 3 of the License, or
00008 // (at your option) any later version.
00009 // 
00010 // This program is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with this program; if not, write to the Free Software
00017 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018 //
00019 
00020 #ifndef PROGRAM_OPTIONS_ACCUMULATOR_HPP
00021 #define PROGRAM_OPTIONS_ACCUMULATOR_HPP
00022 
00023 #include <boost/program_options/value_semantic.hpp>
00024 #include <boost/any.hpp>
00025 #include <boost/function.hpp>
00026 #include <vector>
00027 #include <string>
00028 
00030 template<typename T>
00031 class accumulator_type : public boost::program_options::value_semantic
00032 {
00033 public:
00034 
00035     accumulator_type() : _interval(1), _default(0) {}
00036 
00038     accumulator_type* notifier(boost::function1<void, const T&> f) {
00039         _notifier = f;
00040         return this;
00041     }
00042 
00044     accumulator_type* default_value(const T& t) {
00045         _default = t;
00046         return this;
00047     }
00048 
00050     //
00053     accumulator_type* implicit_value(const T& t) {
00054         _interval = t;
00055         return this;
00056     }
00057 
00058     virtual std::string name() const { return std::string(); }
00059 
00061     virtual unsigned min_tokens() const { return 0; }
00062     virtual unsigned max_tokens() const { return 0; }
00063 
00065     virtual bool is_composing() const { return false; }
00066 
00068     virtual bool is_required() const { return false; }
00069     
00071     //
00073     virtual void parse(boost::any& value_store, 
00074                        const std::vector<std::string>& new_tokens,
00075                        bool /*utf8*/) const
00076     {
00077         assert(new_tokens.empty());
00078         if (value_store.empty()) value_store = T();
00079         boost::any_cast<T&>(value_store) += _interval;
00080     }
00081 
00083     virtual bool apply_default(boost::any& value_store) const {
00084         value_store = _default;
00085         return true;
00086     }
00087  
00089     virtual void notify(const boost::any& value_store) const {
00090         if (_notifier) _notifier(boost::any_cast<T>(value_store));
00091     }
00092     
00093     virtual ~accumulator_type() {}
00094 
00095 private:
00096     boost::function1<void, const T&> _notifier;
00097     T _interval;
00098     T _default;
00099 };
00100 
00101 template<typename T>
00102 accumulator_type<T>* accumulator() {
00103     return new accumulator_type<T>();
00104 }
00105 
00106 #endif