Boost logo

Boost :

From: Anthony Williams (anthwil_at_[hidden])
Date: 2002-09-12 09:43:40


David Abrahams writes:
> From: "Anthony Williams" <anthwil_at_[hidden]>
>
> > David Abrahams writes:
> > > It would probably be easy to derive your Policies from the generator
> > > adaptor policies.
> >
> > I contemplated that, but it requires that the Generator class be
> callable,
> > which isn't going to be the case for the upper bound; to avoid this, the
> > policy class is sufficiently different to warrant being unrelated.
> >
> > Do you think it is a sufficiently common case to be worth adding to the
> > iterator adaptors library alongside generator_iterator?
>
> Sure, sounds good.

OK, I've uploaded it to:

http://groups.yahoo.com/group/boost/files/bounded_generator_iterator.hpp

It needs the StrongStorage class that I uploaded earlier. Usage is as for
generator_iterator, except that the generator is not invoked unless the
iterator is dereferenced, and the iterators compare equal if the generators
compare equal, rather than if they are both using the same generator. Here is
the example from the generator_iterator page, modified to use the bounded
generator:

#include <iostream>
#include "bounded_generator_iterator.hpp"

class my_generator
{
public:
  typedef int result_type;
  my_generator() : state(0) { }
    my_generator(int i):
        state(i)
    {}
    
  int operator()() { return ++state; }

    friend bool operator==(const my_generator& lhs,const my_generator& rhs)
    {
        return lhs.state==rhs.state;
    }
        
private:
  int state;
};

int main()
{
  my_generator gen,endgen(10);
  for(boost::bounded_generator_iterator_generator<my_generator>::type
          it = boost::make_bounded_generator_iterator(gen),
          end = boost::make_bounded_generator_iterator(endgen);
      it!=end;
      ++it)
    std::cout << *it << std::endl;
}


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