...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
return_opaque_pointer
return_opaque_pointer
synopsisreturn_opaque_pointer
metafunctionsreturn_opaque_pointer
return_opaque_pointer
is a model of
ResultConverterGenerator
which can be used to wrap C++ functions returning pointers to
undefined types such that the return value is copied into a
new Python object.
In addition to specifying the return_opaque_pointer
policy the
BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID
macro must be
used to define specializations for the
type_id function
on the type pointed to by returned pointer.
return_opaque_pointer
synopsisnamespace boost { namespace python { struct return_opaque_pointer { template <class R> struct apply; }; }}
return_opaque_pointer
metafunctionstemplate <class R> struct apply
# include <boost/python/return_opaque_pointer.hpp> # include <boost/python/def.hpp> # include <boost/python/module.hpp> # include <boost/python/return_value_policy.hpp> typedef struct opaque_ *opaque; opaque the_op = ((opaque) 0x47110815); opaque get () { return the_op; } void use (opaque op) { if (op != the_op) throw std::runtime_error (std::string ("failed")); } void failuse (opaque op) { if (op == the_op) throw std::runtime_error (std::string ("success")); } BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID(opaque_) namespace bpl = boost::python; BOOST_PYTHON_MODULE(opaque_ext) { bpl::def ( "get", &::get, bpl::return_value_policy<bpl::return_opaque_pointer>()); bpl::def ("use", &::use); bpl::def ("failuse", &::failuse); }
""" >>> from opaque_ext import * >>> # >>> # Check for correct conversion >>> use(get()) >>> failuse(get()) Traceback (most recent call last): ... RuntimeError: success >>> # >>> # Check that there is no conversion from integers ... >>> use(0) Traceback (most recent call last): ... TypeError: bad argument type for built-in operation >>> # >>> # ... and from strings to opaque objects >>> use("") Traceback (most recent call last): ... TypeError: bad argument type for built-in operation """ def run(args = None): import sys import doctest if args is not None: sys.argv = args return doctest.testmod(sys.modules.get(__name__)) if __name__ == '__main__': print "running..." import sys sys.exit(run()[0])
Revised 28 January, 2003
© Copyright 2003 Haufe Mediengruppe. All Rights Reserved.