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