the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
1// Copyright David Abrahams 2002.
2// Distributed under the Boost Software License, Version 1.0. (See
3// accompanying file LICENSE_1_0.txt or copy at
4// http://www.boost.org/LICENSE_1_0.txt)
5#ifndef EXTRACT_DWA200265_HPP
6# define EXTRACT_DWA200265_HPP
7
8# include <boost/python/detail/prefix.hpp>
9
10# include <boost/python/converter/object_manager.hpp>
11# include <boost/python/converter/from_python.hpp>
12# include <boost/python/converter/rvalue_from_python_data.hpp>
13# include <boost/python/converter/registered.hpp>
14# include <boost/python/converter/registered_pointee.hpp>
15
16# include <boost/python/object_core.hpp>
17# include <boost/python/refcount.hpp>
18
19# include <boost/python/detail/copy_ctor_mutates_rhs.hpp>
20# include <boost/python/detail/void_ptr.hpp>
21# include <boost/python/detail/void_return.hpp>
22# include <boost/call_traits.hpp>
23
24#if BOOST_WORKAROUND(BOOST_MSVC, <= 1300) || BOOST_WORKAROUND(BOOST_INTEL_WIN, <= 900)
25// workaround for VC++ 6.x or 7.0
26# define BOOST_EXTRACT_WORKAROUND ()
27#else
28# define BOOST_EXTRACT_WORKAROUND
29#endif
30
31namespace boost { namespace python {
32
33namespace api
34{
35 class object;
36}
37
38namespace converter
39{
40 template <class Ptr>
41 struct extract_pointer
42 {
43 typedef Ptr result_type;
44 extract_pointer(PyObject*);
45
46 bool check() const;
47 Ptr operator()() const;
48
49 private:
50 PyObject* m_source;
51 void* m_result;
52 };
53
54 template <class Ref>
55 struct extract_reference
56 {
57 typedef Ref result_type;
58 extract_reference(PyObject*);
59
60 bool check() const;
61 Ref operator()() const;
62
63 private:
64 PyObject* m_source;
65 void* m_result;
66 };
67
68 template <class T>
69 struct extract_rvalue : private noncopyable
70 {
71 typedef typename mpl::if_<
72 python::detail::copy_ctor_mutates_rhs<T>
73 , T&
74 , typename call_traits<T>::param_type
75 >::type result_type;
76
77 extract_rvalue(PyObject*);
78
79 bool check() const;
80 result_type operator()() const;
81 private:
82 PyObject* m_source;
83 mutable rvalue_from_python_data<T> m_data;
84 };
85
86 template <class T>
87 struct extract_object_manager
88 {
89 typedef T result_type;
90 extract_object_manager(PyObject*);
91
92 bool check() const;
93 result_type operator()() const;
94 private:
95 PyObject* m_source;
96 };
97
98 template <class T>
99 struct select_extract
100 {
101 BOOST_STATIC_CONSTANT(
102 bool, obj_mgr = is_object_manager<T>::value);
103
104 BOOST_STATIC_CONSTANT(
105 bool, ptr = is_pointer<T>::value);
106
107 BOOST_STATIC_CONSTANT(
108 bool, ref = is_reference<T>::value);
109
110 typedef typename mpl::if_c<
111 obj_mgr
112 , extract_object_manager<T>
113 , typename mpl::if_c<
114 ptr
115 , extract_pointer<T>
116 , typename mpl::if_c<
117 ref
118 , extract_reference<T>
119 , extract_rvalue<T>
120 >::type
121 >::type
122 >::type type;
123 };
124}
125
126template <class T>
127struct extract
128 : converter::select_extract<T>::type
129{
130 private:
131 typedef typename converter::select_extract<T>::type base;
132 public:
133 typedef typename base::result_type result_type;
134
135 operator result_type() const
136 {
137 return (*this)();
138 }
139
140 extract(PyObject*);
141 extract(api::object const&);
142};
143
144//
145// Implementations
146//
147template <class T>
148inline extract<T>::extract(PyObject* o)
149 : base(o)
150{
151}
152
153template <class T>
154inline extract<T>::extract(api::object const& o)
155 : base(o.ptr())
156{
157}
158
159namespace converter
160{
161 template <class T>
162 inline extract_rvalue<T>::extract_rvalue(PyObject* x)
163 : m_source(x)
164 , m_data(
165 (rvalue_from_python_stage1)(x, registered<T>::converters)
166 )
167 {
168 }
169
170 template <class T>
171 inline bool
172 extract_rvalue<T>::check() const
173 {
174 return m_data.stage1.convertible;
175 }
176
177 template <class T>
178 inline typename extract_rvalue<T>::result_type
179 extract_rvalue<T>::operator()() const
180 {
181 return *(T*)(
182 // Only do the stage2 conversion once
183 m_data.stage1.convertible == m_data.storage.bytes
184 ? m_data.storage.bytes
185 : (rvalue_from_python_stage2)(m_source, m_data.stage1, registered<T>::converters)
186 );
187 }
188
189 template <class Ref>
190 inline extract_reference<Ref>::extract_reference(PyObject* obj)
191 : m_source(obj)
192 , m_result(
193 (get_lvalue_from_python)(obj, registered<Ref>::converters)
194 )
195 {
196 }
197
198 template <class Ref>
199 inline bool extract_reference<Ref>::check() const
200 {
201 return m_result != 0;
202 }
203
204 template <class Ref>
205 inline Ref extract_reference<Ref>::operator()() const
206 {
207 if (m_result == 0)
208 (throw_no_reference_from_python)(m_source, registered<Ref>::converters);
209
210 return python::detail::void_ptr_to_reference(m_result, (Ref(*)())0);
211 }
212
213 template <class Ptr>
214 inline extract_pointer<Ptr>::extract_pointer(PyObject* obj)
215 : m_source(obj)
216 , m_result(
217 obj == Py_None ? 0 : (get_lvalue_from_python)(obj, registered_pointee<Ptr>::converters)
218 )
219 {
220 }
221
222 template <class Ptr>
223 inline bool extract_pointer<Ptr>::check() const
224 {
225 return m_source == Py_None || m_result != 0;
226 }
227
228 template <class Ptr>
229 inline Ptr extract_pointer<Ptr>::operator()() const
230 {
231 if (m_result == 0 && m_source != Py_None)
232 (throw_no_pointer_from_python)(m_source, registered_pointee<Ptr>::converters);
233
234 return Ptr(m_result);
235 }
236
237 template <class T>
238 inline extract_object_manager<T>::extract_object_manager(PyObject* obj)
239 : m_source(obj)
240 {
241 }
242
243 template <class T>
244 inline bool extract_object_manager<T>::check() const
245 {
246 return object_manager_traits<T>::check(m_source);
247 }
248
249 template <class T>
250 inline T extract_object_manager<T>::operator()() const
251 {
252 return T(
253 object_manager_traits<T>::adopt(python::incref(m_source))
254 );
255 }
256}
257
258}} // namespace boost::python::converter
259
260#endif // EXTRACT_DWA200265_HPP