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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
|
// Copyright David Abrahams 2002.
// Copyright Stefan Seefeld 2016.
// 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 "test_class.hpp"
using namespace boost::python;
typedef test_class<> X;
typedef test_class<1> Y;
template <class T>
struct functions
{
static int look(shared_ptr<T> const& x)
{
return (x.get()) ? x->value() : -1;
}
static void store(shared_ptr<T> x)
{
storage = x;
}
static void release_store()
{
store(shared_ptr<T>());
}
static void modify(shared_ptr<T>& x)
{
x.reset();
}
static shared_ptr<T> get() { return storage; }
static shared_ptr<T> &get1() { return storage; }
static int look_store()
{
return look(get());
}
template <class C>
static void expose(C const& c)
{
def("look", &look);
def("store", &store);
def("modify", &modify);
def("identity", &identity);
def("null", &null);
const_cast<C&>(c)
.def("look", &look)
.staticmethod("look")
.def("store", &store)
.staticmethod("store")
.def("modify", &modify)
.staticmethod("modify")
.def("look_store", &look_store)
.staticmethod("look_store")
.def("identity", &identity)
.staticmethod("identity")
.def("null", &null)
.staticmethod("null")
.def("get1", &get1, return_internal_reference<>())
.staticmethod("get1")
.def("get", &get)
.staticmethod("get")
.def("count", &T::count)
.staticmethod("count")
.def("release", &release_store)
.staticmethod("release")
;
}
static shared_ptr<T> identity(shared_ptr<T> x) { return x; }
static shared_ptr<T> null(T const&) { return shared_ptr<T>(); }
static shared_ptr<T> storage;
};
template <class T> shared_ptr<T> functions<T>::storage;
struct Z : test_class<2>
{
Z(int x) : test_class<2>(x) {}
virtual int v() { return this->value(); }
};
struct ZWrap : Z
{
ZWrap(PyObject* self, int x)
: Z(x), m_self(self) {}
virtual int v() { return call_method<int>(m_self, "v"); }
int default_v() { return Z::v(); }
PyObject* m_self;
};
struct YY : Y
{
YY(int n) : Y(n) {}
};
struct YYY : Y
{
YYY(int n) : Y(n) {}
};
shared_ptr<Y> factory(int n)
{
return shared_ptr<Y>(n < 42 ? new Y(n) : new YY(n));
}
// regressions from Nicodemus
struct A
{
virtual ~A() {}; // silence compiler warnings
virtual int f() = 0;
static int call_f(shared_ptr<A>& a) { return a->f(); }
};
struct B: A
{
int f() { return 1; }
};
shared_ptr<A> New(bool make)
{
return shared_ptr<A>( make ? new B() : 0 );
}
struct A_Wrapper: A
{
A_Wrapper(PyObject* self_):
A(), self(self_) {}
int f() {
return call_method< int >(self, "f");
}
PyObject* self;
};
// ------
// from Neal Becker
struct Test {
shared_ptr<X> x;
};
// ------
BOOST_PYTHON_MODULE(MODULE)
{
class_<A, shared_ptr<A_Wrapper>, boost::noncopyable>("A")
.def("call_f", &A::call_f)
.staticmethod("call_f")
;
// This is the ugliness required to register a to-python converter
// for shared_ptr<A>.
objects::class_value_wrapper<
shared_ptr<A>
, objects::make_ptr_instance<A, objects::pointer_holder<shared_ptr<A>,A> >
>();
def("New", &New);
def("factory", factory);
functions<X>::expose(
class_<X, boost::noncopyable>("X", init<int>())
.def("value", &X::value)
);
functions<Y>::expose(
class_<Y, shared_ptr<Y> >("Y", init<int>())
.def("value", &Y::value)
);
class_<YY, bases<Y>, boost::noncopyable>("YY", init<int>())
;
class_<YYY, shared_ptr<YYY>, bases<Y> >("YYY", init<int>())
;
functions<Z>::expose(
class_<Z, ZWrap>("Z", init<int>())
.def("value", &Z::value)
.def("v", &Z::v, &ZWrap::default_v)
);
// from Neal Becker
class_<Test> ("Test")
.def_readonly ("x", &Test::x, "x")
;
// ------
}
|