c++ - 包装C++类时类型不完整的GLFWwindow

标签 c++ boost-python

我正在尝试使用Boost::Python从Python中的Application C++基类派生,并且正在努力包装GLFW回调,尤其是由于GLFWwindow* window参数。
这是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()
编译:
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;


感谢您对如何解决此问题的任何反馈。

最佳答案

错误来自template <class T> inline type_info type_id()通过...

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
    >
{
};
...并且最初来自pointer_deep_arg_to_python<Ptr>::pointer_deep_arg_to_python(Ptr x)
要将GLFWwindow *注册为opaque pointer,请在#include之后插入以下内容。
BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID(GLFWwindow)

关于c++ - 包装C++类时类型不完整的GLFWwindow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62669860/

相关文章:

c++ - 分部分读取文件,在最后读取的部分之后继续

c++ - 在开源项目中存储 OAuth secret

c++ - python/c++ - 使用 cmake 编译共享库并使用 distutils 安装

python - 使用 CMake 构建与平台无关的目录结构

boost - 使用 Boost Python 将 Python 列表输入到接受向量的函数中

c++ - 在 Boost.Python : wrong type 中的类中存储和检索指针时出现 ArgumentError

c++ - 为 posix recv 设置超时会导致丢失 udp 数据包吗?

c++ - 模板化函数以从输入参数推断返回类型 STL 容器

c++ - 将 C++ 缓冲区公开为 Python 3 字节

c++ - 为什么 C++ 链接器不提示缺少这些函数的定义?