Browse Source

python: make the PyNonDestructibleClass helper public.

We'll need it for GL holder types.
pull/2/head
Vladimír Vondruš 7 years ago
parent
commit
65d39b079e
  1. 11
      src/Magnum/Python.h
  2. 4
      src/python/magnum/CMakeLists.txt
  3. 55
      src/python/magnum/NonDestructible.h
  4. 9
      src/python/magnum/gl.cpp
  5. 11
      src/python/magnum/shaders.cpp

11
src/Magnum/Python.h

@ -48,6 +48,17 @@ template<class T> PyImageViewHolder<T> pyImageViewHolder(const T& view, pybind11
return PyImageViewHolder<T>{new T{view}, owner};
}
/* This is a variant of https://github.com/pybind/pybind11/issues/1178,
implemented on the client side instead of patching pybind itself */
template<class, bool> struct PyNonDestructibleBaseDeleter;
template<class T> struct PyNonDestructibleBaseDeleter<T, false> {
void operator()(T*) { CORRADE_ASSERT_UNREACHABLE(); /* LCOV_EXCL_LINE */ }
};
template<class T> struct PyNonDestructibleBaseDeleter<T, true> {
void operator()(T* ptr) { delete ptr; }
};
template<class T, class... Args> using PyNonDestructibleClass = pybind11::class_<T, Args..., std::unique_ptr<T, PyNonDestructibleBaseDeleter<T, std::is_destructible<T>::value>>>;
}
PYBIND11_DECLARE_HOLDER_TYPE(T, Magnum::PyImageViewHolder<T>)

4
src/python/magnum/CMakeLists.txt

