Search code examples
rrcpp

Rcpp error: static assertion failed: cannot convert type to SEXP, why?


Error

I can't understand why I'm having this compilation error using Rcpp (version 1.0.2):

From file /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

enter image description here

Source of the error

I'm trying to wrap this C function glfwSetKeyCallback from the GLFW library.

And I know that the error is stemming somehow from the CPP source file below but I don't understand what I am doing wrong:

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);

}

EDIT

The source code below that compiles the function glfw_destroy_window and exports it as glfwDestroyWindow uses the type GLFWwindow_ptr and compiles and works from R just fine.

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);
}

EDIT 2

This function also works fine:

#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);
}

EDIT 3

This seems to be working...

The new 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);

}

Any help is greatly appreciated!


Solution

  • #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);
    
    }