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

Add docstring support for non-static properties.

[SVN r26814]
This commit is contained in:
Jonathan Brandmeyer
2005-01-22 21:41:37 +00:00
parent 4c21a29a9f
commit 5933fdbf39
6 changed files with 50 additions and 35 deletions

View File

@@ -269,9 +269,10 @@ namespace boost { namespace python
// property creation
template <class Get>
void add_property(char const* name, Get const& fget);
void add_property(char const* name, Get const& fget, char const* doc=0);
template <class Get, class Set>
void add_property(char const* name, Get const& fget, Set const& fset);
void add_property(
char const* name, Get const& fget, Set const& fset, char const* doc=0);
template <class Get>
void add_static_property(char const* name, Get const& fget);
@@ -564,14 +565,15 @@ class_& setattr(char const* name, U const& u);
<pre>
template &lt;class Get&gt;
void add_property(char const* name, Get const&amp; fget);
void add_property(char const* name, Get const&amp; fget, char const* doc=0);
template &lt;class Get, class Set&gt;
void add_property(char const* name, Get const&amp; fget, Set const&amp; fset);
void add_property(
char const* name, Get const&amp; fget, Set const&amp; fset, char const* doc=0);
</pre>
<dl class="function-semantics">
<dt><b>Requires:</b> <code>name</code> is an <a href=
"definitions.html#ntbs">ntbs</a> which conforms to Python's <a href=
"definitions.html#ntbs">ntbs</a> which conform to Python's <a href=
"http://www.python.org/doc/current/ref/identifiers.html">identifier
naming rules</a>.</dt>
@@ -580,9 +582,9 @@ void add_property(char const* name, Get const&amp; fget, Set const&amp; fset);
class instance, passing <code><a href=
"object.html#object-spec-ctors">object</a>(fget)</code> (and <code><a
href="object.html#object-spec-ctors">object</a>(fset)</code> in the
second form) to its constructor, then adds that property to the Python
class object under construction with the given attribute
<code>name</code>.</dt>
second form) with an (optional) docstring <code>doc</code> to its constructor,
then adds that property to the Python class object under construction
with the given attribute <code>name</code>.</dt>
<dt><b>Returns:</b> <code>*this</code></dt>
@@ -622,7 +624,7 @@ void add_static_property(char const* name, Get const&amp; fget, Set const&amp; f
<br>
<pre>
template &lt;class D&gt;
class_&amp; def_readonly(char const* name, D T::*pm);
class_&amp; def_readonly(char const* name, D T::*pm, char const* doc=0);
template &lt;class D&gt;
class_&amp; def_readonly(char const* name, D const&amp; d);
</pre>
@@ -631,14 +633,14 @@ class_&amp; def_readonly(char const* name, D const&amp; d);
<dt><b>Requires:</b> <code>name</code> is an <a href=
"definitions.html#ntbs">ntbs</a> which conforms to Python's <a href=
"http://www.python.org/doc/current/ref/identifiers.html">identifier
naming rules</a>.</dt>
naming rules</a>. <code>doc</code> is also an ntbs.</dt>
<dt><b>Effects:</b></dt>
<dd>
<pre>
this-&gt;add_property(name, <a href=
"data_members.html#make_getter-spec">make_getter</a>(pm));
"data_members.html#make_getter-spec">make_getter</a>(pm), doc);
</pre>
and
<pre>
@@ -657,7 +659,7 @@ this-&gt;add_static_property(name, <a href=
</dl>
<pre>
template &lt;class D&gt;
class_&amp; def_readwrite(char const* name, D T::*pm);
class_&amp; def_readwrite(char const* name, D T::*pm, char const* doc=0);
template &lt;class D&gt;
class_&amp; def_readwrite(char const* name, D&amp; d);
</pre>
@@ -669,7 +671,7 @@ class_&amp; def_readwrite(char const* name, D&amp; d);
<pre>
this-&gt;add_property(name, <a href=
"data_members.html#make_getter-spec">make_getter</a>(pm), <a href=
"data_members.html#make_setter-spec">make_setter</a>(pm));
"data_members.html#make_setter-spec">make_setter</a>(pm), doc);
</pre>
and
<pre>

View File

