Exposing a C struct with Rcpp?

寵の児 提交于 2019-12-22 14:59:36

问题


I am looking for advice on how to write a wrapper package for the GLFW C library using Rcpp.

I have already started, and I've managed to wrap a few simple functions from the GLFW library.

Now I was looking at the function glfwCreateWindow :

GLFWwindow* glfwCreateWindow(int width,
                             int height,
                             const char* title,
                             GLFWmonitor* monitor,
                             GLFWwindow* share) 

As can be seen from its prototype, it accepts these arguments that are pointers to structs. Because this function needs to be used by the user, these objects also have to be exposed to the user in R. I would like to expose these objects as S4 classed objects. I read the Rccp vignette on Rcpp Modules that looked very promising. However, I am not entirely sure how to apply them to C structs...?

From what I could find:

  • There is this similar question on SO: How to expose a C structure from C library to R with Rcpp, however it's been six years since this question has been posted so I'm wondering if there has been any update on this? This question is further expanded in this thread: http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2013-July/006249.html;
  • I also found this example of an R package wrapping a C library in the Rcpp gallery: https://gallery.rcpp.org/articles/accessing-xts-api/, but unfortunately only functions are exposed using Modules, i.e., no C structs are exposed.

So my understanding right now is that I'll have to use the macro RCPP_EXPOSED_CLASS but I could not fully understand what it is doing.

Do you recommend creating C++ classes for all those C structs and then use Rcpp Modules to expose them?

Do you know of a package that does this in an idiomatic way that I could study from?

Thank you!

PS. Please do not flag this question as a duplicate immediately as there might had been recent developments.

来源:https://stackoverflow.com/questions/58148581/exposing-a-c-struct-with-rcpp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!