# pickle.h

    00001 /*
    00002     PyEO
    00003
    00004     Copyright (C) 2003 Maarten Keijzer
    00005
    00006     This program is free software; you can redistribute it and/or modify
    00007     it under the terms of the GNU General Public License as published by
    00008     the Free Software Foundation; either version 2 of the License, or
    00009     (at your option) any later version.
    00010
    00011     This program is distributed in the hope that it will be useful,
    00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00014     GNU General Public License for more details.
    00015
    00016     You should have received a copy of the GNU General Public License
    00017     along with this program; if not, write to the Free Software
    00018     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    00019 */
    00020
    00021 #ifndef PICKLE_H
    00022 #define PICKLE_h
    00023
    00024 #include <config.h>
    00025
    00026 #include <boost/python.hpp>
    00027 #include <sstream>
    00031 template <class T>
    00032 struct T_pickle_suite : boost::python::pickle_suite
    00033 {
    00034     static
    00035     std::string print_to_string(const T& t)
    00036     {
    00037         std::ostringstream os;
    00038         t.printOn(os);
    00039         os << std::ends;
    00040         return os.str();
    00041     }
    00042
    00043     static
    00044     boost::python::tuple getstate(const T& t)
    00045     {
    00046         std::string s = print_to_string(t);
    00047         return boost::python::make_tuple( boost::python::str(s));
    00048     }
    00049
    00050     static
    00051     void setstate(T& t, boost::python::tuple pickled)
    00052     {
    00053         std::string s = boost::python::extract<std::string>(pickled[0]);
    00054         std::istringstream is(s);
    00055         t.readFrom(is);
    00056     }
    00057 };
    00058
    00062 template <class Persistent, class X1, class X2, class X3>
    00063 boost::python::class_<Persistent, X1, X2, X3>& pickle(boost::python::class_<Persistent, X1, X2, X3>& c)
    00064 {
    00065     return c.def_pickle(T_pickle_suite<Persistent>())
    00066         .def("__str__", T_pickle_suite<Persistent>::print_to_string);
    00067 }
    00068
    00069 #endif