EvolvingObjects
|
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 // #ifndef WIN32 00025 // #include <config.h> 00026 // #endif 00027 00028 #include <boost/python.hpp> 00029 #include <sstream> 00032 template <class T> 00033 struct T_pickle_suite : boost::python::pickle_suite 00034 { 00035 static 00036 std::string print_to_string(const T& t) 00037 { 00038 std::ostringstream os; 00039 t.printOn(os); 00040 os << std::ends; 00041 return os.str(); 00042 } 00043 00044 static 00045 boost::python::tuple getstate(const T& t) 00046 { 00047 std::string s = print_to_string(t); 00048 return boost::python::make_tuple( boost::python::str(s)); 00049 } 00050 00051 static 00052 void setstate(T& t, boost::python::tuple pickled) 00053 { 00054 std::string s = boost::python::extract<std::string>(pickled[0]); 00055 std::istringstream is(s); 00056 t.readFrom(is); 00057 } 00058 }; 00059 00063 template <class Persistent, class X1, class X2, class X3> 00064 boost::python::class_<Persistent, X1, X2, X3>& pickle(boost::python::class_<Persistent, X1, X2, X3>& c) 00065 { 00066 return c.def_pickle(T_pickle_suite<Persistent>()) 00067 .def("__str__", T_pickle_suite<Persistent>::print_to_string); 00068 } 00069 00070 #endif