minor redesign/generalization of def_property mechanism

This commit is contained in:
Wenzel Jakob 2016-03-21 17:54:24 +01:00
parent 76eb607d4e
commit 84ec78feeb

View File

@ -799,7 +799,6 @@ public:
template <typename C, typename D, typename... Extra> template <typename C, typename D, typename... Extra>
class_ &def_readwrite(const char *name, D C::*pm, const Extra&... extra) { class_ &def_readwrite(const char *name, D C::*pm, const Extra&... extra) {
cpp_function fget([pm](const C &c) -> const D &{ return c.*pm; }, cpp_function fget([pm](const C &c) -> const D &{ return c.*pm; },
return_value_policy::reference_internal,
is_method(*this), extra...), is_method(*this), extra...),
fset([pm](C &c, const D &value) { c.*pm = value; }, fset([pm](C &c, const D &value) { c.*pm = value; },
is_method(*this), extra...); is_method(*this), extra...);
@ -810,7 +809,6 @@ public:
template <typename C, typename D, typename... Extra> template <typename C, typename D, typename... Extra>
class_ &def_readonly(const char *name, const D C::*pm, const Extra& ...extra) { class_ &def_readonly(const char *name, const D C::*pm, const Extra& ...extra) {
cpp_function fget([pm](const C &c) -> const D &{ return c.*pm; }, cpp_function fget([pm](const C &c) -> const D &{ return c.*pm; },
return_value_policy::reference_internal,
is_method(*this), extra...); is_method(*this), extra...);
def_property_readonly(name, fget); def_property_readonly(name, fget);
return *this; return *this;
@ -818,8 +816,7 @@ public:
template <typename D, typename... Extra> template <typename D, typename... Extra>
class_ &def_readwrite_static(const char *name, D *pm, const Extra& ...extra) { class_ &def_readwrite_static(const char *name, D *pm, const Extra& ...extra) {
cpp_function fget([pm](object) -> const D &{ return *pm; }, cpp_function fget([pm](object) -> const D &{ return *pm; }, scope(*this), extra...),
return_value_policy::reference_internal, scope(*this), extra...),
fset([pm](object, const D &value) { *pm = value; }, scope(*this), extra...); fset([pm](object, const D &value) { *pm = value; }, scope(*this), extra...);
def_property_static(name, fget, fset); def_property_static(name, fget, fset);
return *this; return *this;
@ -827,37 +824,43 @@ public:
template <typename D, typename... Extra> template <typename D, typename... Extra>
class_ &def_readonly_static(const char *name, const D *pm, const Extra& ...extra) { class_ &def_readonly_static(const char *name, const D *pm, const Extra& ...extra) {
cpp_function fget([pm](object) -> const D &{ return *pm; }, cpp_function fget([pm](object) -> const D &{ return *pm; }, scope(*this), extra...);
return_value_policy::reference_internal, scope(*this), extra...);
def_property_readonly_static(name, fget); def_property_readonly_static(name, fget);
return *this; return *this;
} }
class_ &def_property_readonly(const char *name, const cpp_function &fget, const char *doc = nullptr) { template <typename... Extra>
def_property(name, fget, cpp_function(), doc); class_ &def_property_readonly(const char *name, const cpp_function &fget, const Extra& ...extra) {
def_property(name, fget, cpp_function(), extra...);
return *this; return *this;
} }
class_ &def_property_readonly_static(const char *name, const cpp_function &fget, const char *doc = nullptr) { template <typename... Extra>
def_property_static(name, fget, cpp_function(), doc); class_ &def_property_readonly_static(const char *name, const cpp_function &fget, const Extra& ...extra) {
def_property_static(name, fget, cpp_function(), extra...);
return *this; return *this;
} }
class_ &def_property(const char *name, const cpp_function &fget, const cpp_function &fset, const char *doc = nullptr) { template <typename... Extra>
object doc_obj = doc ? pybind11::str(doc) : (object) fget.attr("__doc__"); class_ &def_property(const char *name, const cpp_function &fget, const cpp_function &fset, const Extra& ...extra) {
return def_property_static(name, fget, fset, is_method(*this),
return_value_policy::reference_internal, extra...);
}
template <typename... Extra>
class_ &def_property_static(const char *name, const cpp_function &fget, const cpp_function &fset, const Extra& ...extra) {
auto rec_fget = get_function_record(fget), rec_fset = get_function_record(fset);
detail::process_attributes<Extra...>::init(extra..., rec_fget);
if (rec_fset)
detail::process_attributes<Extra...>::init(extra..., rec_fset);
pybind11::str doc_obj = pybind11::str(rec_fget->doc ? rec_fget->doc : "");
object property( object property(
PyObject_CallFunctionObjArgs((PyObject *) &PyProperty_Type, fget.ptr() ? fget.ptr() : Py_None, PyObject_CallFunctionObjArgs((PyObject *) &PyProperty_Type, fget.ptr() ? fget.ptr() : Py_None,
fset.ptr() ? fset.ptr() : Py_None, Py_None, doc_obj.ptr(), nullptr), false); fset.ptr() ? fset.ptr() : Py_None, Py_None, doc_obj.ptr(), nullptr), false);
attr(name) = property; if (rec_fget->class_)
return *this; attr(name) = property;
} else
metaclass().attr(name) = property;
class_ &def_property_static(const char *name, const cpp_function &fget, const cpp_function &fset, const char *doc = nullptr) {
object doc_obj = doc ? pybind11::str(doc) : (object) fget.attr("__doc__");
object property(
PyObject_CallFunctionObjArgs((PyObject *) &PyProperty_Type, fget.ptr() ? fget.ptr() : Py_None,
fset.ptr() ? fset.ptr() : Py_None, Py_None, doc_obj.ptr(), nullptr), false);
metaclass().attr(name) = property;
return *this; return *this;
} }
@ -911,6 +914,12 @@ private:
} }
generic_type::dealloc((detail::instance<void> *) inst); generic_type::dealloc((detail::instance<void> *) inst);
} }
static detail::function_record *get_function_record(handle h) {
h = detail::get_function(h);
return h ? (detail::function_record *) capsule(
PyCFunction_GetSelf(h.ptr()), true) : nullptr;
}
}; };
/// Binds C++ enumerations and enumeration classes to Python /// Binds C++ enumerations and enumeration classes to Python