
On Fri, Oct 22, 2010 at 8:18 PM, Robert Valkenburg <rj.valkenburg@gmail.com>wrote:
On Sat, Oct 23, 2010 at 2:41 PM, James C. Sutherland < James.Sutherland@utah.edu> wrote:
Would you be willing to share (on-list or off-list) some of the code that you wrote to make this happen? I haven't done much with mex and frankly dread the thought. It would be great it I had an example to work from...
Sure, I'm away from my work for a few days but will send you an example when i get back. I will send it off-list because its really more about Matlab mex than boost serialisation.
Thanks. FWIW, I just hacked together a really simple test case that seems to work. I will post here for the benefit of anyone who may be interested in the future. The serialization library is *really* nice! Three files: - Test.h - Test_mex.cpp - creates a function that can be called from Matlab that un-serializes the object - Test.cpp - creates the archive. ================= <Test.h> ================== #ifndef Test_h #define Test_h #include <ostream> #ifdef BINARY_IO # include <boost/archive/text_oarchive.hpp> # include <boost/archive/text_iarchive.hpp> typedef boost::archive::text_oarchive OutputArchive; typedef boost::archive::text_iarchive InputArchive; #else # include <boost/archive/binary_oarchive.hpp> # include <boost/archive/binary_iarchive.hpp> typedef boost::archive::binary_oarchive OutputArchive; typedef boost::archive::binary_iarchive InputArchive; #endif struct Test { private: friend std::ostream& operator<<(std::ostream& os, const Test& t ); friend class boost::serialization::access; template<typename Archive> void serialize( Archive& ar, const unsigned int version ) { ar & i; ar & d; ar & *p; } public: int i; double d; double* p; Test() { p=new double; } Test( int _i, double _d ) { i=_i; d=_d; p = new double; *p=d*2; } ~Test(){ delete p; } }; std::ostream& operator<<( std::ostream& os, const Test& t ) { os << t.i << ", " << t.d << ", " << *(t.p) << std::endl; } #endif Test_h ======================== </Test.h> ==================== ======================= <Test_mex.cpp> =============== #include "mex.h" #include "Test.h" #include <iostream> #include <fstream> using namespace std; void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) { mexPrintf("Hello.\n"); Test t; ifstream in( "serialize.out",ios_base::in ); InputArchive ia(in); ia >> t; mexPrintf("%i\n",t.i); mexPrintf("%f\n",t.d); mexPrintf("%f\n",*(t.p)); } ================= </Test_mex.cpp> ================ =============== <Test.cpp> ================== #include <iostream> #include <fstream> #include "Test.h" using namespace std; int main() { Test t(1,0.1); { std::ofstream ofs("serialize.out"); OutputArchive oa(ofs); oa << t; } cout << "original : " << t; { ifstream ifs("serialize.out",ios_base::in); InputArchive ia(ifs); Test t2; cout << "ready to import..." << flush; ia >> t2; cout << "done" << endl; cout << "restored : " << t2; } } ================== </Test.cpp> ===================