@@ -277,41 +277,42 @@ class class_ : public objects::class_base
// Data member access
//
template <class D>
self& def_readonly(char const* name, D const& d)
self& def_readonly(char const* name, D const& d, char const* doc=0)
{
return this->def_readonly_impl(name, d BOOST_PYTHON_DATA_MEMBER_HELPER(D));
return this->def_readonly_impl(name, d BOOST_PYTHON_DATA_MEMBER_HELPER(D), doc);
}
template <class D>
self& def_readwrite(char const* name, D const& d)
self& def_readwrite(char const* name, D const& d, char const* doc=0)
{
return this->def_readwrite_impl(name, d BOOST_PYTHON_DATA_MEMBER_HELPER(D));
return this->def_readwrite_impl(name, d BOOST_PYTHON_DATA_MEMBER_HELPER(D), doc);
}
template <class D>
self& def_readonly(char const* name, D& d)
self& def_readonly(char const* name, D& d, char const* doc=0)
{
return this->def_readonly_impl(name, d BOOST_PYTHON_DATA_MEMBER_HELPER(D));
return this->def_readonly_impl(name, d BOOST_PYTHON_DATA_MEMBER_HELPER(D), doc);
}
template <class D>
self& def_readwrite(char const* name, D& d)
self& def_readwrite(char const* name, D& d, char const* doc=0)
{
return this->def_readwrite_impl(name, d BOOST_PYTHON_DATA_MEMBER_HELPER(D));
return this->def_readwrite_impl(name, d BOOST_PYTHON_DATA_MEMBER_HELPER(D), doc);
}
// Property creation
template <class Get>
self& add_property(char const* name, Get fget)
self& add_property(char const* name, Get fget, char const* docstr = 0)
{
base::add_property(name, this->make_getter(fget));
base::add_property(name, this->make_getter(fget), docstr);
return *this;
}
template <class Get, class Set>
self& add_property(char const* name, Get fget, Set fset)
self& add_property(char const* name, Get fget, Set fset, char const* docstr = 0)
{
base::add_property(name, this->make_getter(fget), this->make_setter(fset));
base::add_property(
name, this->make_getter(fget), this->make_setter(fset), docstr);
return *this;
}
@@ -421,16 +422,16 @@ class class_ : public objects::class_base
template <class D, class B>
self& def_readonly_impl(
char const* name, D B::*pm_ BOOST_PYTHON_YES_DATA_MEMBER)
char const* name, D B::*pm_ BOOST_PYTHON_YES_DATA_MEMBER, char const* doc)
{
return this->add_property(name, pm_);
return this->add_property(name, pm_, doc);
}
template <class D, class B>
self& def_readwrite_impl(
char const* name, D B::*pm_ BOOST_PYTHON_YES_DATA_MEMBER)
char const* name, D B::*pm_ BOOST_PYTHON_YES_DATA_MEMBER, char const* doc)
{
return this->add_property(name, pm_, pm_);
return this->add_property(name, pm_, pm_, doc);
}
template <class D>

View File

@@ -34,8 +34,10 @@ struct BOOST_PYTHON_DECL class_base : python::api::object
void enable_pickling_(bool getstate_manages_dict);
protected:
void add_property(char const* name, object const& fget);
void add_property(char const* name, object const& fget, object const& fset);
void add_property(
char const* name, object const& fget, char const* docstr);
void add_property(char const* name,
object const& fget, object const& fset, char const* docstr);
void add_static_property(char const* name, object const& fget);
void add_static_property(char const* name, object const& fget, object const& fset);

View File

@@ -535,20 +535,22 @@ namespace objects
this->attr("__instance_size__") = instance_size;
}
void class_base::add_property(char const* name, object const& fget)
void class_base::add_property(
char const* name, object const& fget, char const* docstr)
{
object property(
(python::detail::new_reference)
PyObject_CallFunction((PyObject*)&PyProperty_Type, "O", fget.ptr()));
PyObject_CallFunction((PyObject*)&PyProperty_Type, "Osss", fget.ptr(), 0, 0, docstr));
this->setattr(name, property);
}
void class_base::add_property(char const* name, object const& fget, object const& fset)
void class_base::add_property(
char const* name, object const& fget, object const& fset, char const* docstr)
{
object property(
(python::detail::new_reference)
PyObject_CallFunction((PyObject*)&PyProperty_Type, "OO", fget.ptr(), fset.ptr()));
PyObject_CallFunction((PyObject*)&PyProperty_Type, "OOss", fget.ptr(), fset.ptr(), 0, docstr));
this->setattr(name, property);
}

View File

@@ -64,8 +64,11 @@ BOOST_PYTHON_MODULE(properties_ext)
class_<X>("X", init<int>() )
//defining read only property
.add_property( "value_r", &X::get_value )
.add_property( "value_r_ds", &X::get_value, "value_r_ds is read-only")
//defining read \ write property
.add_property( "value_rw", &X::get_value, &X::set_value )
.add_property( "value_rw_ds", &X::get_value, &X::set_value,
"value_rw_ds is read-write")
//defining read \ write property using make_getter and make_setter
.add_property( "value_direct",
make_getter( &X::m_value, return_by_value_t() ),

View File

@@ -79,6 +79,11 @@ after creating second intstance of X instances count is 2
>>> del x2
>>> assert x1.instance_count == 1
>>> assert properties.X.value_r_ds.__doc__ == "value_r_ds is read-only"
>>> assert properties.X.value_rw_ds.__doc__ == "value_rw_ds is read-write"
"""
#import sys; sys.path.append(r'P:\Actimize4.0\smart_const\py_smart_const___Win32_Debug')