1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/ossrv_pub/boost_apis/boost/python/extract.hpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,261 @@
1.4 +// Copyright David Abrahams 2002.
1.5 +// Distributed under the Boost Software License, Version 1.0. (See
1.6 +// accompanying file LICENSE_1_0.txt or copy at
1.7 +// http://www.boost.org/LICENSE_1_0.txt)
1.8 +#ifndef EXTRACT_DWA200265_HPP
1.9 +# define EXTRACT_DWA200265_HPP
1.10 +
1.11 +# include <boost/python/detail/prefix.hpp>
1.12 +
1.13 +# include <boost/python/converter/object_manager.hpp>
1.14 +# include <boost/python/converter/from_python.hpp>
1.15 +# include <boost/python/converter/rvalue_from_python_data.hpp>
1.16 +# include <boost/python/converter/registered.hpp>
1.17 +# include <boost/python/converter/registered_pointee.hpp>
1.18 +
1.19 +# include <boost/python/object_core.hpp>
1.20 +# include <boost/python/refcount.hpp>
1.21 +
1.22 +# include <boost/python/detail/copy_ctor_mutates_rhs.hpp>
1.23 +# include <boost/python/detail/void_ptr.hpp>
1.24 +# include <boost/python/detail/void_return.hpp>
1.25 +# include <boost/utility.hpp>
1.26 +# include <boost/call_traits.hpp>
1.27 +
1.28 +#if BOOST_WORKAROUND(BOOST_MSVC, <= 1300) || BOOST_WORKAROUND(BOOST_INTEL_WIN, <= 900)
1.29 +// workaround for VC++ 6.x or 7.0
1.30 +# define BOOST_EXTRACT_WORKAROUND ()
1.31 +#else
1.32 +# define BOOST_EXTRACT_WORKAROUND
1.33 +#endif
1.34 +
1.35 +namespace boost { namespace python {
1.36 +
1.37 +namespace api
1.38 +{
1.39 + class object;
1.40 +}
1.41 +
1.42 +namespace converter
1.43 +{
1.44 + template <class Ptr>
1.45 + struct extract_pointer
1.46 + {
1.47 + typedef Ptr result_type;
1.48 + extract_pointer(PyObject*);
1.49 +
1.50 + bool check() const;
1.51 + Ptr operator()() const;
1.52 +
1.53 + private:
1.54 + PyObject* m_source;
1.55 + void* m_result;
1.56 + };
1.57 +
1.58 + template <class Ref>
1.59 + struct extract_reference
1.60 + {
1.61 + typedef Ref result_type;
1.62 + extract_reference(PyObject*);
1.63 +
1.64 + bool check() const;
1.65 + Ref operator()() const;
1.66 +
1.67 + private:
1.68 + PyObject* m_source;
1.69 + void* m_result;
1.70 + };
1.71 +
1.72 + template <class T>
1.73 + struct extract_rvalue : private noncopyable
1.74 + {
1.75 + typedef typename mpl::if_<
1.76 + python::detail::copy_ctor_mutates_rhs<T>
1.77 + , T&
1.78 + , typename call_traits<T>::param_type
1.79 + >::type result_type;
1.80 +
1.81 + extract_rvalue(PyObject*);
1.82 +
1.83 + bool check() const;
1.84 + result_type operator()() const;
1.85 + private:
1.86 + PyObject* m_source;
1.87 + mutable rvalue_from_python_data<T> m_data;
1.88 + };
1.89 +
1.90 + template <class T>
1.91 + struct extract_object_manager
1.92 + {
1.93 + typedef T result_type;
1.94 + extract_object_manager(PyObject*);
1.95 +
1.96 + bool check() const;
1.97 + result_type operator()() const;
1.98 + private:
1.99 + PyObject* m_source;
1.100 + };
1.101 +
1.102 + template <class T>
1.103 + struct select_extract
1.104 + {
1.105 + BOOST_STATIC_CONSTANT(
1.106 + bool, obj_mgr = is_object_manager<T>::value);
1.107 +
1.108 + BOOST_STATIC_CONSTANT(
1.109 + bool, ptr = is_pointer<T>::value);
1.110 +
1.111 + BOOST_STATIC_CONSTANT(
1.112 + bool, ref = is_reference<T>::value);
1.113 +
1.114 + typedef typename mpl::if_c<
1.115 + obj_mgr
1.116 + , extract_object_manager<T>
1.117 + , typename mpl::if_c<
1.118 + ptr
1.119 + , extract_pointer<T>
1.120 + , typename mpl::if_c<
1.121 + ref
1.122 + , extract_reference<T>
1.123 + , extract_rvalue<T>
1.124 + >::type
1.125 + >::type
1.126 + >::type type;
1.127 + };
1.128 +}
1.129 +
1.130 +template <class T>
1.131 +struct extract
1.132 + : converter::select_extract<T>::type
1.133 +{
1.134 + private:
1.135 + typedef typename converter::select_extract<T>::type base;
1.136 + public:
1.137 + typedef typename base::result_type result_type;
1.138 +
1.139 + operator result_type() const
1.140 + {
1.141 + return (*this)();
1.142 + }
1.143 +
1.144 + extract(PyObject*);
1.145 + extract(api::object const&);
1.146 +};
1.147 +
1.148 +//
1.149 +// Implementations
1.150 +//
1.151 +template <class T>
1.152 +inline extract<T>::extract(PyObject* o)
1.153 + : base(o)
1.154 +{
1.155 +}
1.156 +
1.157 +template <class T>
1.158 +inline extract<T>::extract(api::object const& o)
1.159 + : base(o.ptr())
1.160 +{
1.161 +}
1.162 +
1.163 +namespace converter
1.164 +{
1.165 + template <class T>
1.166 + inline extract_rvalue<T>::extract_rvalue(PyObject* x)
1.167 + : m_source(x)
1.168 + , m_data(
1.169 + (rvalue_from_python_stage1)(x, registered<T>::converters)
1.170 + )
1.171 + {
1.172 + }
1.173 +
1.174 + template <class T>
1.175 + inline bool
1.176 + extract_rvalue<T>::check() const
1.177 + {
1.178 + return m_data.stage1.convertible;
1.179 + }
1.180 +
1.181 + template <class T>
1.182 + inline typename extract_rvalue<T>::result_type
1.183 + extract_rvalue<T>::operator()() const
1.184 + {
1.185 + return *(T*)(
1.186 + // Only do the stage2 conversion once
1.187 + m_data.stage1.convertible == m_data.storage.bytes
1.188 + ? m_data.storage.bytes
1.189 + : (rvalue_from_python_stage2)(m_source, m_data.stage1, registered<T>::converters)
1.190 + );
1.191 + }
1.192 +
1.193 + template <class Ref>
1.194 + inline extract_reference<Ref>::extract_reference(PyObject* obj)
1.195 + : m_source(obj)
1.196 + , m_result(
1.197 + (get_lvalue_from_python)(obj, registered<Ref>::converters)
1.198 + )
1.199 + {
1.200 + }
1.201 +
1.202 + template <class Ref>
1.203 + inline bool extract_reference<Ref>::check() const
1.204 + {
1.205 + return m_result != 0;
1.206 + }
1.207 +
1.208 + template <class Ref>
1.209 + inline Ref extract_reference<Ref>::operator()() const
1.210 + {
1.211 + if (m_result == 0)
1.212 + (throw_no_reference_from_python)(m_source, registered<Ref>::converters);
1.213 +
1.214 + return python::detail::void_ptr_to_reference(m_result, (Ref(*)())0);
1.215 + }
1.216 +
1.217 + template <class Ptr>
1.218 + inline extract_pointer<Ptr>::extract_pointer(PyObject* obj)
1.219 + : m_source(obj)
1.220 + , m_result(
1.221 + obj == Py_None ? 0 : (get_lvalue_from_python)(obj, registered_pointee<Ptr>::converters)
1.222 + )
1.223 + {
1.224 + }
1.225 +
1.226 + template <class Ptr>
1.227 + inline bool extract_pointer<Ptr>::check() const
1.228 + {
1.229 + return m_source == Py_None || m_result != 0;
1.230 + }
1.231 +
1.232 + template <class Ptr>
1.233 + inline Ptr extract_pointer<Ptr>::operator()() const
1.234 + {
1.235 + if (m_result == 0 && m_source != Py_None)
1.236 + (throw_no_pointer_from_python)(m_source, registered_pointee<Ptr>::converters);
1.237 +
1.238 + return Ptr(m_result);
1.239 + }
1.240 +
1.241 + template <class T>
1.242 + inline extract_object_manager<T>::extract_object_manager(PyObject* obj)
1.243 + : m_source(obj)
1.244 + {
1.245 + }
1.246 +
1.247 + template <class T>
1.248 + inline bool extract_object_manager<T>::check() const
1.249 + {
1.250 + return object_manager_traits<T>::check(m_source);
1.251 + }
1.252 +
1.253 + template <class T>
1.254 + inline T extract_object_manager<T>::operator()() const
1.255 + {
1.256 + return T(
1.257 + object_manager_traits<T>::adopt(python::incref(m_source))
1.258 + );
1.259 + }
1.260 +}
1.261 +
1.262 +}} // namespace boost::python::converter
1.263 +
1.264 +#endif // EXTRACT_DWA200265_HPP