Skip to content

Commit 07c1319

Browse files
committed
Added the new arg class from
"nickm-at-sitius.com" (Nikolay Mladenov) which supplies the ability to wrap functions that can be called with ommitted arguments in the middle. [SVN r19441]
1 parent 714b5dc commit 07c1319

File tree

6 files changed

+241
-54
lines changed

6 files changed

+241
-54
lines changed

doc/v2/args.html

Lines changed: 114 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<html>
44
<head>
55
<meta name="generator" content=
6-
"HTML Tidy for Windows (vers 1st August 2002), see www.w3.org">
6+
"HTML Tidy for Cygwin (vers 1st April 2002), see www.w3.org">
77
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
88
<link rel="stylesheet" type="text/css" href="../boost.css">
99

@@ -35,7 +35,31 @@ <h2>Contents</h2>
3535

3636
<dt><a href="#keyword-expression"><i>keyword-expressions</i></a></dt>
3737

38-
<dt><a href="#functions">Functions</a></dt>
38+
<dt><a href="#classes">Classes</a></dt>
39+
40+
<dd>
41+
<dl class="page-index">
42+
<dt><a href="#arg-spec">class <code>arg</code></a></dt>
43+
44+
<dd>
45+
<dl class="page-index">
46+
<dt><a href="#arg-synopsis">class <code>arg</code>
47+
synopsis</a></dt>
48+
49+
<dt><a href="#arg-ctor">class <code>arg</code>
50+
constructor</a></dt>
51+
52+
<dt><a href="#arg-operator">class <code>arg</code> template
53+
<code>operator =</code></a></dt>
54+
</dl>
55+
</dd>
56+
</dl>
57+
</dd>
58+
59+
<dt><a href="#keyword-expression-operators"><i>Keyword-expression</i>
60+
operator <code>,</code></a></dt>
61+
62+
<dt><a href="#functions">Functions (deprecated)</a></dt>
3963

4064
<dd>
4165
<dl class="page-index">
@@ -57,47 +81,117 @@ <h2><a name="keyword-expression"></a><i>keyword-expressions</i></h2>
5781

5882
<p>A <b>keyword-expression</b> results in an object which holds a
5983
sequence of <a href="definitions.html#ntbs">ntbs</a>es, and whose type
60-
encodes the number of keywords specified.</p>
84+
encodes the number of keywords specified. The <b>keyword-expression</b>
85+
may contain default values for some or all of the keywords it holds</p>
86+
87+
<h2><a name="classes"></a>Classes</h2>
6188

62-
<h2><a name="functions"></a>Functions</h2>
89+
<h3><a name="arg-spec"></a><code>class arg;</code></h3>
6390

64-
<h3><a name="args-spec"></a><code>args(</code>...<code>)</code></h3>
91+
<p>The objects of class arg are keyword-expressions holding one keyword (
92+
size one )</p>
93+
94+
<h4><a name="arg-synopsis"></a>Class <code>arg</code> synopsis</h4>
95+
<pre>
96+
namespace boost { namespace python
97+
{
98+
struct arg
99+
{
100+
template &lt;class T&gt;
101+
arg &amp;perator = (T const &amp;value);
102+
explicit arg (char const *name){elements[0].name = name;}
103+
};
104+
105+
}}
106+
</pre>
107+
108+
<h4><a name="arg-ctor"></a>Class <code>arg</code> constructor</h4>
65109
<pre>
66-
<i>unspecified1</i> args(char const*);
67-
<i>unspecified2</i> args(char const*, char const*);
68-
.
69-
.
70-
.
71-
<i>unspecifiedN</i> args(char const*, char const*, ... char const*);
110+
arg(char const* name);
72111
</pre>
73112

74113
<dl class="function-semantics">
75-
<dt><b>Requires:</b> Every argument must be a <a href=
114+
<dt><b>Requires:</b> The argument must be a <a href=
76115
"definitions.html#ntbs">ntbs</a>.</dt>
77116

78-
<dt><b>Returns:</b> an object representing a <a href=
79-
"#keyword-expression"><i>keyword-expression</i></a> encapsulating the
80-
arguments passed.</dt>
117+
<dt><b>Effects:</b> Constructs an <code>arg</code> object holding a
118+
keyword with name <code>name</code>.</dt>
119+
</dl>
120+
121+
<h4><a name="arg-operator"></a>Class <code>arg</code> operator =</h4>
122+
<pre>
123+
template &lt;class T&gt; arg &amp;operator = (T const &amp;value);
124+
</pre>
125+
126+
<dl class="function-semantics">
127+
<dt><b>Requires:</b> The argument must convertible to python.</dt>
128+
129+
<dt><b>Effects:</b> Assigns default value for the keyword.</dt>
130+
131+
<dt><b>Returns:</b> Reference to <code>this</code>.</dt>
132+
</dl>
133+
134+
<h2><a name="keyword-expression-operators"><i>Keyword-expression</i>
135+
operator <code>,</code></a></h2>
136+
<pre>
137+
<i>keyword-expression</i> operator , (<i>keyword-expression</i>, const arg &amp;kw) const
138+
<i>keyword-expression</i> operator , (<i>keyword-expression</i>, const char *name) const;
139+
</pre>
140+
141+
<dl class="function-semantics">
142+
<dt><b>Requires:</b> The argument <code>name</code> must be a <a href=
143+
"definitions.html#ntbs">ntbs</a>.</dt>
144+
145+
<dt><b>Effects:</b> Extends the <i>keyword-expression</i> argument with
146+
one more keyword.</dt>
147+
148+
<dt><b>Returns:</b> The extended <i>keyword-expression</i>.</dt>
149+
</dl>
150+
151+
<h2><font color="#7F7F7F"><a name="functions"></a>Functions
152+
(deprecated)</font></h2>
153+
154+
<h3><a name="args-spec"></a><code><font color=
155+
"#7F7F7F">args</font>(</code>...<code>)</code></h3>
156+
<pre>
157+
<font color="#7F7F7F"> <i>unspecified1</i> args(char const*);
158+
<i>unspecified2</i> args(char const*, char const*);
159+
.
160+
.
161+
.
162+
<i>unspecifiedN</i> args(char const*, char const*, ... char const*);
163+
</font>
164+
</pre>
165+
166+
<dl class="function-semantics">
167+
<dt><font color="#7F7F7F"><b>Requires:</b> Every argument must be a <a
168+
href="definitions.html#ntbs">ntbs</a>.</font></dt>
169+
170+
<dt><font color="#7F7F7F"><b>Returns:</b> an object representing a <a
171+
href="#keyword-expression"><i>keyword-expression</i></a> encapsulating
172+
the arguments passed.</font></dt>
81173
</dl>
82174

83175
<h2><a name="examples"></a>Example</h2>
84176
<pre>
85177
#include &lt;boost/python/def.hpp&gt;
86178
using namespace boost::python;
87179

88-
int f(int x, int y, int z);
180+
int f(double x, double y, double z=0.0, double w=1.0);
89181

90182
BOOST_PYTHON_MODULE(xxx)
91183
{
92-
def("f", f, args("x", "y", "z"));
184+
def("f", f
185+
, ( arg("x"), "y", arg("z")=0.0, arg("w")=1.0 )
186+
);
93187
}
94188
</pre>
95189

96-
<p>Revised 05 November, 2001</p>
190+
<p>Revised 01 August, 2003</p>
97191

98192
<p><i>&copy; Copyright <a href=
99-
"../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002. All Rights
100-
Reserved.</i></p>
193+
"../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002-2003. All
194+
Rights Reserved.</i></p>
101195
</body>
102196
</html>
103197

include/boost/python/args.hpp

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@
2323
# include <boost/preprocessor/iteration/local.hpp>
2424

2525
# include <boost/python/detail/mpl_lambda.hpp>
26+
# include <boost/python/object_core.hpp>
2627

2728
# include <boost/mpl/bool.hpp>
2829

2930
# include <cstddef>
30-
31+
# include <algorithm>
3132

3233
namespace boost { namespace python {
3334

@@ -42,6 +43,16 @@ namespace detail
4243
{
4344
return keyword_range(elements, elements + nkeywords);
4445
}
46+
47+
keywords<nkeywords+1> operator,(const keywords<1> &k) const
48+
{
49+
python::detail::keywords<size+1> res;
50+
std::copy(elements, elements+size, res.elements);
51+
res.elements[size] = k.elements[0];
52+
return res;
53+
}
54+
55+
keywords<nkeywords+1> operator,(const char *name) const;
4556

4657
keyword elements[nkeywords];
4758
};
@@ -97,6 +108,37 @@ namespace detail
97108
# endif
98109
}
99110

111+
struct arg : detail::keywords<1>
112+
{
113+
explicit arg(char const *name)
114+
{
115+
elements[0].name = name;
116+
}
117+
118+
template <class T>
119+
arg& operator=(T const& value)
120+
{
121+
object z(value);
122+
elements[0].default_value = handle<>(python::borrowed(object(value).ptr()));
123+
return *this;
124+
}
125+
126+
operator detail::keyword const&() const
127+
{
128+
return elements[0];
129+
}
130+
};
131+
132+
namespace detail
133+
{
134+
template <std::size_t nkeywords>
135+
inline keywords<nkeywords + 1>
136+
keywords<nkeywords>::operator,(const char *name) const
137+
{
138+
return this->operator,(arg(name));
139+
}
140+
}
141+
100142
# define BOOST_PYTHON_ASSIGN_NAME(z, n, _) result.elements[n].name = name##n;
101143
# define BOOST_PP_LOCAL_MACRO(n) \
102144
inline detail::keywords<n> args(BOOST_PP_ENUM_PARAMS_Z(1, n, char const* name)) \

include/boost/python/args_fwd.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ namespace detail
1919
{
2020
struct keyword
2121
{
22+
keyword(char const* name_=0)
23+
: name(name_)
24+
{}
25+
2226
char const* name;
2327
handle<> default_value;
2428
};

include/boost/python/object/function.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ struct BOOST_PYTHON_DECL function : PyObject
5151
object m_namespace;
5252
object m_doc;
5353
object m_arg_names;
54+
unsigned m_nkeyword_values;
5455
};
5556

5657
//

0 commit comments

Comments
 (0)