Rcpp 错误:静态断言失败:无法将类型转换为 SEXP,为什么?

植物

错误

我不明白为什么使用 Rcpp(1.0.2 版)时出现此编译错误:

从文件/home/rmagno/R/x86_64-pc-linux-gnu-library/3.6/Rcpp/include/Rcpp/internal/wrap.h

Line 523 static assertion failed: cannot convert type to SEXP

在此处输入图片说明

错误来源

我正在尝试从 GLFW 库中包装这个 C 函数glfwSetKeyCallback

而且我知道错误是以某种方式源于下面的 CPP 源文件,但我不明白我做错了什么:

glfw_types.h

#ifndef RCPP_GLFW_TYPES_H
#define RCPP_GLFW_TYPES_H

#include <Rcpp.h>
#include <GLFW/glfw3.h>

// https://stackoverflow.com/questions/41210595/s4-object-with-a-pointer-to-a-c-struct

typedef Rcpp::XPtr<GLFWwindow, Rcpp::PreserveStorage, glfwDestroyWindow> GLFWwindow_ptr;

#endif

glfw_set_key_callback.cpp

#include "glfw_types.h"
using namespace Rcpp;

namespace {
  std::unique_ptr<Rcpp::Function> key_callback_func_ptr;
}

void glfw_set_key_callback_wrapper(GLFWwindow* window, int key, int scancode, int action, int modes)
{
  (*key_callback_func_ptr)(window, key, scancode, action, modes);
}

// [[Rcpp::export]]
void glfw_set_key_callback(GLFWwindow_ptr window, Rcpp::Function key_callback) {

  key_callback_func_ptr = std::make_unique<Rcpp::Function>(key_callback);
  glfwSetKeyCallback((GLFWwindow*)window, (GLFWkeyfun) glfw_set_key_callback_wrapper);

}

编辑

下面的源代码编译函数glfw_destroy_window并将其导出为glfwDestroyWindow使用类型,GLFWwindow_ptr然后编译并从 R 中正常工作。

glfwDestroyWindow.cpp

#include "glfw_types.h"
using namespace Rcpp;

//' @export
// [[Rcpp::export("glfwDestroyWindow")]]
void glfw_destroy_window(GLFWwindow_ptr window) {
  glfwDestroyWindow((GLFWwindow*)window);
  R_ClearExternalPtr(window);
}

编辑 2

此功能也可以正常工作:

#include "glfw_types.h"
using namespace Rcpp;

//' @export
// [[Rcpp::export("glfwCreateWindow")]]
GLFWwindow_ptr glfw_create_window(int width, int height, std::string title) {
  const char *title_c = title.c_str();
  return GLFWwindow_ptr(glfwCreateWindow(width, height, title_c, NULL, NULL), true);
}

编辑 3

这似乎有效...

新的 glfw_set_key_callback.cpp

#include "glfw_types.h"
using namespace Rcpp;

namespace {
  std::unique_ptr<Rcpp::Function> key_callback_func_ptr;
}

void glfw_set_key_callback_wrapper(GLFWwindow* window, int key, int scancode, int action, int modes)
{
  (*key_callback_func_ptr)(GLFWwindow_ptr(window, true), key, scancode, action, modes);
}

// [[Rcpp::export]]
void glfw_set_key_callback(GLFWwindow_ptr window, Rcpp::Function key_callback) {

  key_callback_func_ptr = std::make_unique<Rcpp::Function>(key_callback);
  glfwSetKeyCallback((GLFWwindow*)window, (GLFWkeyfun) glfw_set_key_callback_wrapper);

}

任何帮助是极大的赞赏!

植物
#include "glfw_types.h"
using namespace Rcpp;

namespace {
  std::unique_ptr<Rcpp::Function> key_callback_func_ptr;
}

void glfw_set_key_callback_wrapper(GLFWwindow* window, int key, int scancode, int action, int modes)
{
  (*key_callback_func_ptr)(GLFWwindow_ptr(window, false), key, scancode, action, modes);
}

// [[Rcpp::export]]
void glfw_set_key_callback(GLFWwindow_ptr window, Rcpp::Function key_callback) {

  key_callback_func_ptr = std::make_unique<Rcpp::Function>(key_callback);
  glfwSetKeyCallback((GLFWwindow*)window, (GLFWkeyfun) glfw_set_key_callback_wrapper);

}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章