diff options
Diffstat (limited to 'boost/iterator/function_input_iterator.hpp')
-rw-r--r-- | boost/iterator/function_input_iterator.hpp | 45 |
1 files changed, 32 insertions, 13 deletions
diff --git a/boost/iterator/function_input_iterator.hpp b/boost/iterator/function_input_iterator.hpp index 8f1df6828c..8a793df1b7 100644 --- a/boost/iterator/function_input_iterator.hpp +++ b/boost/iterator/function_input_iterator.hpp @@ -1,4 +1,6 @@ // Copyright 2009 (C) Dean Michael Berris <me@deanberris.com> +// Copyright 2012 (C) Google, Inc. +// Copyright 2012 (C) Jeffrey Lee Hellrung, Jr. // 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) @@ -7,14 +9,19 @@ #ifndef BOOST_FUNCTION_INPUT_ITERATOR #define BOOST_FUNCTION_INPUT_ITERATOR +#include <boost/assert.hpp> #include <boost/mpl/if.hpp> #include <boost/function_types/is_function_pointer.hpp> #include <boost/function_types/is_function_reference.hpp> #include <boost/function_types/result_type.hpp> #include <boost/iterator/iterator_facade.hpp> +#include <boost/none.hpp> +#include <boost/optional/optional.hpp> namespace boost { +namespace iterators { + namespace impl { template <class Function, class Input> @@ -28,17 +35,20 @@ namespace boost { { public: function_input_iterator() {} - function_input_iterator(Function & f_, Input state_ = Input()) - : f(&f_), state(state_), value((*f)()) {} + function_input_iterator(Function & f_, Input state_ = Input()) + : f(&f_), state(state_) {} void increment() { - value = (*f)(); + if(value) + value = none; + else + (*f)(); ++state; } - typename Function::result_type const & + typename Function::result_type const & dereference() const { - return value; + return (value ? value : value = (*f)()).get(); } bool equal(function_input_iterator const & other) const { @@ -48,7 +58,7 @@ namespace boost { private: Function * f; Input state; - typename Function::result_type value; + mutable optional<typename Function::result_type> value; }; template <class Function, class Input> @@ -63,17 +73,19 @@ namespace boost { public: function_pointer_input_iterator() {} function_pointer_input_iterator(Function &f_, Input state_ = Input()) - : f(f_), state(state_), value((*f)()) - {} + : f(f_), state(state_) {} void increment() { - value = (*f)(); + if(value) + value = none; + else + (*f)(); ++state; } typename function_types::result_type<Function>::type const & dereference() const { - return value; + return (value ? value : value = (*f)()).get(); } bool equal(function_pointer_input_iterator const & other) const { @@ -83,7 +95,7 @@ namespace boost { private: Function f; Input state; - typename function_types::result_type<Function>::type value; + mutable optional<typename function_types::result_type<Function>::type> value; }; template <class Function, class Input> @@ -99,7 +111,7 @@ namespace boost { } // namespace impl template <class Function, class Input> - class function_input_iterator + class function_input_iterator : public mpl::if_< function_types::is_function_pointer<Function>, impl::function_pointer_input_iterator<Function,Input>, @@ -144,7 +156,14 @@ namespace boost { bool operator==(infinite &) const { return false; }; bool operator==(infinite const &) const { return false; }; }; -} + +} // namespace iterators + +using iterators::function_input_iterator; +using iterators::make_function_input_iterator; +using iterators::infinite; + +} // namespace boost #endif |