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.
Tianqi On Tue, Oct 6, 2015 at 2:40 PM, Tianqi Chen <tianqi.tc...@gmail.com> wrote: > Dear Rcpp Developers: > > ```c++ > class MyClass { > public: > static void SaveList(SEXP list, const std::string& filename) { > Rcpp::List data_lst(list); > std::vector<MyClass *> internals(data_lst[i]); > for (size_t i = 0; i < data_lst.size(); ++i) { > SEXP obj = data_lst[i]; > // Need to Get internal pointers from S4 MyClass object > internals[i] = GetPtr<MyClass>(obj); > } > SomeRealSave(internals, filename); > } > static void LoadList(const std::string& filename) { > std::vector<MyClass > internals; > SomeReadLoad(&internals); > Rcpp::List out(internals.size()); > for (size_t i = 0; i < internals.size(); ++i) { > // need to construct a MyClass object from C++ side > out[i] = MyClassConstructor(internals[i]); > } > return out; > } > }; > > RCPP_MODULE(MyClass) { > class_<MyClass>("MyClass"); > > function("save.myclass.list", &MyClass::SaveList); > function("load.myclass.list", &MyClass::LoadList); > } > ``` > > 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. > > So the question is how is there any solution to GetPtr<MyClass> and > 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. > > > What was wanted is indeed possible if we choose to only expose > XPtr<MyClass>, so we can simply do > ``` > MyClass * GetPtr<MyClass>(SEXP obj) { > XPtr<MyClass>(obj).get(); > } > MyClassConstructor(MyClass *ptr) { > return XPtr<MyClass>(ptr); > } > > ``` > > But it would be really nice we can directly return a wrapped S4 object > instead of raw ptr > > Tianqi >
_______________________________________________ Rcpp-devel mailing list Rcpp-devel@lists.r-forge.r-project.org https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel