C++ Boost

Boost.Python

CallPolicies Concept


Introduction
CallPolicies Composition
Concept Requirements
CallPolicies Concept
ܼ/a>
CallPoliciesשꏼ/a>
腄Ҫdz
CallPolicies腄a>

Introduction

ܼ/h2>

Models of the CallPolicies concept are used to specialize the behavior of Python callable objects generated by Boost.Python to wrapped C++ objects like function and member function pointers, providing three behaviors:

巔o߂ԣ胡llPolicies㩸ńЍ㬍 ӃӚָ樂oost.Python˹ɺ㉵Đython瓃攏㵄Ϊ㬍 Ҕװ碈純ʽꍳɔắʽָի֮ 儃++攏㣬 ˼̡驈Ϊ㺍

  1. precall - Python argument tuple management before the wrapped object is invoked
  2. result_converter - C++ return value handling
  3. postcall - Python argument tuple and result management after the wrapped object is invoked
  4. extract_return_type - metafunction for extracting the return type from a given signature type sequence
  1. precall - Ԛ巔÷◰攏㖮ǰ需퐹thon⎊픪ש
  2. result_converter - 䦀탫+絻ؖ彎li>
  3. postcall - Ԛ巔÷◰攏㖮곹܀퐹thon⎊픪שꍽṻ
  4. extract_return_type - Ԫꯊmetafunction㩣썊 䓸趨ǩuʽ ЍP֐̡ȡ絻؀Ѝ

CallPolicies Composition

CallPoliciesשꏼ/h2>

In order to allow the use of multiple models of CallPolicies in the same callable object, Boost.Python's CallPolicies class templates provide a chaining interface which allows them to be recursively composed. This interface takes the form of an optional template parameter, Base which defaults to default_call_policies. By convention, the precall function of the Base is invoked after the precall function supplied by the outer template, and the postcall function of the Base is invoked before the postcall function of the outer template. If a result_converter is supplied by the outer template, it replaces any result_converter supplied by the Base. For an example, see return_internal_reference.

ΪKԊԚͬһ趿ɵ瓃攏㖐ʹӃ核海Ӄ⟂ԣ썊 Boost.Python儵瓃⟂ԀģौṩKһ趁䊽퓿ڣ썊 ԈE샇穴閂ꏡ㍊ բ趽ӿڲɓoĐΊ튇һ趿ɑᵄģॲΊ Base㬄숏Ϊ default_call_policiesᣍ കչ漲㬼code>Base 儠precall ꯊ픚͢⣄㰥̡駔Ġprecall ֮과/i>巔a썊 渠Basepostcall ꯊ퍊 Ԛ͢⣄㰥儠postcall ꯊ>֮ǰ巔a㍊ ȧ黍ⲣģौṩK result_converter㬍 ˼Ἡ>̦뻼/i> Base ̡駔Ĉκ΍ result_converterᣍ =컠return_internal_reference

Concept Requirements

腄Ҫdz

CallPolicies腄h3>

In the table below, x denotes an object whose type P is a model of CallPolicies, a denotes a PyObject* pointing to a Python argument tuple object, and r denotes a PyObject* referring to a "preliminary" result object.

Ԛς᭖켣ode>x ʇ趶ԏ㣬 ˼儀Ѝ P ʇ֖巔o߂ԵĄ㐍㬍 a ʇָϲPython⎊픪ש攏㵄 PyObject*㬍 渠r ʇָϲһ趡೵⽵ġὡ黶ԏ㵄 PyObject*

Expression Type Result/Semantics
x.precall(a) convertible to bool returns false and PyErr_Occurred() != 0 upon failure, true otherwise.
P::result_converter A model of ResultConverterGenerator. An MPL unary Metafunction Class used produce the "preliminary" result object.
x.postcall(a, r) convertible to PyObject* 0 0 and PyErr_Occurred() != 0 upon failure. Must "conserve references" even in the event of an exception. In other words, if r is not returned, its reference count must be decremented; if another existing object is returned, its reference count must be incremented.
P::extract_return_type A model of Metafunction. An MPL unary Metafunction used extract the return type from a given signature. By default it is derived from mpl::front.

᭴ Ѝ 믓
x.precall(a) 껻擭bool ʧ܊᷵똠false㬍 ⢇ҠPyErr_Occurred() != 0㬍 籔⎪ true᣼/td>
P::result_converter һ֖ ResultConverterGeneratorģЍ MPLһԪ Metafunction 㬍 ӃӚ⺉ꡰ㵲ᱽṻ攏㡣
x.postcall(a, r) 껻彼code>PyObject* ʧ܊Ꭺ 0㬍 ⢇ҠPyErr_Occurred() != 0ᣍ ᘐ롰ᣴ撽Ӄᱣ켴ʹʇԚҬ㣊¼ᣍ 뻾仰˵㬈繻 r δ絻أ썊 ˼儒퓃솊坼婿 ȧ黷廘Kmһ趏ִ涔ϳ㬍 ˼儒퓃솊地檔
P::extract_return_type һ֖ Metafunction ģЍ᣼/td> ʹӃMPLһԪ Metafunction 䓸趨ǩuʽ֐̡ȡ絻؀Ѝᣍ ĬȏΪ mpl::front ד ᣼/td>

Models of CallPolicies are required to be CopyConstructible.

巔o߂Ԅ㐍Ҫdzʇ鹼淨㨃opyConstructible㩼/a>ᣍ


Revised 13 November, 2002

© Copyright Dave Abrahams 2002.

筒룺켯a>

ҫ΄輐£결08.6.15

Permission to copy, use, modify, sell and distribute this software is granted provided this copyright notice appears in all copies. This software is provided "as is" without express or implied warranty, and with no claim as to its suitability for any purpose.