Search code examples
c++boost-python

incomplete type GLFWwindow when wrapping a C++ class


I am trying to derive from an Application C++ base class in Python with Boost::Python and am struggling with wrapping the GLFW callbacks, particularly due to the GLFWwindow* window argument.

Here's the MCVE:

#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>

#include <boost/python.hpp>

class Application
{
public:
    Application();
    GLFWwindow* window;
    virtual int onKeyPressed(GLFWwindow*, int, int, int , int);
};

Application *app;

Application::Application()
{
    glfwInit();
    window = glfwCreateWindow(800.0, 600.0, "example", NULL, NULL);
    glfwMakeContextCurrent(window);
}

int Application::onKeyPressed(GLFWwindow *window, int key, int scancode, int action, int mods)
{
    return 1;
}

struct ApplicationWrap : Application, boost::python::wrapper<Application>
{
    int onKeyPressed(GLFWwindow *window, int key, int scancode, int action, int mods)
    {
        return this->get_override("onKeyPressed")(window, key, scancode, action, mods);
    }
};

static void _onKeyPressed(GLFWwindow *window, int key, int scancode, int action, int mods)
{
    app->onKeyPressed(window, key, scancode, action, mods);
}


int main() {
    // app = new Application();
    // glfwSetKeyCallback(app->window, _onKeyPressed);
    // delete app;
    return 0;
}


BOOST_PYTHON_MODULE(example)
{
    namespace python = boost::python;

    python::class_<ApplicationWrap, boost::noncopyable>("Application")
    //.def("onKeyPressed", _onKeyPressed).staticmethod("_onKeyPressed")
    ;
}

// import example
// class DerivedApplication(example.Application):
//     def __init__(self):
//         example.Application.__init__(self)
//     def onKeyPressed(window):
//         print("successfully overrides example.Application.onKeyPressed.")

// DerivedApplication()

compiling with:

g++ -I/usr/include/python3.7 main.cpp -lglfw -lpython3.7 -lboost_python3

error: invalid use of incomplete type ‘struct GLFWwindow’ typeid(T) ^~~~~~~~~

note: forward declaration of ‘struct GLFWwindow’ typedef struct GLFWwindow GLFWwindow;

Thanks for any feedback on how this problem is to be solved.


Solution

  • The error comes from template <class T> inline type_info type_id() via...

    template <class T>
    struct registered_pointee
        : registered<
            typename boost::python::detail::remove_pointer<  // <-- fails for incomplete types
               typename boost::python::detail::remove_cv<
                  typename boost::python::detail::remove_reference<T>::type
               >::type
            >::type
        >
    {
    };
    

    ...and originally from pointer_deep_arg_to_python<Ptr>::pointer_deep_arg_to_python(Ptr x).

    To register GLFWwindow * as an opaque pointer, insert the following after the #include's.

    BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID(GLFWwindow)