2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-26 18:52:26 +00:00

Test that shared_ptr<Derived> can be converted to shared_ptr<Base>

[SVN r18851]
This commit is contained in:
Dave Abrahams
2003-06-20 22:57:37 +00:00
parent 67b265fe96
commit 7dba18e7b9
2 changed files with 71 additions and 55 deletions

View File

@@ -49,15 +49,36 @@ struct functions
{
return look(get());
}
static void expose()
template <class C>
static void expose(C const& c)
{
def("look", &look);
def("store", &store);
def("modify", &modify);
def("look_store", &look_store);
def("identity", &identity);
def("null", &null);
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("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; }
@@ -93,14 +114,16 @@ 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));
}
static int stored_v() { return functions<Z>::get()->v(); }
static shared_ptr<Z> stored_z() { return functions<Z>::get(); }
// regressions from Nicodemus
struct A
{
@@ -147,41 +170,30 @@ BOOST_PYTHON_MODULE(shared_ptr_ext)
>();
def("New", &New);
class_<X, boost::noncopyable>("X", init<int>())
.def("value", &X::value)
;
def("factory", factory);
functions<X>::expose();
def("x_count", &X::count);
def("x_release", &functions<X>::release_store);
def("x_look_store", &functions<X>::look_store);
functions<X>::expose(
class_<X, boost::noncopyable>("X", init<int>())
.def("value", &X::value)
);
class_<Y, boost::shared_ptr<Y> >("Y", init<int>())
.def("value", &Y::value)
;
functions<Y>::expose(
class_<Y, boost::shared_ptr<Y> >("Y", init<int>())
.def("value", &Y::value)
);
class_<YY, bases<Y>, boost::noncopyable>("YY", init<int>())
;
functions<Y>::expose();
def("y_count", &Y::count);
def("y_release", &functions<Y>::release_store);
def("y_look_store", &functions<Y>::look_store);
class_<Z, ZWrap>("Z", init<int>())
.def("value", &Z::value)
.def("v", &Z::v, &ZWrap::default_v)
class_<YYY, shared_ptr<YYY>, bases<Y> >("YYY", init<int>())
;
functions<Z>::expose();
def("z_count", &Z::count);
def("z_release", &functions<Z>::release_store);
def("z_look_store", &functions<Z>::look_store);
def("stored_z", &stored_z);
def("stored_v", &stored_v);
functions<Z>::expose(
class_<Z, ZWrap>("Z", init<int>())
.def("value", &Z::value)
.def("v", &Z::v, &ZWrap::default_v)
);
}
#include "module_tail.cpp"

View File

@@ -1,6 +1,10 @@
'''
>>> from shared_ptr_ext import *
Test that shared_ptr<Derived> can be converted to shared_ptr<Base>
>>> Y.store(YYY(42))
>>> x = X(17)
>>> null_x = null(x)
>>> null_x # should be None
@@ -36,15 +40,15 @@
-1
>>> store(p)
>>> del p
>>> stored_v()
>>> Z.get().v()
-12
>>> z_count()
>>> Z.count()
1
>>> z_look_store()
>>> Z.look_store()
12
>>> z_release()
>>> Z.release()
bye
>>> z_count()
>>> Z.count()
0
>>> z = Z(13)
@@ -56,18 +60,18 @@ bye
... except TypeError: pass
... else: 'print expected a TypeError'
>>> stored_z() # should be None
>>> Z.get() # should be None
>>> store(z)
>>> assert stored_z() is z # show that deleter introspection works
>>> assert Z.get() is z # show that deleter introspection works
>>> del z
>>> stored_v()
>>> Z.get().value()
13
>>> z_count()
>>> Z.count()
1
>>> z_look_store()
>>> Z.look_store()
13
>>> z_release()
>>> z_count()
>>> Z.release()
>>> Z.count()
0
>>> x = X(17)
@@ -82,12 +86,12 @@ bye
-1
>>> store(x)
>>> del x
>>> x_count()
>>> X.count()
1
>>> x_look_store()
>>> X.look_store()
17
>>> x_release()
>>> x_count()
>>> X.release()
>>> X.count()
0
@@ -98,12 +102,12 @@ bye
>>> look(y)
-1
>>> store(Y(23))
>>> y_count()
>>> Y.count()
1
>>> y_look_store()
>>> Y.look_store()
23
>>> y_release()
>>> y_count()
>>> Y.release()
>>> Y.count()
0
'''