This commit is contained in:
maartenkeijzer 2003-03-18 09:35:46 +00:00
commit cde5b5d22b
5 changed files with 50 additions and 11 deletions

View file

@ -25,7 +25,7 @@
# #
CXX = g++ #-3.2 CXX = g++ #-3.2
CXXFLAGS = #-g #-DNDEBUG CXXFLAGS = -DHAVE_SSTREAM#-g #-DNDEBUG
CPPFLAGS = -Wall -O2 #-g #-O2 CPPFLAGS = -Wall -O2 #-g #-O2
LDFLAGS = LDFLAGS =
COMPILE = $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c COMPILE = $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c
@ -41,7 +41,7 @@ OBJECTS=eoFunctorStore.o PyEO.o abstract1.o algos.o \
all: PyEO/PyEO.so all: PyEO/PyEO.so
clean: clean:
rm *.so *.o test/*.pyc rm PyEO/*.so *.o test/*.pyc
PyEO/PyEO.so: $(OBJECTS) PyEO/PyEO.so: $(OBJECTS)
$(LINK) -o PyEO/PyEO.so $(OBJECTS) -lboost_python -lpython2.2 -shared -lstlport $(LINK) -o PyEO/PyEO.so $(OBJECTS) -lboost_python -lpython2.2 -shared -lstlport

View file

@ -21,7 +21,13 @@
#include <eoPop.h> #include <eoPop.h>
#include "PyEO.h" #include "PyEO.h"
#ifdef HAVE_SSTREAM
#include <sstream>
#else
#include <strstream>
#endif
using namespace std;
// static member, needs to be instantiated somewhere // static member, needs to be instantiated somewhere
std::vector<int> PyFitness::objective_info; std::vector<int> PyFitness::objective_info;
@ -76,7 +82,7 @@ struct pyPop_pickle_suite : boost::python::pickle_suite
for (unsigned i = 0; i != _pop.size(); ++i) for (unsigned i = 0; i != _pop.size(); ++i)
entries.append( PyEO_pickle_suite::getstate(_pop[i]) ); entries.append( PyEO_pickle_suite::getstate(_pop[i]) );
return make_tuple(object(_pop.size()), entries); return boost::python::make_tuple(object(_pop.size()), entries);
} }
static void setstate( eoPop<PyEO>& _pop, boost::python::tuple pickled) static void setstate( eoPop<PyEO>& _pop, boost::python::tuple pickled)
@ -85,7 +91,9 @@ struct pyPop_pickle_suite : boost::python::pickle_suite
boost::python::list entries = list(pickled[1]); boost::python::list entries = list(pickled[1]);
_pop.resize(sz); _pop.resize(sz);
for (unsigned i = 0; i != _pop.size(); ++i) for (unsigned i = 0; i != _pop.size(); ++i)
PyEO_pickle_suite::setstate(_pop[i], tuple(entries[i]) ); {
PyEO_pickle_suite::setstate(_pop[i], boost::python::tuple(entries[i]) );
}
} }
}; };
@ -93,11 +101,17 @@ struct pyPop_pickle_suite : boost::python::pickle_suite
template <class T> template <class T>
boost::python::str to_string(T& _p) boost::python::str to_string(T& _p)
{ {
#ifdef HAVE_SSTREAM
std::ostringstream os;
_p.printOn(os);
return str(os.str().c_str());
#else
std::ostrstream os; std::ostrstream os;
_p.printOn(os); _p.printOn(os);
os << ends; os << ends;
std::string s(os.str()); std::string s(os.str());
return str(s.c_str()); return str(s.c_str());
#endif
} }
void pop_sort(eoPop<PyEO>& pop) { pop.sort(); } void pop_sort(eoPop<PyEO>& pop) { pop.sort(); }

View file

@ -101,9 +101,9 @@ class PyFitness : public boost::python::object
return other.operator<(*this); return other.operator<(*this);
} }
//void printOn(std::ostream& os) const { const object& o = *this; os << o; } void printOn(std::ostream& os) const { const object& o = *this; boost::python::api::operator<<(os,o); }
//friend std::ostream& operator<<(std::ostream& os, const PyFitness& p) { p.printOn(os); return os; } friend std::ostream& operator<<(std::ostream& os, const PyFitness& p) { p.printOn(os); return os; }
//friend std::istream& operator>>(std::istream& is, PyFitness& p) { object o; is >> o; p = o; return is; } friend std::istream& operator>>(std::istream& is, PyFitness& p) { object o; is >> o; p = o; return is; }
}; };
struct PyEO : public EO< PyFitness > struct PyEO : public EO< PyFitness >
@ -117,7 +117,7 @@ struct PyEO : public EO< PyFitness >
void setGenome(object g) { genome = g; } void setGenome(object g) { genome = g; }
object genome; object genome;
std::string to_std::string() const std::string to_string() const
{ {
std::string result; std::string result;
result += extract<const char*>(str(getFitness())); result += extract<const char*>(str(getFitness()));

View file

@ -22,7 +22,11 @@
#define PICKLE_h #define PICKLE_h
#include <boost/python.hpp> #include <boost/python.hpp>
#ifdef HAVE_SSTREAM
#include <sstream>
#else
#include <strstream> #include <strstream>
#endif
/** Implements pickle support for eoPersistent derivatives */ /** Implements pickle support for eoPersistent derivatives */
@ -30,9 +34,13 @@ template <class T>
struct T_pickle_suite : boost::python::pickle_suite struct T_pickle_suite : boost::python::pickle_suite
{ {
static static
std::string print_to_std::string(const T& t) std::string print_to_string(const T& t)
{ {
#ifdef HAVE_SSTREAM
std::ostringstream os;
#else
std::ostrstream os; std::ostrstream os;
#endif
t.printOn(os); t.printOn(os);
os << std::ends; os << std::ends;
return os.str(); return os.str();
@ -41,7 +49,7 @@ struct T_pickle_suite : boost::python::pickle_suite
static static
boost::python::tuple getstate(const T& t) boost::python::tuple getstate(const T& t)
{ {
std::string s = print_to_std::string(t); std::string s = print_to_string(t);
return boost::python::make_tuple( boost::python::str(s)); return boost::python::make_tuple( boost::python::str(s));
} }
@ -49,7 +57,11 @@ struct T_pickle_suite : boost::python::pickle_suite
void setstate(T& t, boost::python::tuple pickled) void setstate(T& t, boost::python::tuple pickled)
{ {
std::string s = extract<std::string>(pickled[0]); std::string s = extract<std::string>(pickled[0]);
#ifdef HAVE_SSTREAM
std::istringstream is(s);
#else
std::istrstream is(s.c_str(), s.size()); std::istrstream is(s.c_str(), s.size());
#endif
t.readFrom(is); t.readFrom(is);
} }
}; };
@ -61,7 +73,7 @@ template <class Persistent, class X1, class X2, class X3>
boost::python::class_<Persistent, X1, X2, X3>& pickle(boost::python::class_<Persistent, X1, X2, X3>& c) boost::python::class_<Persistent, X1, X2, X3>& pickle(boost::python::class_<Persistent, X1, X2, X3>& c)
{ {
return c.def_pickle(T_pickle_suite<Persistent>()) return c.def_pickle(T_pickle_suite<Persistent>())
.def("__str__", T_pickle_suite<Persistent>::print_to_std::string); .def("__str__", T_pickle_suite<Persistent>::print_to_string);
} }
#endif #endif

View file

@ -21,7 +21,12 @@
#include <utils/eoRNG.h> #include <utils/eoRNG.h>
#include <boost/python.hpp> #include <boost/python.hpp>
#ifdef HAVE_SSTREAM
#include <sstream>
#else
#include <strstream> #include <strstream>
#endif
#include <boost/python/detail/api_placeholder.hpp> #include <boost/python/detail/api_placeholder.hpp>
using namespace boost::python; using namespace boost::python;
@ -36,7 +41,11 @@ double normal(eoRng& rng) { return rng.normal(); }
std::string rng_to_string(const eoRng& _rng) std::string rng_to_string(const eoRng& _rng)
{ {
#ifdef HAVE_SSTREAM
std::ostringstream os;
#else
std::ostrstream os; std::ostrstream os;
#endif
_rng.printOn(os); _rng.printOn(os);
os << std::ends; os << std::ends;
return os.str(); return os.str();
@ -44,7 +53,11 @@ std::string rng_to_string(const eoRng& _rng)
void rng_from_string(eoRng& _rng, std::string s) void rng_from_string(eoRng& _rng, std::string s)
{ {
#ifdef HAVE_SSTREAM
std::istringstream is(s);
#else
std::istrstream is(s.c_str(), s.size()); std::istrstream is(s.c_str(), s.size());
#endif
_rng.readFrom(is); _rng.readFrom(is);
} }