[PATCH 06/19] Wrap iterators on mln::box2d.

* box_piter.ixx, box2d_piter.i: New. * box2d.i (__iter__): New method (extension). * Makefile.am (meta_wrappers): Add box_piter.i (wrappers): Add box2d_piter.i. * python/swilena.py: Import module `box2d' before `point2d'. * python/Makefile.am (pyexec_LTLIBRARIES): Add _box2d.la. (nodist__box2d_la_SOURCES, _box2d_la_LIBADD): New variables. (CLEANFILES): Add $(nodist__box2d_la_SOURCES) box2d.py box2d.py[co] (nodist_python_PYTHON): Add box2d.py. --- swilena/ChangeLog | 14 ++++ swilena/Makefile.am | 9 ++- swilena/box2d.i | 11 +++ swilena/box2d_piter.i | 154 ++++++++++++++++++++++++++++++++++++++++++++ swilena/box_piter.ixx | 39 +++++++++++ swilena/python/Makefile.am | 10 +++ swilena/python/swilena.py | 4 +- 7 files changed, 235 insertions(+), 6 deletions(-) create mode 100644 swilena/box2d_piter.i create mode 100644 swilena/box_piter.ixx diff --git a/swilena/ChangeLog b/swilena/ChangeLog index 917269e..f9bf4a4 100644 --- a/swilena/ChangeLog +++ b/swilena/ChangeLog @@ -1,5 +1,19 @@ 2009-05-26 Roland Levillain <roland@lrde.epita.fr> + Wrap iterators on mln::box2d. + + * box_piter.ixx, box2d_piter.i: New. + * box2d.i (__iter__): New method (extension). + * Makefile.am (meta_wrappers): Add box_piter.i + (wrappers): Add box2d_piter.i. + * python/swilena.py: Import module `box2d' before `point2d'. + * python/Makefile.am (pyexec_LTLIBRARIES): Add _box2d.la. + (nodist__box2d_la_SOURCES, _box2d_la_LIBADD): New variables. + (CLEANFILES): Add $(nodist__box2d_la_SOURCES) box2d.py box2d.py[co] + (nodist_python_PYTHON): Add box2d.py. + +2009-05-26 Roland Levillain <roland@lrde.epita.fr> + * box2d.i: Add a ctor taking two `int's as argument. 2009-05-26 Roland Levillain <roland@lrde.epita.fr> diff --git a/swilena/Makefile.am b/swilena/Makefile.am index ef711d8..d20d238 100644 --- a/swilena/Makefile.am +++ b/swilena/Makefile.am @@ -5,13 +5,14 @@ SUBDIRS = python # Meta-wrappers (templates), not generating a module, but factoring # common parts. meta_wrappers = \ - ch_value.ixx concrete.ixx fill.ixx image2d.ixx intp.ixx \ - int_u.ixx morpho.ixx pgm.ixx println.ixx + box_piter.i ch_value.ixx concrete.ixx fill.ixx image2d.ixx \ + intp.ixx int_u.ixx morpho.ixx pgm.ixx println.ixx # Wrappers (generating actual modules). wrappers = \ - box2d.i dpoint2d.i image2d_int.i image2d_int_u8.i int_u8.i \ - int_u32.i neighb2d.i point2d.i window2d.i \ + box2d.i box2d_piter.i dpoint2d.i image2d_int.i \ + image2d_int_u8.i int_u8.i int_u32.i neighb2d.i point2d.i \ + window2d.i \ \ dynamic_image2d.i diff --git a/swilena/box2d.i b/swilena/box2d.i index e24f016..7a25565 100644 --- a/swilena/box2d.i +++ b/swilena/box2d.i @@ -31,6 +31,8 @@ %module box2d +%import box2d_piter.i + %{ #include "mln/core/site_set/box.hh" #include "mln/core/alias/box2d.hh" @@ -70,6 +72,15 @@ return new mln::box<mln::point2d>(nrows, ncols); } + // FIXME: Shorten using macros... + mln::box_fwd_piter_< mln::point<mln::grid::square, mln::def::coord> > + __iter__() const + { + mln::box_fwd_piter_<mln::point2d> p(*$self); + p.start(); + return p; + } + unsigned nrows() const { // FIXME: This is the exact content of box_impl_<2, C, E>::nrows. diff --git a/swilena/box2d_piter.i b/swilena/box2d_piter.i new file mode 100644 index 0000000..f5ac026 --- /dev/null +++ b/swilena/box2d_piter.i @@ -0,0 +1,154 @@ +// -*- C++ -*- +// Copyright (C) 2009 EPITA Research and Development Laboratory (LRDE) +// +// This file is part of the Olena Library. This library is free +// software; you can redistribute it and/or modify it under the terms +// of the GNU General Public License version 2 as published by the +// Free Software Foundation. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this library; see the file COPYING. If not, write to +// the Free Software Foundation, 51 Franklin Street, Fifth Floor, +// Boston, MA 02111-1307, USA. +// +// As a special exception, you may use this file as part of a free +// software library without restriction. Specifically, if other files +// instantiate templates or use macros or inline functions from this +// file, or you compile this file and link it with other files to +// produce an executable, this file does not by itself cause the +// resulting executable to be covered by the GNU General Public +// License. +// reasons why the executable file might be covered by the GNU General +// Public License. + +/// \file box2d_piter.i +/// \brief A wrapper of mln::box_fwd_piter_< mln::point2d >. + +%module box2d_piter + +%include box_piter.ixx + +%{ +#include "mln/core/alias/point2d.hh" +%} + +%import point2d.i + +// FIXME: Move reusable parts elsewhere (in box_piter.i and/or another +// file). + +// Ignore internal methods. +%ignore mln::box_fwd_piter_< mln::point<mln::grid::square, mln::def::coord> >::is_valid_(); +%ignore mln::box_fwd_piter_< mln::point<mln::grid::square, mln::def::coord> >::invalidate_(); +%ignore mln::box_fwd_piter_< mln::point<mln::grid::square, mln::def::coord> >::start_(); +%ignore mln::box_fwd_piter_< mln::point<mln::grid::square, mln::def::coord> >::next_(); + +// Ignore the backward iterator (for the moment). +%ignore mln::box_bkd_piter_< mln::point<mln::grid::square, mln::def::coord> >; + + +/*--------------------. +| Generic interface. | +`--------------------*/ + +/* These methods are mostly for debugging purpose: we should use the + language's interface (e.g. Python's `next()') to manipulate an + iterator. */ +/* FIXME: Except for `site()', these methods could be generated by + Swig by wrapping base classes mln::Iterator and mln::Site_Iterator + of mln::box_fwd_piter_ (and renaming Site_Iterator's `next()' as + `advance()' to avoid a name clash with the new `next()' method + added to Python's code. */ +%extend mln::box_fwd_piter_< mln::point<mln::grid::square, mln::def::coord> > +{ + // Return the site pointed by an iterator. + const mln::point<mln::grid::square, mln::def::coord> + site() + { + return *$self; + } + + // Initiate the iterator. + void + start() + { + $self->start(); + } + + // Move the iterator forward. + void + advance() + { + $self->next(); + } + + // Is the iterator valid? + bool + is_valid() + { + return $self->is_valid(); + } + + // Invalidate the iterator. + void + invalidate() + { + $self->invalidate(); + } +} + + +/*-------------------. +| Python interface. | +`-------------------*/ + +#if SWIGPYTHON + +// Handling iterators à la Python. + +%extend mln::box_fwd_piter_< mln::point<mln::grid::square, mln::def::coord> > +{ + mln::box_fwd_piter_< mln::point<mln::grid::square, mln::def::coord> >& + __iter__() + { + return *$self; + } +} + +// Raise a Python `StopIteration' exception on `next()' if the +// iterator is invalid. +%exception mln::box_fwd_piter_< mln::point<mln::grid::square, mln::def::coord> >::next +{ + /* FIXME: Is it safe to use `arg1'? It seems to be pretty + low-level, and may not be part of Swig's standard interface. */ + if (!arg1->is_valid()) + { + PyErr_SetString(PyExc_StopIteration, "Invalid iterator"); + return NULL; + } + $action +} + +%extend mln::box_fwd_piter_< mln::point<mln::grid::square, mln::def::coord> > +{ + const mln::point<mln::grid::square, mln::def::coord> + next() + { + // Keep a copy of the current site before incrementation. + const mln::point<mln::grid::square, mln::def::coord> current = *$self; + // Delegate incrementation to `Super_Iterator's `next()' method. + $self->next(); + return current; + } +} + +#endif // !SWIGPYTHON + + +%template(box2d_piter) +mln::box_fwd_piter_< mln::point<mln::grid::square, mln::def::coord> >; diff --git a/swilena/box_piter.ixx b/swilena/box_piter.ixx new file mode 100644 index 0000000..7b34272 --- /dev/null +++ b/swilena/box_piter.ixx @@ -0,0 +1,39 @@ +// -*- C++ -*- +// Copyright (C) 2009 EPITA Research and Development Laboratory (LRDE) +// +// This file is part of the Olena Library. This library is free +// software; you can redistribute it and/or modify it under the terms +// of the GNU General Public License version 2 as published by the +// Free Software Foundation. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this library; see the file COPYING. If not, write to +// the Free Software Foundation, 51 Franklin Street, Fifth Floor, +// Boston, MA 02111-1307, USA. +// +// As a special exception, you may use this file as part of a free +// software library without restriction. Specifically, if other files +// instantiate templates or use macros or inline functions from this +// file, or you compile this file and link it with other files to +// produce an executable, this file does not by itself cause the +// resulting executable to be covered by the GNU General Public +// License. +// reasons why the executable file might be covered by the GNU General +// Public License. + +/// \file box_piter.ixx +/// \brief A wrapper of mln::box_fwd_piter_<P>. + +%module box_piter + +%{ + +#include "mln/core/site_set/box_piter.hh" +%} + +%include "mln/core/site_set/box_piter.hh" diff --git a/swilena/python/Makefile.am b/swilena/python/Makefile.am index 110a9c0..be2effc 100644 --- a/swilena/python/Makefile.am +++ b/swilena/python/Makefile.am @@ -77,6 +77,16 @@ CLEANFILES += $(nodist__box2d_la_SOURCES) box2d.py box2d.py[co] @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/_box2d-wrap.Pcc@am__quote@ nodist_python_PYTHON += box2d.py +## box2d_piter. +pyexec_LTLIBRARIES += _box2d_piter.la +nodist__box2d_piter_la_SOURCES = box2d_piter-wrap.cc +_box2d_piter_la_LIBADD = $(AM_LIBADD) +CLEANFILES += $(nodist__box2d_piter_la_SOURCES) box2d_piter.py box2d_piter.py[co] +## Include the dependency files. Copied from Automake's generated +## case for C++. +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/_box2d_piter-wrap.Pcc@am__quote@ +nodist_python_PYTHON += box2d_piter.py + ## int_u8. pyexec_LTLIBRARIES += _int_u8.la nodist__int_u8_la_SOURCES = int_u8-wrap.cc diff --git a/swilena/python/swilena.py b/swilena/python/swilena.py index e5a7e45..601b6ee 100644 --- a/swilena/python/swilena.py +++ b/swilena/python/swilena.py @@ -1,6 +1,6 @@ #! /usr/bin/env python -# Copyright (C) 2008 EPITA Research and Development Laboratory (LRDE) +# Copyright (C) 2008, 2009 EPITA Research and Development Laboratory (LRDE) # # This file is part of the Olena Library. This library is free # software; you can redistribute it and/or modify it under the terms @@ -32,9 +32,9 @@ import ltihooks +from box2d import * from point2d import * from dpoint2d import * -from box2d import * from neighb2d import * from window2d import * -- 1.6.1.2
participants (1)
-
Roland Levillain