Re: [C++-sig] Iterators for heterogeneous container
by Michele De Stefano other posts by this author
Nov 5 2009 11:42PM messages near this date
[C++-sig] Iterators for heterogeneous container
|
Re: [C++-sig] Iterators for heterogeneous container
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@[...].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@[...].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
_______________________________________________
Cplusplus-sig mailing list
Cplusplus-sig@[...].org
http://mail.python.org/mailman/listinfo/cplusplus-sig
Thread:
Thomas Daniel
Michele De Stefano
Thomas Daniel
Troy D. Straszheim
Thomas Daniel
|