3

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:

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.

Ramiro Magno
  • 3,085
  • 15
  • 30
  • 2
    Does https://stackoverflow.com/q/51110244/8416610 help? – Ralf Stubner Sep 28 '19 at 20:25
  • Thank you @RalfStubner, I missed that question! I'm going to take a careful look. – Ramiro Magno Sep 28 '19 at 20:30
  • 1
    That answer does help but I am not sure how to free the memory associated with that structure. – Ramiro Magno Sep 28 '19 at 21:20
  • 2
    There are over 1750 package on CRAN using Rcpp, and *many* wrap C libraries. The devil is however in the detail -- it depends on what you want done, on what the library does, but data persists between calls etc pp. So you can't expect a simple "do x, y and z and it is done" answer. – Dirk Eddelbuettel Sep 28 '19 at 21:29
  • Hi @DirkEddelbuettel: Thank you for your comments! I'm a newbie on Rcpp, and I only know C, so I'm just starting on C++ too, so I don't really know what to expect. I was just trying to get some pointers to learn more. And btw, indeed, in my case the data should persist. But I'll browse again github code with the Rcpp tag and search for packages wrapping C libraries (I must confess I've found mostly C++). – Ramiro Magno Sep 28 '19 at 21:37
  • 2
    You might also consider `Rcpp::Xptr` for holding pointers. This allows for custom cleanup code. – Ralf Stubner Sep 29 '19 at 06:55

0 Answers0