mirror of
https://github.com/boostorg/python.git
synced 2026-01-19 16:32:16 +00:00
166 lines
5.3 KiB
HTML
166 lines
5.3 KiB
HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<meta name="generator" content="HTML Tidy, see www.w3.org">
|
|
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
|
<link rel="stylesheet" type="text/css" href="../boost.css">
|
|
|
|
<title>Boost.Python - <boost/python/from_python.hpp></title>
|
|
|
|
<table border="0" cellpadding="7" cellspacing="0" width="100%" summary=
|
|
"header">
|
|
<tr>
|
|
<td valign="top" width="300">
|
|
<h3><a href="../../../../index.htm"><img height="86" width="277" alt=
|
|
"C++ Boost" src="../../../../c++boost.gif" border="0"></a></h3>
|
|
|
|
<td valign="top">
|
|
<h1 align="center"><a href="../index.html">Boost.Python</a></h1>
|
|
|
|
<h2 align="center">Header <boost/python/from_python.hpp></h2>
|
|
</table>
|
|
<hr>
|
|
|
|
<h2>Contents</h2>
|
|
|
|
<dl class="page-index">
|
|
<dt><a href="#introduction">Introduction</a>
|
|
|
|
<dt><a href="#classes">Classes</a>
|
|
|
|
<dd>
|
|
<dl class="page-index">
|
|
<dt><a href="#from_python-spec">Class
|
|
Template<code>from_python</code></a>
|
|
|
|
<dd>
|
|
<dl class="page-index">
|
|
<dt><a href="#from_python-spec-synopsis">Class Template
|
|
<code>from_python</code> synopsis</a>
|
|
|
|
<dt><a href="#from_python-spec-ctors">Class Template
|
|
<code>from_python</code> constructor</a>
|
|
|
|
<dt><a href="#from_python-spec-observers">Class Template
|
|
<code>from_python</code> observer functions</a>
|
|
</dl>
|
|
</dl>
|
|
|
|
<dt><a href="#examples">Example</a>
|
|
</dl>
|
|
<hr>
|
|
|
|
<h2><a name="introduction"></a>Introduction</h2>
|
|
|
|
<p><code><boost/python/from_python.hpp></code> introduces a class
|
|
template <code>from_python<T></code> for extracting a C++ object of
|
|
type <code>T</code> from a Python object.
|
|
|
|
<h2><a name="classes"></a>Classes</h2>
|
|
|
|
<h3><a name="from_python-spec"></a>Class Template
|
|
<code>from_python<class T></code></h3>
|
|
|
|
<p><code>from_python<T></code> is the type used internally by
|
|
Boost.Python to extract C++ function arguments from a Python argument tuple
|
|
when calling a wrapped function. It can also be used directly to make
|
|
similar conversions in other contexts.
|
|
|
|
<h4><a name="from_python-spec-synopsis"></a>Class Template
|
|
<code>from_python</code> synopsis</h4>
|
|
<pre>
|
|
namespace boost { namespace python
|
|
{
|
|
template <class T>
|
|
struct from_python : private <a href=
|
|
"../../../utility/utility.htm#Class_noncopyable">boost::noncopyable</a> // Exposition only.
|
|
// from_python<T> meets the NonCopyable requirements
|
|
{
|
|
from_python(PyObject*);
|
|
bool convertible() const;
|
|
<i>convertible-to-T</i> operator()(PyObject*) const;
|
|
};
|
|
}
|
|
</pre>
|
|
|
|
<h4><a name="from_python-spec-ctors"></a>Class Template
|
|
<code>from_python</code> constructor</h4>
|
|
<pre>
|
|
from_python(PyObject* p);
|
|
</pre>
|
|
|
|
<dl class="function-semantics">
|
|
<dt><b>Requires:</b> <code>p != 0</code>
|
|
|
|
<dt><b>Effects:</b> Constructs a <code>from_python</code> object suitable
|
|
for extracting a C++ object of type <code>T</code> from <code>p</code>.
|
|
</dl>
|
|
|
|
<h4><a name="from_python-spec-observers"></a>Class Template
|
|
<code>from_python</code> observer functions</h4>
|
|
<pre>
|
|
bool convertible() const;
|
|
</pre>
|
|
|
|
<dl class="function-semantics">
|
|
<dt><b>Returns:</b> <code>false</code> if the conversion cannot succeed.
|
|
This indicates that either:
|
|
|
|
<dd>
|
|
<ol>
|
|
<li>No <code>from_python_converter</code> was registered for
|
|
<code>T</code>, or
|
|
|
|
<li>any such converter rejected the constructor argument
|
|
<code>p</code> by returning <code>0</code> from its
|
|
<code>convertible()</code> function
|
|
</ol>
|
|
Note that conversion may still fail in <code>operator()</code> due to
|
|
an exception.
|
|
|
|
<dt><b>Throws:</b> nothing
|
|
|
|
<dt><b>Rationale:</b> Because <code>from_python<></code> is used in
|
|
overload resolution, and throwing an exception can be slow, it is useful
|
|
to be able to rule out a broad class of unsuccessful conversions without
|
|
throwing an exception.
|
|
</dl>
|
|
<pre>
|
|
<i>convertible-to-T</i> operator()(PyObject* p) const;
|
|
</pre>
|
|
|
|
<dl class="function-semantics">
|
|
<dt><b>Requires:</b> <code>*p</code> refers to the same object which was
|
|
passed to the constructor, and <code>convertible()</code> returns
|
|
<code>true</code>.
|
|
|
|
<dt><b>Effects:</b> performs the conversion
|
|
|
|
<dt><b>Returns:</b> an object convertible to <code>T</code>.
|
|
</dl>
|
|
|
|
<h2><a name="examples"></a>Example</h2>
|
|
<pre>
|
|
#include <string>
|
|
#include <boost/python/from_python.hpp>
|
|
|
|
// If a std::string can be extracted from p, return its
|
|
// length. Otherwise, return 0.
|
|
std::size_t length_if_string(PyObject* p)
|
|
{
|
|
from_python<std::string> converter(p);
|
|
if (!converter.convertible())
|
|
return 0;
|
|
else
|
|
return converter(p).size();
|
|
}
|
|
</pre>
|
|
|
|
<p>Revised
|
|
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->
|
|
13 November, 2002
|
|
<!--webbot bot="Timestamp" endspan i-checksum="39359" -->
|
|
|
|
|
|
<p><i>© Copyright <a href="../../../../people/dave_abrahams.htm">Dave
|
|
Abrahams</a> 2002. All Rights Reserved.</i>
|
|
|