@ -118,7 +118,9 @@ if(NOT MAGNUM_BUILD_STATIC)
if(Magnum_Shaders_FOUND)
pybind11_add_module(magnum_shaders SYSTEM ${magnum_shaders_SRCS})
target_include_directories(magnum_shaders PRIVATE ${PROJECT_SOURCE_DIR}/src/python)
target_include_directories(magnum_shaders PRIVATE
${PROJECT_SOURCE_DIR}/src
${PROJECT_SOURCE_DIR}/src/python)
target_link_libraries(magnum_shaders PRIVATE Magnum::Shaders)
set_target_properties(magnum_shaders PROPERTIES
FOLDER "python"

55
src/python/magnum/NonDestructible.h

@ -1,55 +0,0 @@
#ifndef magnum_NonDestructible_h
#define magnum_NonDestructible_h
/*
This file is part of Magnum.
Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019
Vladimír Vondruš <mosra@centrum.cz>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <memory>
#include <Corrade/Utility/Assert.h>
#include "magnum/bootstrap.h"
namespace pybind11 {
template<typename, typename ...> class class_;
}
/* This is a variant of https://github.com/pybind/pybind11/issues/1178,
implemented on the client side instead of patching pybind itself */
namespace magnum {
template<class, bool> struct NonDestructibleBaseDeleter;
template<class T> struct NonDestructibleBaseDeleter<T, false> {
void operator()(T*) { CORRADE_ASSERT_UNREACHABLE(); /* LCOV_EXCL_LINE */ }
};
template<class T> struct NonDestructibleBaseDeleter<T, true> {
void operator()(T* ptr) { delete ptr; }
};
template<class T> using NonDestructible = py::class_<T, std::unique_ptr<T, NonDestructibleBaseDeleter<T, std::is_destructible<T>::value>>>;
template<class T, class Base> using NonDestructibleBase = py::class_<T, Base, std::unique_ptr<T, NonDestructibleBaseDeleter<T, std::is_destructible<T>::value>>>;
}
#endif

9
src/python/magnum/gl.cpp

@ -44,7 +44,6 @@
#include "corrade/EnumOperators.h"
#include "magnum/bootstrap.h"
#include "magnum/NonDestructible.h"
#include "magnum/PyGL.h"
namespace magnum {
@ -60,7 +59,7 @@ void gl(py::module& m) {
m.doc() = "OpenGL wrapping layer";
/* Abstract shader program */
NonDestructible<GL::AbstractShaderProgram>{m,
PyNonDestructibleClass<GL::AbstractShaderProgram>{m,
"AbstractShaderProgram", "Base for shader program implementations"};
/** @todo more */
@ -291,7 +290,7 @@ void gl(py::module& m) {
.value("STENCIL", GL::FramebufferClear::Stencil);
corrade::enumOperators(framebufferClear);
NonDestructible<GL::AbstractFramebuffer> abstractFramebuffer{m,
PyNonDestructibleClass<GL::AbstractFramebuffer> abstractFramebuffer{m,
"AbstractFramebuffer", "Base for default and named framebuffers"};
abstractFramebuffer
@ -302,10 +301,10 @@ void gl(py::module& m) {
.def("read", static_cast<void(GL::AbstractFramebuffer::*)(const Range2Di&, const MutableImageView2D&)>(&GL::AbstractFramebuffer::read),
"Read block of pixels from the framebuffer to an image view");
NonDestructibleBase<GL::DefaultFramebuffer, GL::AbstractFramebuffer> defaultFramebuffer{m,
PyNonDestructibleClass<GL::DefaultFramebuffer, GL::AbstractFramebuffer> defaultFramebuffer{m,
"DefaultFramebuffer", "Default framebuffer"};
NonDestructibleBase<PyFramebuffer, GL::AbstractFramebuffer> framebuffer{m,
PyNonDestructibleClass<PyFramebuffer, GL::AbstractFramebuffer> framebuffer{m,
"Framebuffer", "Framebuffer"};
py::class_<GL::Framebuffer::ColorAttachment>{framebuffer, "ColorAttachment", "Color attachment"}

11
src/python/magnum/shaders.cpp

@ -29,15 +29,16 @@
#include <Magnum/Shaders/Phong.h>
#include <Magnum/Shaders/VertexColor.h>
#include "Magnum/Python.h"
#include "corrade/EnumOperators.h"
#include "magnum/bootstrap.h"
#include "magnum/NonDestructible.h"
namespace magnum {
namespace {
template<UnsignedInt dimensions> void vertexColor(NonDestructibleBase<Shaders::VertexColor<dimensions>, GL::AbstractShaderProgram>& c) {
template<UnsignedInt dimensions> void vertexColor(PyNonDestructibleClass<Shaders::VertexColor<dimensions>, GL::AbstractShaderProgram>& c) {
/* Attributes */
c.attr("COLOR3") = GL::DynamicAttribute{
GL::DynamicAttribute::Kind::Generic, 3,
@ -71,9 +72,9 @@ void shaders(py::module& m) {
/* 2D/3D vertex color shader */
{
NonDestructibleBase<Shaders::VertexColor2D, GL::AbstractShaderProgram> vertexColor2D{m,
PyNonDestructibleClass<Shaders::VertexColor2D, GL::AbstractShaderProgram> vertexColor2D{m,
"VertexColor2D", "2D vertex color shader"};
NonDestructibleBase<Shaders::VertexColor3D, GL::AbstractShaderProgram> vertexColor3D{m,
PyNonDestructibleClass<Shaders::VertexColor3D, GL::AbstractShaderProgram> vertexColor3D{m,
"VertexColor3D", "3D vertex color shader"};
vertexColor2D.attr("POSITION") = GL::DynamicAttribute{
GL::DynamicAttribute::Kind::Generic, 0,
@ -89,7 +90,7 @@ void shaders(py::module& m) {
/* Phong shader */
{
NonDestructibleBase<Shaders::Phong, GL::AbstractShaderProgram> phong{m,
PyNonDestructibleClass<Shaders::Phong, GL::AbstractShaderProgram> phong{m,
"Phong", "Phong shader"};
phong.attr("POSITION") = GL::DynamicAttribute{
GL::DynamicAttribute::Kind::Generic, 0,

Loading…
Cancel
Save