Crazy Eddie's GUI System  ${CEGUI_VERSION}
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
visitor.hpp
1 // This file has been generated by Py++.
2 
3 // Copyright (c) 2003 Raoul M. Gough
4 //
5 // Use, modification and distribution is subject to the Boost Software
6 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy
7 // at http://www.boost.org/LICENSE_1_0.txt)
8 //
9 // Header file visitor.hpp:
10 //
11 // def_visitor implementation to install the container_suite's Python
12 // methods in an object of a boost::python::class_<> instance.
13 //
14 // History
15 // =======
16 // 2003/ 9/11 rmg File creation from container_suite.hpp
17 // 2008/12/08 Roman Change indexing suite layout
18 //
19 // $Id: visitor.hpp,v 1.1.2.16 2004/02/08 18:57:42 raoulgough Exp $
20 //
21 
22 #ifndef BOOST_PYTHON_INDEXING_VISITOR_HPP
23 #define BOOST_PYTHON_INDEXING_VISITOR_HPP
24 
25 #include <indexing_suite/slice_handler.hpp>
26 #include <indexing_suite/suite_utils.hpp> // Get index_style_t
27 
28 #include <boost/python/def_visitor.hpp>
29 #include <boost/python/iterator.hpp>
30 #include <boost/python/default_call_policies.hpp>
31 #include <boost/type_traits/ice.hpp>
32 #include <boost/bind.hpp>
33 #include <functional>
34 
35 namespace boost { namespace python { namespace indexing {
37  // Policy override template that masks everything except the precall
38  // functions. i.e. it uses default policies for everything except
39  // precall, which must be provided by the template argument.
41 
42  namespace detail {
43  template<typename PrecallPolicy>
44  struct precall_only : public boost::python::default_call_policies
45  {
46  precall_only () : m_precall () { }
47  explicit precall_only (PrecallPolicy const &copy) : m_precall (copy) { }
48 
49  bool precall (PyObject *args) { return m_precall.precall (args); }
50  bool precall (PyObject *args) const { return m_precall.precall (args); }
51 
52  private:
53  PrecallPolicy m_precall;
54  };
55  }
56 
58  // Ugly macro to define a template that optionally adds a method to
59  // a Python class. This version (OPTIONAL_ALGO_SUPPORT) works with
60  // static functions in an Algorithms class.
61  //
62  // This macro is #undef'd at the end of this header
64 
65 #define OPTIONAL_ALGO_SUPPORT(ADDER_NAME, METHOD_NAME, ALGO_FN) template<bool doit> struct ADDER_NAME { template<class PythonClass, class Algorithms, class Policy> static void apply (PythonClass &, Algorithms const &, Policy const &) { } }; template<> struct ADDER_NAME<true> { template<class PythonClass, class Algorithms, class Policy> static void apply( PythonClass &pyClass, Algorithms const &, Policy const &policy) { pyClass.def (METHOD_NAME, &Algorithms::ALGO_FN, policy); } }
66 
68  // Ugly macro to define a template that optionally adds a method to
69  // a Python class. This version (OPTIONAL_SLICE_SUPPORT) works with
70  // static functions in the slice_handler template.
71  //
72  // This macro is #undef'd at the end of this header
74 
75 #define OPTIONAL_SLICE_SUPPORT(ADDER_NAME, METHOD_NAME, SLICE_HANDLER_FN) template<bool doit> struct ADDER_NAME { template<class PythonClass, class Algorithms, class Policy> static void apply (PythonClass &, Algorithms const &, Policy const &) { } }; template<> struct ADDER_NAME<true> { template<class PythonClass, class Algorithms, class Policy> static void apply( PythonClass &pyClass, Algorithms const &, Policy const &policy) { pyClass.def ( METHOD_NAME, slice_handler<Algorithms, Policy>::SLICE_HANDLER_FN (policy)); } }
76 
78  // __iter__ is a special case not handled by the above macros. First
79  // the unspecialized (do-nothing) version
81 
82  template<bool doit>
83  struct maybe_add_iter {
84  template<class PythonClass, class Algorithms, class Policy>
85  static void apply (PythonClass &, Algorithms const &, Policy const &) { }
86  };
87 
89  // Specialization with the real implementation of __iter__
91 
92  template<>
93  struct maybe_add_iter<true> {
94  template<class PythonClass, class Algorithms, class Policy>
95  static void apply(
96  PythonClass &pyClass,
97  Algorithms const &,
98  Policy const &)
99  {
100  // Should maybe separate precall and postcall portions of the
101  // policy (precall when generating the range object, postcall
102  // when returing from range.next())?
103 
104  pyClass.def(
105  "__iter__",
106  boost::python::range<Policy>(
107  Algorithms::begin,
108  Algorithms::end));
109  }
110  };
111 
113  // All other optional methods are covered by the two OPTIONAL_*
114  // macros
116 
117  OPTIONAL_ALGO_SUPPORT (maybe_add_len, "__len__", size);
118  OPTIONAL_ALGO_SUPPORT (maybe_add_getitem, "__getitem__", get);
119  OPTIONAL_ALGO_SUPPORT (maybe_add_setitem, "__setitem__", assign);
120  OPTIONAL_ALGO_SUPPORT (maybe_add_delitem, "__delitem__", erase_one);
121  OPTIONAL_SLICE_SUPPORT (maybe_add_getslice, "__getitem__", make_getitem);
122  OPTIONAL_SLICE_SUPPORT (maybe_add_setslice, "__setitem__", make_setitem);
123  OPTIONAL_SLICE_SUPPORT (maybe_add_delslice, "__delitem__", make_delitem);
124  OPTIONAL_ALGO_SUPPORT (maybe_add_sort, "sort", sort);
125  OPTIONAL_ALGO_SUPPORT (maybe_add_reverse, "reverse", reverse);
126  OPTIONAL_ALGO_SUPPORT (maybe_add_append, "append", push_back);
127  OPTIONAL_ALGO_SUPPORT (maybe_add_insert, "insert", insert);
128  OPTIONAL_SLICE_SUPPORT (maybe_add_extend, "extend", make_extend);
129  OPTIONAL_ALGO_SUPPORT (maybe_add_index, "index", get_index);
130  OPTIONAL_ALGO_SUPPORT (maybe_add_count, "count", count);
131  OPTIONAL_ALGO_SUPPORT (maybe_add_contains, "__contains__", contains);
132  OPTIONAL_ALGO_SUPPORT (maybe_add_has_key, "has_key", contains);
133 
135  // Do-all visitor
137 
138  template<class Algorithms, class Policy, method_set_type MethodMask>
139  class visitor
140  : public def_visitor< visitor< Algorithms, Policy, MethodMask > >
141  {
142  Policy m_policy;
143 
144  BOOST_STATIC_CONSTANT (
145  method_set_type,
146  enabled_methods = Algorithms::supported_methods & MethodMask);
147 
148  public:
149  typedef Algorithms algorithms_type;
150 
151  explicit visitor (Policy const &policy = Policy()) : m_policy (policy) { }
152 
153  public:
154  template <class PythonClass>
155  void visit (PythonClass &pyClass) const
156  {
157  detail::precall_only<Policy> precallPolicy (m_policy);
158 
159  maybe_add_len<detail::is_member<enabled_methods, method_len>::value>
160  ::apply(pyClass, algorithms_type(), precallPolicy);
161 
162  maybe_add_getitem<
164  >::apply(pyClass, algorithms_type(), m_policy);
165 
166  maybe_add_getslice<
168  >::apply(pyClass, algorithms_type(), m_policy);
169 
170  maybe_add_setitem<
172  >::apply(pyClass, algorithms_type(), m_policy);
173 
174  maybe_add_setslice<
176  >::apply(pyClass, algorithms_type(), m_policy);
177 
178  maybe_add_delitem<
180  >::apply(pyClass, algorithms_type(), m_policy);
181 
182  maybe_add_delslice<
184  >::apply(pyClass, algorithms_type(), m_policy);
185 
188  >::apply (pyClass, algorithms_type(), m_policy);
189 
190  maybe_add_sort<
192  >::apply (pyClass, algorithms_type(), precallPolicy);
193 
194  maybe_add_reverse<
196  >::apply (pyClass, algorithms_type(), precallPolicy);
197 
198  maybe_add_append<
200  >::apply (pyClass, algorithms_type(), precallPolicy);
201 
202  maybe_add_insert<
204  >::apply (pyClass, algorithms_type(), precallPolicy);
205 
206  maybe_add_extend<
208  >::apply (pyClass, algorithms_type(), precallPolicy);
209 
210  maybe_add_index<
212  >::apply (pyClass, algorithms_type(), precallPolicy);
213 
214  maybe_add_count<
216  >::apply (pyClass, algorithms_type(), precallPolicy);
217 
218  maybe_add_contains<
220  >::apply (pyClass, algorithms_type(), precallPolicy);
221 
222  maybe_add_has_key<
224  >::apply (pyClass, algorithms_type(), precallPolicy);
225 
226  Algorithms::visit_container_class (pyClass, m_policy);
227  }
228  };
229 } } }
230 
231 #undef OPTIONAL_SLICE_SUPPORT
232 #undef OPTIONAL_ALGO_SUPPORT
233 
234 #endif // BOOST_PYTHON_INDEXING_VISITOR_HPP
235 
236 
237 
Definition: visitor.hpp:139
Definition: python_CEGUI.h:11
Definition: visitor.hpp:83