summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/python/test/wrapper_held_type.cpp
blob: e99422796e87b91ad5f8eb9ce370c9e8865b20dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright David Abrahams 2005. Distributed under the Boost
// Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#include <boost/python/register_ptr_to_python.hpp>
#include <boost/python/def.hpp>
#include <boost/python/class.hpp>
#include <boost/python/wrapper.hpp>
#include <boost/python/module.hpp>
#include <boost/python/implicit.hpp>

#include <memory>

struct data
{
    virtual ~data() {}; // silence compiler warnings
    virtual int id() const
    {
        return 42;
    }
};
    
std::auto_ptr<data> create_data()
{ 
    return std::auto_ptr<data>( new data ); 
}

void do_nothing( std::auto_ptr<data>& ){}

    
namespace bp = boost::python;

struct data_wrapper : data, bp::wrapper< data >
{
    data_wrapper(data const & arg )
    : data( arg )
      , bp::wrapper< data >()
    {}

    data_wrapper()
    : data()
      , bp::wrapper< data >()
    {}

    virtual int id() const
    {
        if( bp::override id = this->get_override( "id" ) )
            return bp::call<int>(id.ptr()); // id();
        else
            return data::id(  );
    }
    
    virtual int default_id(  ) const
    {
        return this->data::id( );
    }

};

BOOST_PYTHON_MODULE(wrapper_held_type_ext)
{
    bp::class_< data_wrapper, std::auto_ptr< data > >( "data" )    
        .def( "id", &data::id, &::data_wrapper::default_id );

    bp::def( "do_nothing", &do_nothing );
    bp::def( "create_data", &create_data );
}    

#include "module_tail.cpp"