[C++-sig] Iterators for heterogeneous container

Michele De Stefano micdestefano at gmail.com
Fri Nov 6 08:42:15 CET 2009


Thomas,

I think the answer is here:
http://www.boost.org/doc/libs/1_40_0/libs/python/doc/tutorial/doc/html/python/iterators.html

but you should modify your "Garden" class in order to support "begin"
and "end" iterators for "tomatoes" and "potatoes".

With these modifications:

class Garden {
public:
  typedef ....      Tomato_Iter;
  typedef ....      Potato_Iter;

  Tomato_Iter   tomatoes_begin();
  Tomato_Iter   tomatoes_end();
  Potato_Iter   potatoes_begin();
  Potato_Iter   potatoes_end();
};

you can expose your Garden class as here:

class_<Garden>("Garden")
    .property("tomatoes", range(&Garden::tomatoes_begin, &Garden::tomatoes_end))
    .property("potatoes", range(&Garden::potatoes_begin,
&Garden::potatoes_end));

At this point, in Python, you should be able to write:

for potato in garden.potatoes:
   .... and do here whatever you want ...

It's the best I can suggest


2009/11/6 Thomas Daniel <thomasd57 at yahoo.com>:
> I am having trouble wrapping a container that can hold multiple object types
> and therefore has multiple iterator types.
>
> My C++ library has these classes:
>
> class Tomato;
> class Potato;
> class Garden {
>   Iter<Tomato>   get_tomatoes();
>   Iter<Potato>   get_potatoes();
> };
>
> template<class T>
> class Iter {
>   T get_next();
> };
>
> which allows to write:
>
> Iter<Potato> potatoes = garden.get_potatoes();
> while (Potato potato = potatoes.get_next()) {
>  cout << potato.name();
> }
>
> I am trying to use boost to wrap it so that I can write in python:
>
> for potato in garden.get_potatoes():
>  print potato.name()
>
> Any pointers how to achieve this with boost?
>
>
>
>
>
> _______________________________________________
> Cplusplus-sig mailing list
> Cplusplus-sig at python.org
> http://mail.python.org/mailman/listinfo/cplusplus-sig
>



-- 
Michele De Stefano
http://www.linkedin.com/in/micdestefano
http://xoomer.virgilio.it/michele_de_stefano
http://code.google.com/p/mds-utils


More information about the Cplusplus-sig mailing list