Boost logo

Boost :

From: Jeremy Siek (jsiek_at_[hidden])
Date: 2001-05-17 13:00:17


Actually, we do have a function_input_iterator, by another name. However,
it is hidden away in boost/random/detail/iterator_mixin.hpp. We really
need to move this into the iterator_adaptor.hpp header file so that people
have a reasonable chance of finding it. Jens, is that OK by you? (someone
will need to add docs of course)

template<class Generator, class ResultType>
class generator_iterator_mixin_adapter
  : incrementable<Generator>, equality_comparable<Generator>
{
public:
  typedef std::input_iterator_tag iterator_category;
  typedef ResultType value_type;
  typedef std::ptrdiff_t difference_type;
  typedef const value_type * pointer;
  typedef const value_type & reference;
  Generator& operator++() { v = cast()(); return cast(); }
  const value_type& operator*() const { return v; }

protected:
  // instantiate from derived classes only
  generator_iterator_mixin_adapter() { }
  void iterator_init() { operator++(); }
private:
  Generator & cast() { return static_cast<Generator&>(*this); }
  value_type v;
};

On 17 May 2001 nbecker_at_[hidden] wrote:

nbecke> I noticed we have a function_output_iterator, but I didn't see
nbecke> function_input_iterator. Here it is.
nbecke>
nbecke> Example usage:
nbecke>
nbecke> A function expects to take an iterator as an input. You want to
nbecke> provide a constant value. Use in conjuction with Constant.H:
nbecke>
nbecke> #ifndef Constant_H
nbecke> #define Constant_H
nbecke>
nbecke> #include <functional>
nbecke>
nbecke> template<typename out_t>
nbecke> class Constant : public unary_function<out_t, out_t>{
nbecke> const out_t val;
nbecke> public:
nbecke> Constant (out_t _val) :
nbecke> val (_val) {}
nbecke>
nbecke> template<typename T>
nbecke> out_t operator() (T x) const {
nbecke> return val;
nbecke> }
nbecke>
nbecke> out_t operator() () const {
nbecke> return val;
nbecke> }
nbecke> };
nbecke>
nbecke> #endif
nbecke>
nbecke> #ifndef function_input_iterator_hpp
nbecke> #define function_input_iterator_hpp
nbecke>
nbecke> #include <iterator>
nbecke>
nbecke> template <typename UnaryFunction>
nbecke> class function_input_iterator {
nbecke> typedef function_input_iterator self;
nbecke> public:
nbecke> typedef std::input_iterator_tag iterator_category;
nbecke> typedef typename UnaryFunction::result_type value_type;
nbecke> typedef void difference_type;
nbecke> typedef void pointer;
nbecke> typedef typename UnaryFunction::result_type& reference;
nbecke>
nbecke> explicit function_input_iterator (const UnaryFunction& f = UnaryFunction()) :
nbecke> m_f (f) {}
nbecke>
nbecke> value_type operator*() { return m_f(); }
nbecke> self& operator++() { return *this; }
nbecke> self& operator++(int) { return *this; }
nbecke> private:
nbecke> UnaryFunction m_f;
nbecke> };
nbecke>
nbecke> template <typename UnaryFunction>
nbecke> inline function_input_iterator<UnaryFunction>
nbecke> make_function_input_iterator(const UnaryFunction& f = UnaryFunction()) {
nbecke> return function_input_iterator<UnaryFunction>(f);
nbecke> }
nbecke>
nbecke> #endif
nbecke>
nbecke>
nbecke> To unsubscribe, send email to: <mailto:boost-unsubscribe_at_[hidden]>
nbecke>
nbecke>
nbecke> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
nbecke>
nbecke>
nbecke>

----------------------------------------------------------------------
 Jeremy Siek www: http://www.lsc.nd.edu/~jsiek/
 Ph.D. Candidate email: jsiek_at_[hidden]
 Univ. of Notre Dame work phone: (219) 631-3906
----------------------------------------------------------------------


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk