我正在尝试使用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/