|
Boost Users : |
From: Adam Stephens (jas5358_at_[hidden])
Date: 2008-05-29 08:59:13
I'd like to write a class in C++, then use a tool like SWIG (or
Boost.Python, if absolutely necessary) to make a shared library/Python
module. Then, I want to write python code that uses the boost.mpi
python module to pass messages containing instances of my C++ class.
In other words, I want to do what's described here:
"Boost.MPI also supports efficient serialization and transmission of
C++ objects (that have been exposed to Python) through its C++
interface. Any C++ type that provides (de-)serialization routines that
meet the requirements of the Boost.Serialization library is eligible
for this optimization, but the type must be registered in advance. To
register a C++ type, invoke the C++ function register_serialized. If
your C++ types come from other Python modules (they probably will!),
those modules will need to link against the boost_mpi and
boost_mpi_python libraries as described in the installation section.
Note that you do not need to link against the Boost.MPI Python
extension module."
( from http://www.boost.org/doc/libs/1_35_0/doc/html/mpi/python.html#mpi.python_user_data
)
I've looked at the documentation for the function register_serialized,
but I am evidentally using it incorrectly, because I get compile time
errors.
My class looks like this:
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/mpi/python/serialize.hpp>
class Testclass {
private:
static bool registered;
int data;
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version) {
ar & data;
}
public:
Testclass() {
if(!registered) {
registered = true;
boost::mpi::python::register_serialized(*this);
}
}
void set_data(const int & in) {
data = in;
}
int get_data() {
return data;
}
};
bool Testclass::registered = false;
I might be so far out in left field that what I'm trying to accomplish
here needs explanation. According to the only documentation I can
find, register_serialized is defined this way:
template<typename T>
void register_serialized(const T & value = T(), PyTypeObject * type = 0);
The parameters have this meaning:
type: "The Python type associated with the C++ type T. If not
provided, it will be computed from the same value value."
value: "A sample value of the type T. This may be used to compute the
Python type associated with the C++ type T"
So, I'm trying to register my class by passing in the object pointed
to by this. I figure I only need to register it once; hence the
static member.
When I try to compile this to an object file, I get this error:
/share/home/00791/adam/boost/include/boost-1_35/boost/mpi/python/serialize.hpp(323):
error: no operator "<<" matches these operands
operand types are: boost::mpi::packed_oarchive << Testclass
ar << value;
^
detected during:
instantiation of "void
boost::python::detail::direct_serialization_table<IArchiver,
OArchiver>::default_saver<T>::operator()(OArchiver &, const
boost::python::api::object &, unsigned int) [with
IArchiver=boost::mpi::packed_iarchive,
OArchiver=boost::mpi::packed_oarchive, T=Testclass]" at line 158 of
"/share/home/00791/adam/boost/include/boost-1_35/boost/function/function_template.hpp"
Followed by several lines of similar messages, which I can attach if
anyone needs to see them.
There's one final wrinkle.. I've been trying to build boost for the
past few days, and I kept getting a very simliar compile error at the
same point. One of the authors of Boost.MPI took a look at it and
submitted this change, which I made manually on my system:
http://svn.boost.org/trac/boost/changeset/45825
Only after that change was I able to build Boost.
Thanks
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net