2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-22 05:22:45 +00:00

Add proof-of-concept for one technique of wrapping function that return a

pointer


[SVN r8112]
This commit is contained in:
Dave Abrahams
2000-11-03 16:06:45 +00:00
parent 7230b5886f
commit 82e59c6d5f
2 changed files with 50 additions and 0 deletions

View File

@@ -616,6 +616,42 @@ void vd_push_back(std::vector<double>& vd, const double& x)
vd.push_back(x);
}
/************************************************************/
/* */
/* What if I want to return a pointer? */
/* */
/************************************************************/
//
// This example exposes the pointer by copying its referent
//
struct Record {
Record(int x) : value(x){}
int value;
};
const Record* get_record()
{
static Record v(1234);
return &v;
}
template class py::ExtensionClass<Record>; // explicitly instantiate
} // namespace extclass_demo
#ifndef PY_NO_INLINE_FRIENDS_IN_NAMESPACE
namespace py {
#endif
inline PyObject* to_python(const extclass_demo::Record* p)
{
return to_python(*p);
}
#ifndef PY_NO_INLINE_FRIENDS_IN_NAMESPACE
}
#endif
namespace extclass_demo {
/************************************************************/
/* */
/* init the module */
@@ -624,6 +660,10 @@ void vd_push_back(std::vector<double>& vd, const double& x)
void init_module(py::Module& m)
{
m.def(get_record, "get_record");
py::ClassWrapper<Record> record_class(m, "Record");
record_class.def_readonly(&Record::value, "value");
m.def(sizelist, "sizelist");
py::ClassWrapper<std::vector<double> > vector_double(m, "vector_double");

View File

@@ -841,6 +841,16 @@ test inheritB2
'B2::inheritB2'
>>> db2.inheritB2()
'B2::inheritB2'
===============================================================
test methodologies for wrapping functions that return a pointer
>>> get_record().value
1234
In this methodology, the referent is copied
>>> get_record() == get_record()
0
'''
from demo import *