Boost Python def function Export

Source: Internet
Author: User

Introduction

def()Is the function which can be used to expose C ++ functions and callable objects as Python functions in the currentscope.

This function can be used to export functions in C ++ to Python. To be honest, it's really easy to use.

Functions

Def

template <class F>void def(char const* name, F f);template <class Fn, class A1>void def(char const* name, Fn fn, A1 const&);template <class Fn, class A1, class A2>void def(char const* name, Fn fn, A1 const&, A2 const&);template <class Fn, class A1, class A2, class A3>void def(char const* name, Fn fn, A1 const&, A2 const&, A3 const&);

Requires:nameIs an NTBs which conforms to Python's identifier
Naming rules.

  • IfFnIs [derived from]object, It will be added to
    Current scope as a single overload. To be useful,fnShocould be callable.
  • Ifa1Is the result ofOverload-Dispatch-expression, Only the second form
    Is allowed and FN must be a pointer to function or pointer to member function whose Arity is the same as A1'sMaximum
    Arity
    .

    Effects:For each prefix POf Fn'S sequence of argument types, beginning with the one whose length is A1'S Minimum
    Arity
    , Adds name(... )Function overload to the current
    Scope. Each overload generated invokes a1'S call-expression P, Using a copy a1'S call
    Policies. If the longest valid prefix A1Contains NTypes and a1Holds MKeywords, an initial sequence of the keywords are used
    For all but the first N- MArguments of each overload.
  • 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 anya1-a3Are
    Supplied, they may be selected in any order from the table below.
Memnonic name Requirements/Type Properties Effects
Docstring Any NTBs. Value will be bound to__doc__Attribute of the resulting method overload.
Policies A Model of callpolicies A copy will be used as the call policies of the resulting method overload.
Keywords The result ofKeyword-expressionSpecifying no more arguments than thearityfn. A copy will be used as the call policies of the resulting method overload.
In the example below, it is easy to understand. Mainly
For the first parameter, enter the second parameter you used in Python, write the third parameter of the function pointer, and fill in the parameter name so that boost has several parameters as long as your function. The fourth parameter, is the description, can ignore in fact there is a very important return protocol (Return Policy), see: http://blog.csdn.net/cedricporter/article/details/6828322

#include <boost/python/def.hpp>#include <boost/python/module.hpp>#include <boost/python/args.hpp>using namespace boost::python;char const* foo(int x, int y) { return "foo"; }BOOST_PYTHON_MODULE(def_test){    def("foo", foo, args("x", "y"), "foo's docstring");}

7 March, 2003

Copyright Dave Abrahams 2002.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.