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

automatic conversion to object for add_property()

[SVN r15065]
This commit is contained in:
Dave Abrahams
2002-08-23 04:15:37 +00:00
parent 946942214f
commit 0b33d1800d
3 changed files with 17 additions and 17 deletions

View File

@@ -230,8 +230,19 @@ class class_ : public objects::class_base
}
// Property creation
self& add_property(char const* name, object const& fget);
self& add_property(char const* name, object const& fget, object const& fset);
template <class Get>
self& add_property(char const* name, Get const& fget)
{
base::add_property(name, object(fget));
return *this;
}
template <class Get, class Set>
self& add_property(char const* name, Get const& fget, Set const& fset)
{
base::add_property(name, object(fget), object(fset));
return *this;
}
template <class U>
self& setattr(char const* name, U const& x)
@@ -367,20 +378,6 @@ inline class_<T,X1,X2,X3>::class_(char const* name, char const* doc, no_init_t)
this->def_no_init();
}
template <class T, class X1, class X2, class X3>
inline class_<T,X1,X2,X3>& class_<T,X1,X2,X3>::add_property(char const* name, object const& fget)
{
base::add_property(name, fget);
return *this;
}
template <class T, class X1, class X2, class X3>
inline class_<T,X1,X2,X3>& class_<T,X1,X2,X3>::add_property(char const* name, object const& fget, object const& fset)
{
base::add_property(name, fget, fset);
return *this;
}
namespace detail
{
// This is an mpl BinaryMetaFunction object with a runtime behavior,

View File

@@ -26,7 +26,7 @@ BOOST_PYTHON_MODULE_INIT(data_members_ext)
.def("value", &X::value)
.def("set", &X::set)
.def_readonly("x", &X::x)
.add_property("get_fair_value", object(&get_fair_value))
.add_property("fair_value", &get_fair_value)
;
class_<Y>("Y", args<int>())

View File

@@ -7,6 +7,9 @@
... except AttributeError: pass
... else: print 'no error'
>>> x.fair_value
42.0
>>> y = Y(69)
>>> y.x
69