bpl_mods.txt

来自「Boost provides free peer-reviewed portab」· 文本 代码 · 共 913 行 · 第 1/3 页

TXT
913
字号
Copyright David Abrahams 2006. Distributed under the BoostSoftware License, Version 1.0. (See accompanyingfile LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt).. This is a comment. Note how any initial comments are moved by   transforms to after the document title, subtitle, and docinfo... Need intro and conclusion.. Exposing classes    .. Constructors    .. Overloading    .. Properties and data members    .. Inheritance    .. Operators and Special Functions    .. Virtual Functions.. Call Policies++++++++++++++++++++++++++++++++++++++++++++++ Introducing Boost.Python (Extended Abstract)++++++++++++++++++++++++++++++++++++++++++++++.. bibliographic fields (which also require a transform)::Author: David Abrahams:Address: 45 Walnut Street          Somerville, MA 02143:Contact: dave@boost-consulting.com:organization: `Boost Consulting`_:date: $Date: 2006-09-11 18:27:29 -0400 (Mon, 11 Sep 2006) $:status: This is a "work in progress":version: 1:copyright: Copyright David Abrahams 2002. All rights reserved:Dedication:    For my girlfriend, wife, and partner Luann:abstract:    This paper describes the Boost.Python library, a system for    C++/Python interoperability... meta::   :keywords: Boost,python,Boost.Python,C++   :description lang=en: C++/Python interoperability with Boost.Python.. contents:: Table of Contents.. section-numbering::.. _`Boost Consulting`: http://www.boost-consulting.com============== Introduction==============Python and C++ are in many ways as different as two languages couldbe: while C++ is usually compiled to machine-code, Python isinterpreted.  Python's dynamic type system is often cited as thefoundation of its flexibility, while in C++ static typing is thecornerstone of its efficiency. C++ has an intricate and difficultmeta-language to support compile-time polymorphism, while Python isa uniform language with convenient runtime polymorphism.Yet for many programmers, these very differences mean that Python andC++ complement one another perfectly.  Performance bottlenecks inPython programs can be rewritten in C++ for maximal speed, andauthors of powerful C++ libraries choose Python as a middlewarelanguage for its flexible system integration capabilities.Furthermore, the surface differences mask some strong similarities:* 'C'-family control structures (if, while, for...)* Support for object-orientation, functional programming, and generic  programming (these are both *multi-paradigm* programming languages.)* Comprehensive operator overloading facilities, recognizing the  importance of syntactic variability for readability and  expressivity.* High-level concepts such as collections and iterators.* High-level encapsulation facilities (C++: namespaces, Python: modules)  to support the design of re-usable libraries.* Exception-handling for effective management of error conditions.* C++ idioms in common use, such as handle/body classes and  reference-counted smart pointers mirror Python reference semantics.Python provides a rich 'C' API for writers of 'C' extension modules.Unfortunately, using this API directly for exposing C++ type andfunction interfaces to Python is much more tedious than it should be.This is mainly due to the limitations of the 'C' language.  Compared toC++ and Python, 'C' has only very rudimentary abstraction facilities.Support for exception-handling is completely missing. One importantundesirable consequence is that 'C' extension module writers arerequired to manually manage Python reference counts. Another unpleasantconsequence is a very high degree of repetition of similar code in 'C'extension modules. Of course highly redundant code does not only causefrustration for the module writer, but is also very difficult tomaintain.The limitations of the 'C' API have lead to the development of avariety of wrapping systems. SWIG_ is probably the most popular packagefor the integration of C/C++ and Python. A more recent development isthe SIP_ package, which is specifically designed for interfacing Pythonwith the Qt_ graphical user interface library. Both SWIG and SIPintroduce a new specialized language for defining the inter-languagebindings. Of course being able to use a specialized language hasadvantages, but having to deal with three different languages (Python,C/C++ and the interface language) also introduces practical and mentaldifficulties. The CXX_ package demonstrates an interesting alternative.It shows that at least some parts of Python's 'C' API can be wrappedand presented through a much more user-friendly C++ interface. However,unlike SWIG and SIP, CXX does not include support for wrapping C++classes as new Python types. CXX is also no longer actively developed.In some respects Boost.Python combines ideas from SWIG and SIP withideas from CXX. Like SWIG and SIP, Boost.Python is a system forwrapping C++ classes as new Python "built-in" types, and C/C++functions as Python functions. Like CXX, Boost.Python presents Python's'C' API through a C++ interface. Boost.Python goes beyond the scope ofother systems with the unique support for C++ virtual functions thatare overrideable in Python, support for organizing extensions as Pythonpackages with a central registry for inter-language type conversions,and a convenient mechanism for tying into Python's serialization engine(pickle). Importantly, all this is achieved without introducing a newsyntax. Boost.Python leverages the power of C++ meta-programmingtechniques to introspect about the C++ type system, and presents asimple, IDL-like C++ interface for exposing C/C++ code in extensionmodules. Boost.Python is a pure C++ library, the inter-languagebindings are defined in pure C++, and other than a C++ compiler onlyPython itself is required to get started with Boost.Python. Last butnot least, Boost.Python is an unrestricted open source library. Thereare no strings attached even for commercial applications... _SWIG: http://www.swig.org/.. _SIP: http://www.riverbankcomputing.co.uk/sip/index.php.. _Qt: http://www.trolltech.com/.. _CXX: http://cxx.sourceforge.net/=========================== Boost.Python Design Goals ===========================The primary goal of Boost.Python is to allow users to expose C++classes and functions to Python using nothing more than a C++compiler.  In broad strokes, the user experience should be one ofdirectly manipulating C++ objects from Python.However, it's also important not to translate all interfaces *too*literally: the idioms of each language must be respected.  Forexample, though C++ and Python both have an iterator concept, they areexpressed very differently.  Boost.Python has to be able to bridge theinterface gap.It must be possible to insulate Python users from crashes resultingfrom trivial misuses of C++ interfaces, such as accessingalready-deleted objects.  By the same token the library shouldinsulate C++ users from low-level Python 'C' API, replacingerror-prone 'C' interfaces like manual reference-count management andraw ``PyObject`` pointers with more-robust alternatives.Support for component-based development is crucial, so that C++ typesexposed in one extension module can be passed to functions exposed inanother without loss of crucial information like C++ inheritancerelationships.Finally, all wrapping must be *non-intrusive*, without modifying oreven seeing the original C++ source code.  Existing C++ libraries haveto be wrappable by third parties who only have access to header filesand binaries.========================== Hello Boost.Python World==========================And now for a preview of Boost.Python, and how it improves on the rawfacilities offered by Python. Here's a function we might want toexpose::    char const* greet(unsigned x)    {       static char const* const msgs[] = { "hello", "Boost.Python", "world!" };       if (x > 2)            throw std::range_error("greet: index out of range");       return msgs[x];    }To wrap this function in standard C++ using the Python 'C' API, we'dneed something like this::    extern "C" // all Python interactions use 'C' linkage and calling convention    {        // Wrapper to handle argument/result conversion and checking        PyObject* greet_wrap(PyObject* args, PyObject * keywords)        {             int x;             if (PyArg_ParseTuple(args, "i", &x))    // extract/check arguments             {                 char const* result = greet(x);      // invoke wrapped function                 return PyString_FromString(result); // convert result to Python             }             return 0;                               // error occurred        }        // Table of wrapped functions to be exposed by the module        static PyMethodDef methods[] = {            { "greet", greet_wrap, METH_VARARGS, "return one of 3 parts of a greeting" }            , { NULL, NULL, 0, NULL } // sentinel        };        // module initialization function        DL_EXPORT init_hello()        {            (void) Py_InitModule("hello", methods); // add the methods to the module        }    }Now here's the wrapping code we'd use to expose it with Boost.Python::    #include <boost/python.hpp>    using namespace boost::python;    BOOST_PYTHON_MODULE(hello)    {        def("greet", greet, "return one of 3 parts of a greeting");    }and here it is in action::    >>> import hello    >>> for x in range(3):    ...     print hello.greet(x)    ...    hello    Boost.Python    world!Aside from the fact that the 'C' API version is much more verbose thanthe BPL one, it's worth noting that it doesn't handle a few thingscorrectly:* The original function accepts an unsigned integer, and the Python  'C' API only gives us a way of extracting signed integers. The  Boost.Python version will raise a Python exception if we try to pass  a negative number to ``hello.greet``, but the other one will proceed  to do whatever the C++ implementation does when converting an  negative integer to unsigned (usually wrapping to some very large  number), and pass the incorrect translation on to the wrapped  function.* That brings us to the second problem: if the C++ ``greet()``  function is called with a number greater than 2, it will throw an  exception.  Typically, if a C++ exception propagates across the  boundary with code generated by a 'C' compiler, it will cause a  crash.  As you can see in the first version, there's no C++  scaffolding there to prevent this from happening.  Functions wrapped  by Boost.Python automatically include an exception-handling layer  which protects Python users by translating unhandled C++ exceptions  into a corresponding Python exception.* A slightly more-subtle limitation is that the argument conversion  used in the Python 'C' API case can only get that integer ``x`` in  *one way*.  PyArg_ParseTuple can't convert Python ``long`` objects  (arbitrary-precision integers) which happen to fit in an ``unsigned  int`` but not in a ``signed long``, nor will it ever handle a  wrapped C++ class with a user-defined implicit ``operator unsigned  int()`` conversion.  The BPL's dynamic type conversion registry  allows users to add arbitrary conversion methods.================== Library Overview==================This section outlines some of the library's major features.  Except asnecessary to avoid confusion, details of library implementation areomitted.------------------------------------------- The fundamental type-conversion mechanism-------------------------------------------XXX This needs to be rewritten.Every argument of every wrapped function requires some kind ofextraction code to convert it from Python to C++.  Likewise, thefunction return value has to be converted from C++ to Python.Appropriate Python exceptions must be raised if the conversion fails.Argument and return types are part of the function's type, and much ofthis tedium can be relieved if the wrapping system can extract thatinformation through  introspection.Passing a wrapped C++ derived class instance to a C++ functionaccepting a pointer or reference to a base class requires knowledge ofthe inheritance relationship and how to translate the address of a baseclass into that of a derived class.------------------ Exposing Classes------------------

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?