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:
@@ -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");
|
||||
|
||||
@@ -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 *
|
||||
|
||||
Reference in New Issue
Block a user