On 6 October 2015 at 14:40, Tianqi Chen 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.
As I recall, we had several discussions here a few years ago about the impossibility of serialising Module objects to disk. In short, you can't. You write out a bunch of ram which you can re-read, but that doesn't make an object. You will have to rebuild the objects at the next run time so that memory is allocated and correctly pointed to. See here for one such prior email: http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2014-May/007539.html and an older one http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2011-April/002107.html | 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 Yes, it would be -- but that doesn't meam it is possible under the current framework. Sorry, Dirk -- http://dirk.eddelbuettel.com | @eddelbuettel | e...@debian.org _______________________________________________ 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