From edd93c80a1f15c35b9a2b0dfd550d66b8eb9e521 Mon Sep 17 00:00:00 2001 From: Dave Abrahams Date: Sat, 16 Feb 2002 15:42:09 +0000 Subject: [PATCH] inital checkin [SVN r12835] --- doc/v2/to_python_converter.html | 189 ++++++++++++++++++++++++++++++++ 1 file changed, 189 insertions(+) create mode 100644 doc/v2/to_python_converter.html diff --git a/doc/v2/to_python_converter.html b/doc/v2/to_python_converter.html new file mode 100644 index 00000000..63db8c71 --- /dev/null +++ b/doc/v2/to_python_converter.html @@ -0,0 +1,189 @@ + + + + + + Boost.Python - <boost/python/to_python_converter.hpp> + + + +
+

+

+ +
+

Boost.Python

+ +

Header <boost/python/to_python_converter.hpp>

+
+
+ +

Contents

+ +
+
Introduction + + +
Classes + +
+
+
Class Template to_python_converter + +
+
+ +
Class Template + to_python_converter synopsis + +
Class Template + to_python_converter constructor +
+
+ +
Example +
+
+ +

Introduction

+ + to_python_converter registers a conversion from + objects of a given C++ type into a Python object. + +

Classes

+ +

Class template to_python_converter

+ + to_python_converter adds a wrapper around a static + member function of its second template parameter, handling + low-level details such as insertion into the converter registry. + + + + + + + +
+ to_python_converter template parameters
+In the table below, x denotes an object of type T +
Parameter + + Requirements + + Description + +
T + + + + The C++ type of the source object in the conversion + +
Conversion + + PyObject* p = Conversion::convert(x),
+ if p == 0, PyErr_Occurred() != 0. + +
A class type whose static member function + convert does the real work of the conversion. + +
+ +

Class template to_python_converter synopsis

+
+namespace boost { namespace python
+{
+  template <class T, class Conversion>
+  struct to_python_converter
+  {
+      to_python_converter();
+  };
+}}
+
+ +

Class template to_python_converter constructor

+
+to_python_converter();
+
+ +
+ +
Effects: Registers a to_python converter which uses + Conversion::convert() to do its work. + +
+ +

Example

+ +This example presumes that someone has implemented the standard noddy example +module from the Python documentation, and placed the corresponding +declarations in "noddy.h". Because +noddy_NoddyObject is the ultimate trivial extension type, +the example is a bit contrived: it wraps a function for which all +information is contained in the type of its return value. + +

C++ module definition

+ +
+#include <boost/python/reference.hpp>
+#include <boost/python/module.hpp>
+#include "noddy.h"
+
+struct tag {};
+tag make_tag() { return tag(); }
+
+using namespace boost::python;
+
+struct tag_to_noddy
+{
+    static PyObject* convert(tag const& x)
+    {
+        return PyObject_New(noddy_NoddyObject, &noddy_NoddyType);
+    }
+};
+
+BOOST_PYTHON_MODULE_INIT(to_python_converter)
+{
+    module to_python("to_python_converter")
+        .def("make_tag", make_tag)
+        ;
+    to_python_converter<tag, tag_to_noddy>();
+}
+
+ +

Python code

+ +
+>>> import to_python_converter
+>>> def always_none():
+...     return None
+...
+>>> def choose_function(x):
+...     if (x % 2 != 0):
+...         return to_python_converter.make_tag
+...     else:
+...         return always_none
+...
+>>> a = [ choose_function(x) for x in range(5) ]
+>>> b = [ f() for f in a ]
+>>> type(b[0])
+<type 'NoneType'>
+>>> type(b[1])
+<type 'Noddy'>
+>>> type(b[2])
+<type 'NoneType'>
+>>> type(b[3])
+<type 'Noddy'>
+
+ +

Revised + + 05 November, 2001 + + + +

© Copyright Dave + Abrahams 2002. All Rights Reserved. +