1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- [section boost/python/return_opaque_pointer.hpp]
- [section Class `return_opaqe_pointer`]
- return_opaque_pointer is a model of [link concepts.resultconverter.resultconvertergenerator_concept 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 [link to_from_python_type_conversion.boost_python_opaque_pointer_conv.macro_boost_python_opaque_specia `BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID`] macro must be used to define specializations for the [link utility_and_infrastructure.boost_python_type_id_hpp.functions `type_id`] function on the type pointed to by returned pointer.
- ``
- namespace boost { namespace python
- {
- struct return_opaque_pointer
- {
- template <class R> struct apply;
- };
- }}
- ``
- [endsect]
- [section Class `return_opaque_pointer` metafunctions]
- ``template <class T> struct apply``
- [variablelist
- [[Returns][`detail::opaque_conversion_holder<R> type;`]]
- ]
- [endsect]
- [section Example]
- In C++:
- ``
- # 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);
- }
- ``
- Python code:
- ``
- """
- >>> 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])
- ``
- [endsect]
- [endsect]
|