|
|
Boost.PythonHeader <boost/python/lvalue_from_python.hpp> |
lvalue_from_python
lvalue_from_python synopsis
lvalue_from_python constructor
get_member
get_member synopsis
get_member static functions
<boost/python/lvalue_from_python.hpp> supplies
a facility for extracting C++ objects from within instances of a
given Python type. This is typically useful for dealing with
"traditional" Python extension types.
lvalue_from_pythonClass template lvalue_from_python will register
from_python converters which extract a references and pointers to
a C++ type which is held within an object of a given Python
type. Its template arguments are:
| Parameter | Requirements | Description | Default |
|---|---|---|---|
python_type
| A compile-time constant PyTypeObject*
| The Python type of instances convertible by this converter. Python subtypes are also convertible. | |
Value
| A non-reference type. | The C++ type to be extracted | |
PythonObject
| initial sizeof(PyObject) bytes are
layout-compatible with PyObject.
| The C++ type used to hold Python instances of
python_type.
| Value
|
Extract
| Value& v = Extract::execute(x);
| A type whose static execute function extracts a Value reference from within an object of type PythonObject.
| An unspecified type whose execute function consists of return x;
|
PythonObject as a
whole.
If the lifetime of the lvalue_from_python object ends
before the last attempt to convert to one its target types, the
behavior is undefined. The easiest way to ensure correct behavior
is to declare an lvalue_from_python instance as a
static local in a module init
function, as shown in the example
below.
lvalue_from_python synopsis
namespace boost { namespace python
{
template <
PyTypeObject const* python_type
, class Value
, class PythonObject = Value
, class Extract = unspecified
>
class lvalue_from_python
{
lvalue_from_python();
};
}}
lvalue_from_python constructorlvalue_from_python();
Value&, Value const&,
Value*, or Value const* from Python
objects of type python_type using
Extract::execute().
get_memberget_member can be used with
lvalue_from_python in the common case where the C++
type to be extracted is a member of the Python object.
get_member synopsis
namespace boost { namespace python
{
template <class Class, class Member, Member (Class::*mp)>
struct get_member
{
static Member& execute(Class& c);
};
}}
get_member static functionsMember& execute(Class& c);
c.*mp
Noddys. Since
noddy_NoddyObject is so simple that it carries no
interesting information, the example is a bit contrived: it assumes
you want to keep track of one particular object for some reason.
#include <boost/python/reference.hpp>
#include <boost/python/module.hpp>
// definition lifted from the Python docs
typedef struct {
PyObject_HEAD
} noddy_NoddyObject;
using namespace boost::python;
static ref cache;
bool is_cached(noddy_NoddyObject* x)
{
return x == cache.get();
}
void set_cache(noddy_NoddyObject* x)
{
cache.reset((PyObject*)x, ref::increment_count);
}
BOOST_PYTHON_MODULE_INIT(noddy_cache)
{
module noddy_cache("noddy_cache")
.def("is_cached", is_cached)
.def("set_cache", set_cache)
;
}
>>> import noddy >>> n = noddy.new_noddy() >>> import noddy_cache >>> noddy_cache.is_cached(n) 0 >>> noddy_cache.set_cache(n) >>> noddy_cache.is_cached(n) 1 >>> noddy_cache.is_cached(noddy.new_noddy()) 0
Revised 05 November, 2001
© Copyright Dave Abrahams 2002. All Rights Reserved.