~twpol/dcplusplus/trunk

« back to all changes in this revision

Viewing changes to boost/boost/python/object/make_ptr_instance.hpp

  • Committer: James Ross
  • Date: 2010-07-05 00:03:18 UTC
  • mfrom: (1524.1.650 dcplusplus)
  • Revision ID: silver@warwickcompsoc.co.uk-20100705000318-awwqm8ocpp5m47yz
Merged to trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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 MAKE_PTR_INSTANCE_DWA200296_HPP
6
 
# define MAKE_PTR_INSTANCE_DWA200296_HPP
7
 
 
8
 
# include <boost/python/object/make_instance.hpp>
9
 
# include <boost/python/converter/registry.hpp>
10
 
# include <boost/type_traits/is_polymorphic.hpp>
11
 
# include <boost/get_pointer.hpp>
12
 
# include <boost/detail/workaround.hpp>
13
 
# include <typeinfo>
14
 
 
15
 
namespace boost { namespace python { namespace objects { 
16
 
 
17
 
template <class T, class Holder>
18
 
struct make_ptr_instance
19
 
    : make_instance_impl<T, Holder, make_ptr_instance<T,Holder> >
20
 
{
21
 
    template <class Arg>
22
 
    static inline Holder* construct(void* storage, PyObject*, Arg& x)
23
 
    {
24
 
        return new (storage) Holder(x);
25
 
    }
26
 
    
27
 
    template <class Ptr>
28
 
    static inline PyTypeObject* get_class_object(Ptr const& x)
29
 
    {
30
 
        return get_class_object_impl(get_pointer(x));
31
 
    }
32
 
#ifndef BOOST_PYTHON_NO_PY_SIGNATURES
33
 
    static inline PyTypeObject const* get_pytype()
34
 
    {
35
 
        return converter::registered<T>::converters.get_class_object();
36
 
    }
37
 
#endif
38
 
 private:
39
 
    template <class U>
40
 
    static inline PyTypeObject* get_class_object_impl(U const volatile* p)
41
 
    {
42
 
        if (p == 0)
43
 
            return 0; // means "return None".
44
 
 
45
 
        PyTypeObject* derived = get_derived_class_object(
46
 
            BOOST_DEDUCED_TYPENAME is_polymorphic<U>::type(), p);
47
 
        
48
 
        if (derived)
49
 
            return derived;
50
 
        return converter::registered<T>::converters.get_class_object();
51
 
    }
52
 
    
53
 
    template <class U>
54
 
    static inline PyTypeObject* get_derived_class_object(mpl::true_, U const volatile* x)
55
 
    {
56
 
        converter::registration const* r = converter::registry::query(
57
 
            type_info(typeid(*get_pointer(x)))
58
 
        );
59
 
        return r ? r->m_class_object : 0;
60
 
    }
61
 
    
62
 
    template <class U>
63
 
    static inline PyTypeObject* get_derived_class_object(mpl::false_, U*)
64
 
    {
65
 
        return 0;
66
 
    }
67
 
};
68
 
  
69
 
 
70
 
}}} // namespace boost::python::object
71
 
 
72
 
#endif // MAKE_PTR_INSTANCE_DWA200296_HPP
 
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 MAKE_PTR_INSTANCE_DWA200296_HPP
 
6
# define MAKE_PTR_INSTANCE_DWA200296_HPP
 
7
 
 
8
# include <boost/python/object/make_instance.hpp>
 
9
# include <boost/python/converter/registry.hpp>
 
10
# include <boost/type_traits/is_polymorphic.hpp>
 
11
# include <boost/get_pointer.hpp>
 
12
# include <boost/detail/workaround.hpp>
 
13
# include <typeinfo>
 
14
 
 
15
namespace boost { namespace python { namespace objects { 
 
16
 
 
17
template <class T, class Holder>
 
18
struct make_ptr_instance
 
19
    : make_instance_impl<T, Holder, make_ptr_instance<T,Holder> >
 
20
{
 
21
    template <class Arg>
 
22
    static inline Holder* construct(void* storage, PyObject*, Arg& x)
 
23
    {
 
24
        return new (storage) Holder(x);
 
25
    }
 
26
    
 
27
    template <class Ptr>
 
28
    static inline PyTypeObject* get_class_object(Ptr const& x)
 
29
    {
 
30
        return get_class_object_impl(get_pointer(x));
 
31
    }
 
32
#ifndef BOOST_PYTHON_NO_PY_SIGNATURES
 
33
    static inline PyTypeObject const* get_pytype()
 
34
    {
 
35
        return converter::registered<T>::converters.get_class_object();
 
36
    }
 
37
#endif
 
38
 private:
 
39
    template <class U>
 
40
    static inline PyTypeObject* get_class_object_impl(U const volatile* p)
 
41
    {
 
42
        if (p == 0)
 
43
            return 0; // means "return None".
 
44
 
 
45
        PyTypeObject* derived = get_derived_class_object(
 
46
            BOOST_DEDUCED_TYPENAME is_polymorphic<U>::type(), p);
 
47
        
 
48
        if (derived)
 
49
            return derived;
 
50
        return converter::registered<T>::converters.get_class_object();
 
51
    }
 
52
    
 
53
    template <class U>
 
54
    static inline PyTypeObject* get_derived_class_object(mpl::true_, U const volatile* x)
 
55
    {
 
56
        converter::registration const* r = converter::registry::query(
 
57
            type_info(typeid(*get_pointer(x)))
 
58
        );
 
59
        return r ? r->m_class_object : 0;
 
60
    }
 
61
    
 
62
    template <class U>
 
63
    static inline PyTypeObject* get_derived_class_object(mpl::false_, U*)
 
64
    {
 
65
        return 0;
 
66
    }
 
67
};
 
68
  
 
69
 
 
70
}}} // namespace boost::python::object
 
71
 
 
72
#endif // MAKE_PTR_INSTANCE_DWA200296_HPP