[C++-sig] Re: Adding __len__ to range objects

Raoul Gough RaoulGough at yahoo.co.uk
Thu Aug 14 14:48:32 CEST 2003


David Abrahams <dave at boost-consulting.com> writes:
[snip]
> Anyway, I'm not going to implement this one myself.  Patches
> (including docs and tests) will be gratefully considered.

OK, I've added len and read-only indexing to boost::python::range,
dependant on iterator type. I've posted my current progress to
http://home.clara.net/raoulgough/boost/ - any comments are
welcome. There is a test script (currently stand-alone) and no docs
yet. I'll wait a few days to see if anybody suggests any changes
before advancing.

Basically, I've renamed iterator_next to iterator_range_utils, the
original execute to execute_next and added execute_getitem. Distance
is a separate free function, since it doesn't need any policies. Then
it's simply a case of deciding which of these to instantiate and add
to the returned Python object (done on the basis of is_convertible
with the std:: iterator_categories). The hooks in
demand_iterator_class look like this:

!       class_<range_> result (name, no_init);
! 
!       result
            .def("__iter__", identity_function())
!           .setattr("next", next_function);
! 
!       maybe_add_len
!           <is_convertible<Category, std::forward_iterator_tag>::value>
!           ::apply (result);
!      
!       maybe_add_getitem
!           <is_convertible<Category, std::random_access_iterator_tag>::value>
!           ::apply (result, policies);

Full details on the web site...

-- 
Raoul Gough
"Let there be one measure for wine throughout our kingdom, and one
measure for ale, and one measure for corn" - Magna Carta





More information about the Cplusplus-sig mailing list