Crazy Eddie's GUI System  ${CEGUI_VERSION}
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
slice.hpp
1 // This file has been generated by Py++.
2 
3 // Header file slice.hpp
4 //
5 // Copyright (c) 2003 Raoul M. Gough
6 //
7 // Use, modification and distribution is subject to the Boost Software
8 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy
9 // at http://www.boost.org/LICENSE_1_0.txt)
10 //
11 // History
12 // =======
13 // 2003/ 9/10 rmg File creation
14 // 2008/12/08 Roman Change indexing suite layout
15 //
16 // $Id: slice.hpp,v 1.1.2.10 2003/11/24 14:28:31 raoulgough Exp $
17 //
18 // 2008 November 27 Roman Yakovenko
19 // implementation of the member functions was moved from cpp to header.
20 // this was done to simplify "installation" procedure.
21 
22 #ifndef BOOST_PYTHON_INDEXING_SLICE_HPP
23 #define BOOST_PYTHON_INDEXING_SLICE_HPP
24 
25 #include <boost/python/object.hpp>
26 #include <boost/python/errors.hpp>
27 #include <boost/python/converter/pytype_object_mgr_traits.hpp>
28 #include <algorithm>
29 
30 namespace boost { namespace python { namespace indexing {
31  struct /*BOOST_PYTHON_DECL*/ slice : public boost::python::object
32  {
33  // This is just a thin wrapper around boost::python::object
34  // so that it is possible to register a special converter for
35  // PySlice_Type and overload C++ functions on slice
36 
37 #if defined (BOOST_NO_MEMBER_TEMPLATES)
38  // MSVC6 doesn't seem to be able to invoke the templated
39  // constructor, so provide explicit overloads to match the
40  // (currently) known boost::python::object constructors
41  explicit slice (::boost::python::handle<> const& p)
42  : object (p)
43  {}
44 
45  explicit slice (::boost::python::detail::borrowed_reference p)
46  : object (p)
47  {}
48 
49  explicit slice (::boost::python::detail::new_reference p)
50  : object (p)
51  {}
52 
53  explicit slice (::boost::python::detail::new_non_null_reference p)
54  : object (p)
55  {}
56 #else
57  // Better compilers make life easier
58  template<typename T> inline slice (T const &ref);
59 #endif
60 
61  slice (slice const & copy) // Copy constructor
62  : object (copy)
63  {}
64  };
65 
66  struct /*BOOST_PYTHON_DECL*/ integer_slice
67  {
68  // This class provides a convenient interface to Python slice
69  // objects that contain integer bound and stride values.
70 
71  #if PY_VERSION_HEX < 0x02050000
72  typedef int index_type;
73  #else
74  typedef Py_ssize_t index_type;
75  #endif
76 
77  integer_slice (slice const & sl, index_type length)
78  : m_slice (sl) // Leave index members uninitialized
79  {
80  PySlice_GetIndices(
81  #if PY_VERSION_HEX > 0x03020000
82  reinterpret_cast<PyObject *> (m_slice.ptr()),
83  #else
84  reinterpret_cast<PySliceObject *> (m_slice.ptr()),
85  #endif
86  length,
87  &m_start,
88  &m_stop,
89  &m_step);
90 
91  if (m_step == 0)
92  {
93  // Can happen with Python prior to 2.3
94  PyErr_SetString (PyExc_ValueError, "slice step cannot be zero");
95  boost::python::throw_error_already_set ();
96  }
97 
98  m_start = std::max (static_cast<index_type> (0), std::min (length, m_start));
99  m_stop = std::max (static_cast<index_type> (0), std::min (length, m_stop));
100  m_direction = (m_step > 0) ? 1 : -1;
101  }
102 
103  // integer_slice must know how big the container is so it can
104  // adjust for negative indexes, etc...
105 
106  index_type start() const { return m_start; }
107  index_type step() const { return m_step; }
108  index_type stop() const { return m_stop; }
109 
110  index_type size() const { return (m_stop - m_start) / m_step; }
111 
112  bool in_range (index_type index)
113  { return ((m_stop - index) * m_direction) > 0; }
114 
115  private:
116  slice m_slice;
117  index_type m_start;
118  index_type m_step;
119  index_type m_stop;
120  index_type m_direction;
121  };
122 } } }
123 
124 #if !defined (BOOST_NO_MEMBER_TEMPLATES)
125 template<typename T>
126 boost::python::indexing::slice::slice (T const &ref)
127  : boost::python::object (ref)
128 {
129  if (!PySlice_Check (this->ptr()))
130  {
131  PyErr_SetString(
132  PyExc_TypeError, "slice constructor: passed a non-slice object");
133 
134  boost::python::throw_error_already_set();
135  }
136 }
137 #endif
138 
139 namespace boost { namespace python { namespace converter {
140  // Specialized converter to handle PySlice_Type objects
141  template<>
142  struct object_manager_traits<boost::python::indexing::slice>
143  : pytype_object_manager_traits<
144  &PySlice_Type, ::boost::python::indexing::slice>
145  {
146  };
147 }}}
148 
149 #endif // BOOST_PYTHON_INDEXING_SLICE_HPP
150 
151 
152 
Definition: python_CEGUI.h:11
Definition: slice.hpp:31