Crazy Eddie's GUI System  ${CEGUI_VERSION}
python_iterator.hpp
1 // This file has been generated by Py++.
2 
3 // -*- mode:c++ -*-
4 //
5 // Header file python_iterator.hpp
6 //
7 // Handy Python iterable iterators
8 //
9 // Copyright (c) 2003 Raoul M. Gough
10 //
11 // Use, modification and distribution is subject to the Boost Software
12 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy
13 // at http://www.boost.org/LICENSE_1_0.txt)
14 //
15 // History
16 // =======
17 // 2003/ 9/10 rmg File creation
18 // 2008/12/08 Roman Change indexing suite layout
19 //
20 // $Id: python_iterator.hpp,v 1.1.2.5 2003/11/24 16:35:09 raoulgough Exp $
21 //
22 // 2008 November 27 Roman Yakovenko
23 // implementation of the member functions was moved from cpp to header.
24 // this was done to simplify "installation" procedure.
25 
26 #ifndef BOOST_PYTHON_INDEXING_PYTHON_ITERATOR_HPP
27 #define BOOST_PYTHON_INDEXING_PYTHON_ITERATOR_HPP
28 
29 #include <boost/python/object.hpp>
30 #include <boost/python/handle.hpp>
31 
32 namespace boost { namespace python { namespace indexing {
33  struct /*BOOST_PYTHON_DECL*/ python_iterator
34  {
35  python_iterator (boost::python::object obj)
36  : m_iter_obj (handle<> (PyObject_GetIter (obj.ptr()))),
37  m_next_method (m_iter_obj.attr ("next")),
38  m_current()
39  {
40  }
41 
42  // Sets a python type exception and calls throw_error_already_set if
43  // the passed object is not iterable via PyObject_GetIter
44 
45  bool next ()
46  {
47  bool result = true; // Assume success
48 
49  try
50  {
51  m_current = m_next_method ();
52  }
53  catch (boost::python::error_already_set const &)
54  {
55  if (PyErr_ExceptionMatches (PyExc_StopIteration))
56  {
57  // Eat this exception
58  PyErr_Clear ();
59  m_current = boost::python::object (); // No current object
60  result = false; // Report failure via return value only
61  }
62  else
63  {
64  // Pass it up the line
65  throw;
66  }
67  }
68  return result;
69  }
70  // Get the next item from the iterator, returning true for success
71 
72  boost::python::object current() const
73  { return m_current; }
74  // Callable only after a successful next()
75 
76  private:
77  ::boost::python::object m_iter_obj;
78  ::boost::python::object m_next_method;
79  ::boost::python::object m_current;
80  };
81 } } }
82 
83 #endif // BOOST_PYTHON_INDEXING_PYTHON_ITERATOR_HPP
84 
85 
86 
Definition: python_CEGUI.h:11
Definition: python_iterator.hpp:33