Skip to content

Commit 0b33d18

Browse files
committed
automatic conversion to object for add_property()
[SVN r15065]
1 parent 9469422 commit 0b33d18

3 files changed

Lines changed: 17 additions & 17 deletions

File tree

include/boost/python/class.hpp

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,19 @@ class class_ : public objects::class_base
230230
}
231231

232232
// Property creation
233-
self& add_property(char const* name, object const& fget);
234-
self& add_property(char const* name, object const& fget, object const& fset);
233+
template <class Get>
234+
self& add_property(char const* name, Get const& fget)
235+
{
236+
base::add_property(name, object(fget));
237+
return *this;
238+
}
239+
240+
template <class Get, class Set>
241+
self& add_property(char const* name, Get const& fget, Set const& fset)
242+
{
243+
base::add_property(name, object(fget), object(fset));
244+
return *this;
245+
}
235246

236247
template <class U>
237248
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)
367378
this->def_no_init();
368379
}
369380

370-
template <class T, class X1, class X2, class X3>
371-
inline class_<T,X1,X2,X3>& class_<T,X1,X2,X3>::add_property(char const* name, object const& fget)
372-
{
373-
base::add_property(name, fget);
374-
return *this;
375-
}
376-
377-
template <class T, class X1, class X2, class X3>
378-
inline class_<T,X1,X2,X3>& class_<T,X1,X2,X3>::add_property(char const* name, object const& fget, object const& fset)
379-
{
380-
base::add_property(name, fget, fset);
381-
return *this;
382-
}
383-
384381
namespace detail
385382
{
386383
// This is an mpl BinaryMetaFunction object with a runtime behavior,

test/data_members.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ BOOST_PYTHON_MODULE_INIT(data_members_ext)
2626
.def("value", &X::value)
2727
.def("set", &X::set)
2828
.def_readonly("x", &X::x)
29-
.add_property("get_fair_value", object(&get_fair_value))
29+
.add_property("fair_value", &get_fair_value)
3030
;
3131

3232
class_<Y>("Y", args<int>())

test/data_members.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
... except AttributeError: pass
88
... else: print 'no error'
99
10+
>>> x.fair_value
11+
42.0
12+
1013
>>> y = Y(69)
1114
>>> y.x
1215
69

0 commit comments

Comments
 (0)