<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Dec 8, 2014 at 10:34 AM, Dirk Eddelbuettel <span dir="ltr"><<a href="mailto:edd@debian.org" target="_blank">edd@debian.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
On 8 December 2014 at 09:58, Tim Keitt wrote:<br>
|<br>
|<br>
| On Sun, Dec 7, 2014 at 4:52 AM, Dirk Eddelbuettel <<a href="mailto:edd@debian.org">edd@debian.org</a>> wrote:<br>
|<br>
|<br>
|<br>
|     What you are after, if I understand it correctly, is (close to) a common<br>
|     and<br>
|     well studied idiom called 'RAII': Resource Acquisition is Initialization.<br>
|     Two<br>
|     quick links, and you will find many more:<br>
|<br>
|<br>
|<br>
| I don't know if this example is helpful. Any class that can be initialized with<br>
| an SEXP and has an SEXP member function can be used as the argument or return<br>
| type from an Rcpp function call. I wrote this wrapper to avoid dereferencing<br>
| passed null pointers and as a return type for a lot of functions where I want<br>
| an R NULL return if the pointer is null.<br>
<br>
Nice post for the r-devel list :)  But as you don't use Rcpp here...<br></blockquote><div><br></div><div>Touche...</div><div> <br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
In essence Rcpp::XPtr does this for you.  If you rewrote the code in terms of<br>
Rcpp::XPtr, it might make for a nice post for the Rcpp Gallery.<br></blockquote><div><br></div><div>When I eventually get back to R coding, I'll have to take another look at XPtr. I do not recall now why I did not use it. Perhaps I wanted a single point for checking null pointers transparently.</div><div><br></div><div>THK</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
Dirk<br>
<br>
| THK<br>
|<br>
|<br>
| class<br>
| RGDALHandleWrapper<br>
|                         {<br>
|                         public:<br>
|                         RGDALHandleWrapper(void* h)<br>
|                         : handle(h) {} // If initialized NULL will return NULL<br>
|                         SEXP<br>
|                         RGDALHandleWrapper(const SEXP h)<br>
|                         : handle(R_ExternalPtrAddr(h))<br>
|                         {<br>
|                         if ( !handle ) stop("Null pointer passed to function\n"<br>
|                         );<br>
|                         }<br>
|                         operator SEXP() const<br>
|                         {<br>
|                         return handle ? // Return NULL SEXP on NULL pointer<br>
|                         R_MakeExternalPtr(handle, R_NilValue, R_NilValue) :<br>
|                         R_NilValue;<br>
|                         }<br>
|                         void* operator*() const { return handle; }<br>
|                         private:<br>
|                         void* handle;<br>
|                         };<br>
|<br>
|  <br>
|<br>
|<br>
| --<br>
| <a href="http://www.keittlab.org/" target="_blank">http://www.keittlab.org/</a><br>
<span class="HOEnZb"><font color="#888888"><br>
--<br>
<a href="http://dirk.eddelbuettel.com" target="_blank">http://dirk.eddelbuettel.com</a> | @eddelbuettel | <a href="mailto:edd@debian.org">edd@debian.org</a><br>
</font></span></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr"><a href="http://www.keittlab.org/" target="_blank">http://www.keittlab.org/</a></div></div>
</div></div>