7 #ifndef SEQUENCE_ITERATOR_HPP_INCLUDED 8 #define SEQUENCE_ITERATOR_HPP_INCLUDED 11 #include <boost/iterator/iterator_facade.hpp> 40 class ProxyPySeqElement;
42 template<
class T,
class Reference = ProxyPySeqElement<T> >
43 class PySequenceIterator;
60 template<
class PySeqIt>
84 PyObject *
pPySeq()
const {
return seq_it.pPySeq; }
93 size_t cur_pos()
const {
return seq_it.cur_pos; }
102 size_t nel()
const {
return seq_it.nel; }
164 template<
class T,
class Reference>
166 public boost::iterator_facade<
167 PySequenceIterator<T>,
169 boost::bidirectional_traversal_tag,
174 friend class boost::iterator_core_access;
187 void seq_check(PyObject *o) {
188 if (!PySequence_Check(o)) {
189 throw std::invalid_argument(
"Input object is not a sequence");
193 Reference dereference()
const {
194 return Reference(*
this);
197 bool equal(
const PySequenceIterator& rhs)
const {
198 return cur_pos == rhs.cur_pos;
201 void increment() { ++
cur_pos; }
203 void decrement() { --
cur_pos; }
236 nel =
static_cast<size_t>(PySequence_Length(o));
237 cur_pos = (end) ? nel : 0;
243 pPySeq(rhs.pPySeq),nel(rhs.nel),cur_pos(rhs.cur_pos),
244 valid_obj(rhs.valid_obj) {
252 if (valid_obj) Py_DECREF(pPySeq);
257 PySequenceIterator& operator =(
const PySequenceIterator& rhs) {
258 if (
this == &rhs)
return *
this;
260 if (pPySeq != NULL) Py_DECREF(pPySeq);
266 cur_pos = rhs.cur_pos;
267 valid_obj = rhs.valid_obj;
280 PySequence_SetItem(seq_it.pPySeq(),seq_it.cur_pos(),v);
286 PyObject *py_el(PySequence_GetItem(seq_it.pPySeq(),seq_it.cur_pos()));
287 T retval(get<T>(py_el));
295 PyObject *py_el(PySequence_GetItem(seq_it.pPySeq(),seq_it.cur_pos()));
ProxyPySeqElement(const PySequenceIterator< T > &it)
Constructor.
PyObject * to_python(void)
Converts a value into a Python object.
PyObject * pPySeq() const
Returns the sequence object on which PySequenceIterator is working.
size_t nel() const
Returns the total number of elements of the python sequence.
Main namespace of all Michele De Stefano's C++ utilities.
Accessor class for the PySequenceIterator.
PySequenceIterator(PyObject *o, bool end=false)
Constructor that builds the start or the Past-the-end iterator.
PySeqIt_Access(PySeqIt &it)
Constructor.
PySequenceIterator(const PySequenceIterator &rhs)
The copy constructor.
PySequenceIterator()
Default constructor.
size_t cur_pos() const
Returns the current position of the PySequenceIterator instance.
Utilities for converting data from/to Python.
ProxyPySeqElement & operator=(const T &val)
Assignment.
Namespace of all Michele De Stefano's C++ Python utilities.
~PySequenceIterator()
Destructor.
Default Reference type for the PySequenceIterator class.