<div dir="ltr">Sorry about the in-complete message. I sent it earlier but did not get it posted. The question is about how to Get XPtr out from underlying Rcpp S4 object, and how to construct such object from C++ side.<div><br></div><div>Tianqi</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Oct 6, 2015 at 2:40 PM, Tianqi Chen <span dir="ltr"><<a href="mailto:tianqi.tchen@gmail.com" target="_blank">tianqi.tchen@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div style="font-size:12.8px">Dear Rcpp Developers:</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">```c++</div><div style="font-size:12.8px">class MyClass {</div><div style="font-size:12.8px">public:</div><div style="font-size:12.8px">   static void SaveList(SEXP list, const std::string& filename) {</div><div style="font-size:12.8px">          Rcpp::List data_lst(list);</div><div style="font-size:12.8px">          std::vector<MyClass *> internals(data_lst[i]);</div><div style="font-size:12.8px">          for (size_t i = 0; i < data_lst.size(); ++i) {</div><div style="font-size:12.8px">               SEXP obj = data_lst[i];</div><div style="font-size:12.8px">               // Need to Get internal pointers from S4 MyClass object</div><div style="font-size:12.8px">               internals[i] = GetPtr<MyClass>(obj);</div><div style="font-size:12.8px">          }</div><div style="font-size:12.8px">          SomeRealSave(internals, filename);</div><div style="font-size:12.8px">   }</div><div style="font-size:12.8px">   static void LoadList(const std::string& filename) {</div><div style="font-size:12.8px">          std::vector<MyClass > internals;</div><div style="font-size:12.8px">          SomeReadLoad(&internals);</div><div style="font-size:12.8px">          Rcpp::List out(internals.size());</div><div style="font-size:12.8px">          for (size_t i = 0; i < internals.size(); ++i) {</div><div style="font-size:12.8px">              // need to construct a MyClass object from C++ side</div><div style="font-size:12.8px">              out[i] = MyClassConstructor(internals[i]);</div><div style="font-size:12.8px">          }</div><div style="font-size:12.8px">         return out;</div><div style="font-size:12.8px">   }</div><div style="font-size:12.8px">};</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">RCPP_MODULE(MyClass) {</div><div style="font-size:12.8px">     class_<MyClass>("MyClass");</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">     function("save.myclass.list", &<span style="font-size:12.8px">MyClass</span>::SaveList);</div><div style="font-size:12.8px">     function("load.myclass.list", &<span style="font-size:12.8px">MyClass</span>::LoadList);</div><div style="font-size:12.8px">}</div><div style="font-size:12.8px">```</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px"><span style="font-size:12.8px">To be specific, We have ways to Save/Load the pointers to the MyClass object (in the SomeRealSave and SomeRealLoad). In our specific use-case, It was an Array on GPU.</span></div><div style="font-size:12.8px"><br><div style="font-size:12.8px">So the question is how is there any solution to <span style="font-size:12.8px"> </span><span style="font-size:12.8px">GetPtr<MyClass> and </span><span style="font-size:12.8px">MyClassConstructor, so we can get the internal pointer of MyClass from SEXP(which is the S4 object), and construct the corresponding S4 object given the information of MyClass.</span></div><div style="font-size:12.8px"><span style="font-size:12.8px"><br></span></div><div style="font-size:12.8px"><span style="font-size:12.8px"><br></span></div><div style="font-size:12.8px"><span style="font-size:12.8px">What was wanted is indeed possible if we choose to only expose XPtr<MyClass>, so we can simply do </span></div><div style="font-size:12.8px"><span style="font-size:12.8px">```</span></div><div style="font-size:12.8px"><span style="font-size:12.8px">    MyClass * GetPtr<MyClass>(SEXP obj) {</span></div><div style="font-size:12.8px"><span style="font-size:12.8px">         XPtr<MyClass>(obj).get();</span></div><div style="font-size:12.8px"><span style="font-size:12.8px">    } </span><span style="font-size:12.8px"><br></span></div><div style="font-size:12.8px"><span style="font-size:12.8px">    </span><span style="font-size:12.8px">MyClassConstructor(MyClass *ptr) {</span></div><div style="font-size:12.8px"><span style="font-size:12.8px">        return XPtr<MyClass>(ptr); </span></div><div style="font-size:12.8px"><span style="font-size:12.8px">   } </span></div><div style="font-size:12.8px"><span style="font-size:12.8px"><br></span></div><div style="font-size:12.8px"><span style="font-size:12.8px">```</span></div><div style="font-size:12.8px"><span style="font-size:12.8px"><br></span></div><div style="font-size:12.8px"><span style="font-size:12.8px">But it would be really nice we can directly return a wrapped S4 object instead of raw ptr</span></div><span class="HOEnZb"><font color="#888888"><div style="font-size:12.8px"><span style="font-size:12.8px"><br></span></div><div style="font-size:12.8px"><span style="font-size:12.8px">Tianqi</span></div></font></span></div></div>
</blockquote></div><br></div>