1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
#pragma once
#include <Standard_Handle.hxx>
#include <type_traits>
#include <memory>
namespace py = pybind11;
PYBIND11_DECLARE_HOLDER_TYPE(T, opencascade::handle<T>, true);
using opencascade::handle;
template <typename CppException>
void register_occ_exception(py::handle scope,
const char *name){
static py::exception<CppException> ex;
if (!ex) ex = py::exception<CppException>(scope, name);
py::register_exception_translator([](std::exception_ptr p) {
if (!p) return;
try {
std::rethrow_exception(p);
} catch (const CppException &e) {
ex(e.GetMessageString());
}
});
};
template <typename T, typename Ptr>
inline void register_default_constructor(py::module m, const char* name){
if constexpr (std::is_constructible<T>::value){
static_cast<py::class_<T ,Ptr>>(m.attr(name))
.def(py::init([]() { return new T; }));
}
};
template <typename T>
inline void copy_if_copy_constructible(T& t1, T& t2){
if constexpr (std::is_copy_constructible<T>::value){
t1 = t2;
}
};
template<typename T, template<typename> typename Deleter = std::default_delete>
struct shared_ptr : public std::shared_ptr<T>{
explicit shared_ptr(T* t = nullptr) : std::shared_ptr<T>(t, Deleter<T>()) {};
void reset(T* t = nullptr) { std::shared_ptr<T>::reset(t, Deleter<T>()); };
};
template<typename T> struct nodelete{
void operator()(T* p) const {};
};
template<typename T> using shared_ptr_nodelete = shared_ptr<T,nodelete>;
PYBIND11_DECLARE_HOLDER_TYPE(T, shared_ptr<T>);
PYBIND11_DECLARE_HOLDER_TYPE(T, shared_ptr_nodelete<T>);
#include "pystreambuf.h"
|