|
|
Boost.PythonHeader <boost/python/lvalue_from_pytype.hpp> |
lvalue_from_pytype
lvalue_from_pytype synopsis
lvalue_from_pytype constructor
extract_identity
extract_identity synopsis
extract_identity static functions
extract_member
extract_member synopsis
extract_member static functions
<boost/python/lvalue_from_pytype.hpp> supplies
a facility for extracting C++ objects from within Python instances
of a given type. This is typically useful for dealing with
"traditional" Python extension types.
lvalue_from_pytypeClass template lvalue_from_pytype will register
from_python converters which, given an object of the given Python
type, can extract references and pointers to a particular C++
type. Its template arguments are:
| Parameter | Requirements | Semantics |
|---|---|---|
Extractor
| a model of Extractor whose execute function returns a reference type. | Extracts the lvalue from the Python object once its type has been confirmed |
python_type
| A compile-time constant PyTypeObject*
| The Python type of instances convertible by this converter. Python subtypes are also convertible. |
lvalue_from_pytype synopsis
namespace boost { namespace python
{
template <class Extractor, PyTypeObject const* python_type>
struct lvalue_from_pytype
{
lvalue_from_pytype();
};
}}
lvalue_from_pytype constructorlvalue_from_pytype();
Extractor::execute.
extract_identityextract_identity is a model of Extractor which can be
used in the common case where the C++ type to be extracted is the
same as the Python object type.
extract_identity synopsis
namespace boost { namespace python
{
template <class InstanceType>
struct extract_identity
{
static InstanceType& execute(InstanceType& c);
};
}}
extract_identity static functionsInstanceType& execute(InstanceType& c);
c
extract_memberextract_member is a model of Extractor which can be
used in the common case in the common case where the C++
type to be extracted is a member of the Python object.
extract_member synopsis
namespace boost { namespace python
{
template <class InstanceType, class MemberType, MemberType (InstanceType::*member)>
struct extract_member
{
static MemberType& execute(InstanceType& c);
};
}}
extract_member static functionsstatic MemberType& execute(InstanceType& c);
c.*member
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. This
module would have to be dynamically linked to the module which defines
noddy_NoddyType.
#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 reference<PyObject> 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)
;
// register Noddy lvalue converter
lvalue_from_pytype<extract_identity<noddy_NoddyObject>,&noddy_NoddyType>();
}
>>> 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.