Skip to content

Commit 5ea2a9a

Browse files
committed
PyBindConverter : Add new class for PyBind/Boost interoperability
This is essentially the same as the one we've used in Gaffer for interfacing with OpenColorIO bindings for a little while now.
1 parent d293bc9 commit 5ea2a9a

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

Changes

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
10.5.x.x (relative to 10.5.4.2)
22
========
33

4+
Features
5+
--------
6+
7+
- PyBindConverter : Added new class providing interoperability between Boost Python bindings and PyBind11 bindings.
8+
49
Improvements
510
------------
611

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
2+
//////////////////////////////////////////////////////////////////////////
3+
//
4+
// Copyright (c) 2024, Cinesite VFX Ltd. All rights reserved.
5+
//
6+
// Redistribution and use in source and binary forms, with or without
7+
// modification, are permitted provided that the following conditions are
8+
// met:
9+
//
10+
// * Redistributions of source code must retain the above
11+
// copyright notice, this list of conditions and the following
12+
// disclaimer.
13+
//
14+
// * Redistributions in binary form must reproduce the above
15+
// copyright notice, this list of conditions and the following
16+
// disclaimer in the documentation and/or other materials provided with
17+
// the distribution.
18+
//
19+
// * Neither the name of John Haddon nor the names of
20+
// any other contributors to this software may be used to endorse or
21+
// promote products derived from this software without specific prior
22+
// written permission.
23+
//
24+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25+
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
26+
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27+
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28+
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29+
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30+
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31+
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32+
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33+
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34+
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35+
//
36+
//////////////////////////////////////////////////////////////////////////
37+
38+
#ifndef IECOREPYTHON_PYBINDCONVERTER_H
39+
#define IECOREPYTHON_PYBINDCONVERTER_H
40+
41+
#include "boost/python.hpp"
42+
43+
#include "pybind11/pybind11.h"
44+
45+
namespace IECorePython
46+
{
47+
48+
// Registers `boost::python` converters for types
49+
// wrapped using PyBind11.
50+
template<typename T>
51+
struct PyBindConverter
52+
{
53+
54+
static void registerConverters()
55+
{
56+
boost::python::to_python_converter<T, ToPyBind>();
57+
boost::python::converter::registry::push_back(
58+
&FromPyBind::convertible,
59+
&FromPyBind::construct,
60+
boost::python::type_id<T>()
61+
);
62+
}
63+
64+
private :
65+
66+
struct ToPyBind
67+
{
68+
static PyObject *convert( const T &t )
69+
{
70+
pybind11::object o = pybind11::cast( t );
71+
Py_INCREF( o.ptr() );
72+
return o.ptr();
73+
}
74+
};
75+
76+
struct FromPyBind
77+
{
78+
79+
static void *convertible( PyObject *object )
80+
{
81+
pybind11::handle handle( object );
82+
return handle.cast<T>() ? object : nullptr;
83+
}
84+
85+
static void construct( PyObject *object, boost::python::converter::rvalue_from_python_stage1_data *data )
86+
{
87+
void *storage = ( ( boost::python::converter::rvalue_from_python_storage<T> * ) data )->storage.bytes;
88+
T *t = new( storage ) T;
89+
data->convertible = storage;
90+
91+
pybind11::handle handle( object );
92+
*t = handle.cast<T>();
93+
}
94+
95+
};
96+
97+
};
98+
99+
} // namespace IECorePython
100+
101+
#endif // IECOREPYTHON_PYBINDCONVERTER_H
102+

0 commit comments

Comments
 (0)