<div dir="ltr">Hi all,<div><br></div><div>I wrote a wrapper of hiredis, which is a minimalistic C client for the Redis database. Its name is `Rhiredis` and is much faster than rredis, an existed redis client of R. Please see <a href="http://rpubs.com/wush978/rhiredis">http://rpubs.com/wush978/rhiredis</a> for details. </div>

<div><br></div><div>Thanks for the Rcpp, it is much easier to wrap a C library into R and extend the features of R. </div><div><br></div><div>However, I still want to know if there is a better approach. I tried three approaches. Here is my opinions.</div>

<div><br></div><div># Preliminary</div><div><br></div><div>Given a 3rd party C library, it usually provides a C structure and functions of memory management. For example:</div><div><br></div><div>```c</div><div><div>struct A {</div>

<div>    int flag;</div><div>    // ...</div><div>};</div></div><div><br></div><div><div>A* initA();</div><div>void freeA(A* a);</div></div><div>```</div><div><br></div><div>The structure `A` need to be stored into a R object and pass to another function.</div>

<div>Also, the user might need to access `A.flag`. For simplicity, there is only one field in this example. However, there might be a number of fields in practice.</div><div><br></div><div># XPtr</div><div><br></div><div>

Thanks for the user guide of Rcpp modules , this is the first approach I tried.</div><div><br></div><div>We could expose `A` into R as follow:</div><div><br></div><div>```cpp</div><div>Rcpp::XPtr<A, freeA> a(initA());</div>

<div>```</div><div><br></div><div>However, we should write helper function to accessing `A.flag` for every fields.</div><div><br></div><div># RCPP_MODULE</div><div><br></div><div>The guids of Rcpp modules also provides another approach:</div>

<div><br></div><div>```cpp</div><div><div>RCPP_EXPOSED_CLASS(A)</div><div><br></div><div>RCPP_MODULE(A) {</div><div>  class_<A>("A")</div><div>  .field("flag", &A::flag)</div><div>  ;</div><div>

}</div></div><div><br></div><div><div>//'@export</div><div>//[[Rcpp::export]]</div><div>SEXP init() {</div><div>  BEGIN_RCPP</div><div>  return wrap(*initA());</div><div>  END_RCPP</div><div>}</div></div><div>```</div>

<div><br></div><div>This will produce a S4 class named `A` in R which stores C structure `A`.</div><div><br></div><div>However, it also produces memory leak because no `freeA` is called.</div><div><br></div><div>Adding `.finalizer(freeA)` in `RCPP_MODULE` will cause an error of freeing memory twice.<br>

</div><div><br></div><div># Embed `A` into C++ class and expose the class with RCPP_MODULE</div><div><br></div><div>This approach is implemented in `Rhiredis`.</div><div><br></div><div>Finally, I still need to write helper function to expose the field of `A`. But the user could access these flag in R with operator `$`.</div>

<div><br></div><div>Note that I still need a function to extract the pointer of `A` from exposed S4 object:</div><div><br></div><div>```cpp</div><div><div>template<class T></div><div>T* extract_ptr(SEXP s) {</div><div>

<span class="" style="white-space:pre">       </span>Rcpp::S4 s4(s);</div><div><span class="" style="white-space:pre">    </span>Rcpp::Environment env(s4);</div><div><span class="" style="white-space:pre"> </span>Rcpp::XPtr<T> xptr(env.get(".pointer"));</div>

<div><span class="" style="white-space:pre">    </span>return static_cast<T*>(R_ExternalPtrAddr(xptr));</div><div>}</div></div><div>```</div><div><br></div><div>Please give me some suggestion if you know a better or a different approach.</div>

<div><br></div><div>Thanks.</div><div><br></div><div>Wush</div></div>