// Copyright Dean Michael Berris 2008. // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) #ifndef BOOST_GENERATORS_20080617 #define BOOST_GENERATORS_20080617 #include namespace boost { template struct generator { typedef ResultType result_type; typedef InputType input_type; generator() : _function() { }; /// Constructor taking a function as the sole argument generator(boost::function const & f) : _function(f) { }; void set_function(boost::function const & f) { _function = f; }; result_type operator[] (input_type const & input) { return _function(input); }; result_type operator[] (input_type const & input) const { return _function(input); }; /// Define an iterator struct iterator { iterator() : _input(), _function(), end(true) { }; iterator operator++ (int) const { return iterator(_function, _input++); }; iterator & operator++ (void) { ++_input; return *this; }; result_type operator* (void) { return _function(_input); }; bool operator==(iterator const & rhs) { return (end && rhs.end && _input == rhs._input); }; bool operator!=(iterator const & rhs) { return !(*this == rhs); }; iterator & operator=(iterator rhs) { rhs.swap(*this); return *this; }; void swap(iterator & other) { std::swap(_input, other._input); std::swap(_function, other._function); std::swap(end, other.end); }; typedef std::input_iterator_tag iterator_category; typedef result_type value_type; typedef void difference_type; typedef void pointer; typedef void reference; private: friend struct generator; explicit iterator(boost::function const & f) : _input(), _function(f), end(false) { }; explicit iterator(boost::function const & f, input_type i) : _input(i), _function(f), end(false) { }; mutable input_type _input; mutable boost::function _function; mutable bool end; }; typedef iterator const const_iterator; iterator begin() { return iterator(_function); }; const_iterator begin() const { return const_iterator(_function); }; iterator end() { return iterator(); }; const_iterator end() const { return const_iterator(); }; private: mutable boost::function _function; }; }; #endif