[C++-sig] Iterators for heterogeneous container

2009-11-05 Thread Thomas Daniel
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   get_tomatoes();
   Iter   get_potatoes();
};

template
class Iter {
   T get_next();
};

which allows to write:

Iter 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
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig


Re: [C++-sig] Iterators for heterogeneous container

2009-11-05 Thread Michele De Stefano
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")
.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 :
> 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   get_tomatoes();
>   Iter   get_potatoes();
> };
>
> template
> class Iter {
>   T get_next();
> };
>
> which allows to write:
>
> Iter 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
> [email protected]
> 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
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig