]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | [section boost/python/def.hpp] |
2 | [section Introduction] | |
3 | `def()` is the function which can be used to expose C++ functions and callable objects as Python functions in the [link high_level_components.boost_python_scope_hpp.introduction current scope]. | |
4 | [endsect] | |
5 | [section Functions] | |
6 | `` | |
7 | template <class F> | |
8 | void def(char const* name, F f); | |
9 | ||
10 | template <class Fn, class A1> | |
11 | void def(char const* name, Fn fn, A1 const&); | |
12 | ||
13 | template <class Fn, class A1, class A2> | |
14 | void def(char const* name, Fn fn, A1 const&, A2 const&); | |
15 | ||
16 | template <class Fn, class A1, class A2, class A3> | |
17 | void def(char const* name, Fn fn, A1 const&, A2 const&, A3 const&); | |
18 | `` | |
19 | [variablelist | |
20 | [[Requires][name is an [link ntbs] which conforms to Python's [@http://www.python.org/doc/current/ref/identifiers.html identifier naming rules]. | |
21 | ||
22 | * If `Fn` is [derived from] [link object_wrappers.boost_python_object_hpp.class_object object], it will be added to the [link high_level_components.boost_python_scope_hpp.introduction current scope] as a single overload. To be useful, `fn` should be [@http://www.python.org/doc/current/lib/built-in-funcs.html#l2h-6 callable]. | |
23 | * If `a1` is the result of an [link function_invocation_and_creation.boost_python_overloads_hpp.introduction.overload_dispatch_expressions overload-dispatch-expression], only the second form is allowed and `fn` must be a pointer to function or pointer to member function whose [link arity] is the same as A1's [link function_invocation_and_creation.boost_python_overloads_hpp.introduction.overload_dispatch_expressions maximum arity]. | |
24 | ||
25 | [*Effects:] For each prefix `P` of `Fn`\ 's sequence of argument types, beginning with the one whose length is `A1`\ 's [link function_invocation_and_creation.boost_python_overloads_hpp.introduction.overload_dispatch_expressions minimum arity], adds a `name(...)` function overload to the [link high_level_components.boost_python_scope_hpp.introduction current scope]. Each overload generated invokes a1's call-expression with P, using a copy of a1's call policies. If the longest valid prefix of A1 contains N types and a1 holds M keywords, an initial sequence of the keywords are used for all but the first N - M arguments of each overload. | |
26 | ||
27 | * Otherwise, fn must be a non-null function or member function pointer, and a single function overload built around fn is added to the current scope. If any of a1-a3 are supplied, they may be selected in any order from the table below. | |
28 | ||
29 | [table | |
30 | [[Mnemonic Name][Requirements/Type properties][Effects]] | |
31 | [[docstring][Any [link ntbs]][Value will be bound to the `__doc__` attribute of the resulting method overload.]] | |
32 | [[policies][A model of [link concepts.callpolicies CallPolicies]][A copy will be used as the call policies of the resulting method overload.]] | |
33 | [[keywords][The result of a [link function_invocation_and_creation.boost_python_args_hpp.introduction.keyword_expressions keyword-expression] specifying no more arguments than the [link arity] of `fn`.][A copy will be used as the call policies of the resulting method overload.]] | |
34 | ] | |
35 | ]] | |
36 | ] | |
37 | [endsect] | |
38 | [section Example] | |
39 | `` | |
40 | #include <boost/python/def.hpp> | |
41 | #include <boost/python/module.hpp> | |
42 | #include <boost/python/args.hpp> | |
43 | ||
44 | using namespace boost::python; | |
45 | ||
46 | char const* foo(int x, int y) { return "foo"; } | |
47 | ||
48 | BOOST_PYTHON_MODULE(def_test) | |
49 | { | |
50 | def("foo", foo, args("x", "y"), "foo's docstring"); | |
51 | } | |
52 | `` | |
53 | [endsect] | |
54 | [endsect] |