os/ossrv/ossrv_pub/boost_apis/boost/python/handle.hpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/ossrv_pub/boost_apis/boost/python/handle.hpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,264 @@
     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 HANDLE_DWA200269_HPP
     1.9 +# define HANDLE_DWA200269_HPP
    1.10 +
    1.11 +# include <boost/python/detail/prefix.hpp>
    1.12 +
    1.13 +# include <boost/python/cast.hpp>
    1.14 +# include <boost/python/errors.hpp>
    1.15 +# include <boost/python/borrowed.hpp>
    1.16 +# include <boost/python/handle_fwd.hpp>
    1.17 +# include <boost/python/refcount.hpp>
    1.18 +# include <boost/python/tag.hpp>
    1.19 +# include <boost/python/detail/raw_pyobject.hpp>
    1.20 +
    1.21 +namespace boost { namespace python { 
    1.22 +
    1.23 +template <class T> struct null_ok;
    1.24 +
    1.25 +template <class T>
    1.26 +inline null_ok<T>* allow_null(T* p)
    1.27 +{
    1.28 +    return (null_ok<T>*)p;
    1.29 +}
    1.30 +
    1.31 +namespace detail
    1.32 +{
    1.33 +  template <class T>
    1.34 +  inline T* manage_ptr(detail::borrowed<null_ok<T> >* p, int)
    1.35 +  {
    1.36 +      return python::xincref((T*)p);
    1.37 +  }
    1.38 +  
    1.39 +  template <class T>
    1.40 +  inline T* manage_ptr(null_ok<detail::borrowed<T> >* p, int)
    1.41 +  {
    1.42 +      return python::xincref((T*)p);
    1.43 +  }
    1.44 +  
    1.45 +  template <class T>
    1.46 +  inline T* manage_ptr(detail::borrowed<T>* p, long)
    1.47 +  {
    1.48 +      return python::incref(expect_non_null((T*)p));
    1.49 +  }
    1.50 +  
    1.51 +  template <class T>
    1.52 +  inline T* manage_ptr(null_ok<T>* p, long)
    1.53 +  {
    1.54 +      return (T*)p;
    1.55 +  }
    1.56 +  
    1.57 +  template <class T>
    1.58 +  inline T* manage_ptr(T* p, ...)
    1.59 +  {
    1.60 +      return expect_non_null(p);
    1.61 +  }
    1.62 +}
    1.63 +
    1.64 +template <class T>
    1.65 +class handle
    1.66 +{
    1.67 +    typedef T* (handle::* bool_type )() const;
    1.68 +
    1.69 + public: // types
    1.70 +    typedef T element_type;
    1.71 +    
    1.72 + public: // member functions
    1.73 +    handle();
    1.74 +    ~handle();
    1.75 +
    1.76 +    template <class Y>
    1.77 +    explicit handle(Y* p)
    1.78 +        : m_p(
    1.79 +            python::upcast<T>(
    1.80 +                detail::manage_ptr(p, 0)
    1.81 +                )
    1.82 +            )
    1.83 +    {
    1.84 +    }
    1.85 +
    1.86 +    handle& operator=(handle const& r)
    1.87 +    {
    1.88 +        python::xdecref(m_p);
    1.89 +        m_p = python::xincref(r.m_p);
    1.90 +        return *this;
    1.91 +    }
    1.92 +
    1.93 +#if !defined(BOOST_MSVC) || (BOOST_MSVC >= 1300)
    1.94 +
    1.95 +    template<typename Y>
    1.96 +    handle& operator=(handle<Y> const & r) // never throws
    1.97 +    {
    1.98 +        python::xdecref(m_p);
    1.99 +        m_p = python::xincref(python::upcast<T>(r.get()));
   1.100 +        return *this;
   1.101 +    }
   1.102 +
   1.103 +#endif
   1.104 +
   1.105 +    template <typename Y>
   1.106 +    handle(handle<Y> const& r)
   1.107 +        : m_p(python::xincref(python::upcast<T>(r.get())))
   1.108 +    {
   1.109 +    }
   1.110 +    
   1.111 +    handle(handle const& r)
   1.112 +        : m_p(python::xincref(r.m_p))
   1.113 +    {
   1.114 +    }
   1.115 +    
   1.116 +    T* operator-> () const;
   1.117 +    T& operator* () const;
   1.118 +    T* get() const;
   1.119 +    T* release();
   1.120 +    void reset();
   1.121 +    
   1.122 +    operator bool_type() const // never throws
   1.123 +    {
   1.124 +        return m_p ? &handle<T>::get : 0;
   1.125 +    }
   1.126 +    bool operator! () const; // never throws
   1.127 +
   1.128 + public: // implementation details -- do not touch
   1.129 +    // Defining this in the class body suppresses a VC7 link failure
   1.130 +    inline handle(detail::borrowed_reference x)
   1.131 +        : m_p(
   1.132 +            python::incref(
   1.133 +                downcast<T>((PyObject*)x)
   1.134 +                ))
   1.135 +    {
   1.136 +    }
   1.137 +    
   1.138 + private: // data members
   1.139 +    T* m_p;
   1.140 +};
   1.141 +
   1.142 +#ifdef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP
   1.143 +} // namespace python
   1.144 +#endif
   1.145 +
   1.146 +template<class T> inline T * get_pointer(python::handle<T> const & p)
   1.147 +{
   1.148 +    return p.get();
   1.149 +}
   1.150 +
   1.151 +#ifdef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP
   1.152 +namespace python {
   1.153 +#else
   1.154 +
   1.155 +// We don't want get_pointer above to hide the others
   1.156 +using boost::get_pointer;
   1.157 +
   1.158 +#endif
   1.159 +
   1.160 +typedef handle<PyTypeObject> type_handle;
   1.161 +
   1.162 +//
   1.163 +// Compile-time introspection
   1.164 +//
   1.165 +# ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
   1.166 +template<typename T>
   1.167 +class is_handle
   1.168 +{
   1.169 + public:
   1.170 +    BOOST_STATIC_CONSTANT(bool, value = false); 
   1.171 +};
   1.172 +
   1.173 +template<typename T>
   1.174 +class is_handle<handle<T> >
   1.175 +{
   1.176 + public:
   1.177 +    BOOST_STATIC_CONSTANT(bool, value = true);
   1.178 +};
   1.179 +# else
   1.180 +namespace detail
   1.181 +{
   1.182 +  typedef char (&yes_handle_t)[1];
   1.183 +  typedef char (&no_handle_t)[2];
   1.184 +      
   1.185 +  no_handle_t is_handle_test(...);
   1.186 +
   1.187 +  template<typename T>
   1.188 +  yes_handle_t is_handle_test(boost::type< handle<T> >);
   1.189 +}
   1.190 +
   1.191 +template<typename T>
   1.192 +class is_handle
   1.193 +{
   1.194 + public:
   1.195 +    BOOST_STATIC_CONSTANT(
   1.196 +        bool, value = (
   1.197 +            sizeof(detail::is_handle_test(boost::type<T>()))
   1.198 +            == sizeof(detail::yes_handle_t)));
   1.199 +};
   1.200 +# endif
   1.201 +
   1.202 +//
   1.203 +// implementations
   1.204 +//
   1.205 +template <class T>
   1.206 +inline handle<T>::handle()
   1.207 +    : m_p(0)
   1.208 +{
   1.209 +}
   1.210 +
   1.211 +template <class T>
   1.212 +inline handle<T>::~handle()
   1.213 +{
   1.214 +    python::xdecref(m_p);
   1.215 +}
   1.216 +
   1.217 +template <class T>
   1.218 +inline T* handle<T>::operator->() const
   1.219 +{
   1.220 +    return m_p;
   1.221 +}
   1.222 +
   1.223 +template <class T>
   1.224 +inline T& handle<T>::operator*() const
   1.225 +{
   1.226 +    return *m_p;
   1.227 +}
   1.228 +
   1.229 +template <class T>
   1.230 +inline T* handle<T>::get() const
   1.231 +{
   1.232 +    return m_p;
   1.233 +}
   1.234 +    
   1.235 +template <class T>
   1.236 +inline bool handle<T>::operator!() const
   1.237 +{
   1.238 +    return m_p == 0;
   1.239 +}
   1.240 +
   1.241 +template <class T>
   1.242 +inline T* handle<T>::release()
   1.243 +{
   1.244 +    T* result = m_p;
   1.245 +    m_p = 0;
   1.246 +    return result;
   1.247 +}
   1.248 +
   1.249 +template <class T>
   1.250 +inline void handle<T>::reset()
   1.251 +{
   1.252 +    python::xdecref(m_p);
   1.253 +    m_p = 0;
   1.254 +}
   1.255 +
   1.256 +// Because get_managed_object must return a non-null PyObject*, we
   1.257 +// return Py_None if the handle is null.
   1.258 +template <class T>
   1.259 +inline PyObject* get_managed_object(handle<T> const& h, tag_t)
   1.260 +{
   1.261 +    return h.get() ? python::upcast<PyObject>(h.get()) : Py_None;
   1.262 +}
   1.263 +
   1.264 +}} // namespace boost::python
   1.265 +
   1.266 +
   1.267 +#endif // HANDLE_DWA200269_HPP