fixed (?) some problems with stringstreams and std::ends

This commit is contained in:
maartenkeijzer 2003-03-21 02:39:09 +00:00
commit eaabc7ae3b
10 changed files with 57 additions and 50 deletions

View file

@ -21,7 +21,7 @@
Contact: todos@geneura.ugr.es, http://geneura.ugr.es Contact: todos@geneura.ugr.es, http://geneura.ugr.es
Marc.Schoenauer@polytechnique.fr Marc.Schoenauer@polytechnique.fr
mak@dhi.dk mak@dhi.dk
CVS Info: $Date: 2003-02-27 19:25:48 $ $Header: /home/nojhan/dev/eodev/eodev_cvs/eo/src/eoVariableLengthCrossover.h,v 1.8 2003-02-27 19:25:48 okoenig Exp $ $Author: okoenig $ CVS Info: $Date: 2003-03-21 02:38:57 $ $Header: /home/nojhan/dev/eodev/eodev_cvs/eo/src/eoVariableLengthCrossover.h,v 1.9 2003-03-21 02:38:57 maartenkeijzer Exp $ $Author: maartenkeijzer $
*/ */
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -150,10 +150,17 @@ public :
virtual std::string className() const virtual std::string className() const
{ {
#ifdef HAVE_SSTREAM
std::ostringstream os;
os << "eoVlAtomExchangeQuadOp(" << atomExchange.className() << ")";
return os.str()
#else
char s[1024]; char s[1024];
std::ostrstream os(s, 1022); std::ostrstream os(s, 1022);
os << "eoVlAtomExchangeQuadOp(" << atomExchange.className() << ")" << std::ends; os << "eoVlAtomExchangeQuadOp(" << atomExchange.className() << ")" << std::ends;
return std::string(s); return std::string(s);
#endif
} }
private: private:

View file

@ -21,7 +21,7 @@
Contact: todos@geneura.ugr.es, http://geneura.ugr.es Contact: todos@geneura.ugr.es, http://geneura.ugr.es
Marc.Schoenauer@polytechnique.fr Marc.Schoenauer@polytechnique.fr
mak@dhi.dk mak@dhi.dk
CVS Info: $Date: 2003-02-27 19:25:48 $ $Version$ $Author: okoenig $ CVS Info: $Date: 2003-03-21 02:38:57 $ $Version$ $Author: maartenkeijzer $
*/ */
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -141,10 +141,16 @@ public :
virtual std::string className() const virtual std::string className() const
{ {
#ifdef HAVE_SSTREAM
std::ostringstream os;
os << "eoVlDelMutation("<<chooser.className() << ")";
return os.str();
#else
char s[1024]; char s[1024];
std::ostrstream os(s, 1022); std::ostrstream os(s, 1022);
os << "eoVlDelMutation(" << chooser.className() << ")" << std::ends; os << "eoVlDelMutation(" << chooser.className() << ")" << std::ends;
return std::string(s); return std::string(s);
#endif
} }
private: private:

View file

@ -53,6 +53,7 @@ std::string rng_to_string(const eoRng& _rng)
return os.str(); return os.str();
} }
void rng_from_string(eoRng& _rng, std::string s) void rng_from_string(eoRng& _rng, std::string s)
{ {
#ifdef HAVE_SSTREAM #ifdef HAVE_SSTREAM
@ -63,6 +64,21 @@ void rng_from_string(eoRng& _rng, std::string s)
_rng.readFrom(is); _rng.readFrom(is);
} }
struct RNG_pickle_suite : boost::python::pickle_suite
{
static
boost::python::tuple getstate(const eoRng& _rng)
{
return boost::python::make_tuple(str(rng_to_string(_rng)));
}
static
void setstate(eoRng& _rng, boost::python::tuple pickled)
{
std::string state = extract<std::string>(pickled[0]);
rng_from_string(_rng, state);
}
};
int spin(eoRng& _rng, numeric::array values, double total) int spin(eoRng& _rng, numeric::array values, double total)
{ {
if (total == 0.0) if (total == 0.0)
@ -97,6 +113,7 @@ void random_numbers()
.def("to_string", rng_to_string) .def("to_string", rng_to_string)
.def("from_string", rng_from_string) .def("from_string", rng_from_string)
.def("roulette_wheel", spin) .def("roulette_wheel", spin)
.def_pickle(RNG_pickle_suite())
; ;
def("rng", get_rng, return_value_policy<reference_existing_object>()); def("rng", get_rng, return_value_policy<reference_existing_object>());

View file

@ -69,28 +69,7 @@ class TestPickling(unittest.TestCase):
for i in range(10): for i in range(10):
rng().rand() rng().rand()
filename = tempfile.mktemp() rng2 = self.do_pickle(rng())
file = open(filename, 'wb')
pickler = cPickle.Pickler(file)
s = rng().to_string()
pickler.dump(s);
del pickler
file.close()
file = open(filename)
unpickler = cPickle.Unpickler(file)
s = unpickler.load()
rng2 = eoRng(1)
rng2.from_string(s)
del unpickler
file.close()
os.remove(filename)
for i in range(100): for i in range(100):
a = rng().rand() a = rng().rand()
@ -99,19 +78,7 @@ class TestPickling(unittest.TestCase):
def vParam(self,v): def vParam(self,v):
filename = tempfile.mktemp() v2 = self.do_pickle(v);
file = open(filename,'wb')
pickler = cPickle.Pickler(file)
pickler.dump(v)
del pickler
file.close()
file = open(filename)
unpickler = cPickle.Unpickler(file)
v2 = unpickler.load();
self.failUnlessEqual(v.value, v2.value) self.failUnlessEqual(v.value, v2.value)
def testValueParam(self): def testValueParam(self):

View file

@ -97,12 +97,13 @@ public :
{ {
#ifdef HAVE_SSTREAM #ifdef HAVE_SSTREAM
std::ostringstream oscount; std::ostringstream oscount;
oscount << counter;
#else #else
char buff[255]; char buff[255];
std::ostrstream oscount(buff, 254); std::ostrstream oscount(buff, 254);
#endif
oscount << counter; oscount << counter;
oscount << std::ends; oscount << std::ends;
#endif
currentFileName = dirname + "/" + filename + oscount.str(); currentFileName = dirname + "/" + filename + oscount.str();
} }

View file

@ -106,12 +106,13 @@ inline void eoGnuplot::initGnuPlot(std::string _title, std::string _extra)
{ {
#ifdef HAVE_SSTREAM #ifdef HAVE_SSTREAM
std::ostringstream os; std::ostringstream os;
os << "300x200-0+" << numWindow*220;
#else #else
char snum[255]; char snum[255];
std::ostrstream os(snum, 254); std::ostrstream os(snum, 254);
os << "300x200-0+" << numWindow*220 << std::ends;
#endif #endif
os << "300x200-0+" << numWindow*220 << std::ends;
numWindow++; numWindow++;
char *args[6]; char *args[6];
args[0] = strdup( "gnuplot" ); args[0] = strdup( "gnuplot" );
@ -148,7 +149,7 @@ inline void eoGnuplot::initGnuPlot(std::string _title, std::string _extra)
* Created......: Mon Mar 13 13:50:11 1995 * Created......: Mon Mar 13 13:50:11 1995
* Description..: Communication par pipe bidirectionnel avec un autre process * Description..: Communication par pipe bidirectionnel avec un autre process
* *
* Ident........: $Id: eoGnuplot.h,v 1.7 2003-02-28 16:49:14 maartenkeijzer Exp $ * Ident........: $Id: eoGnuplot.h,v 1.8 2003-03-21 02:39:09 maartenkeijzer Exp $
* ---------------------------------------------------------------------- * ----------------------------------------------------------------------
*/ */

View file

@ -120,10 +120,10 @@ inline void eoGnuplot1DMonitor::FirstPlot()
os << ", "; os << ", ";
} }
os << '\n'; os << '\n';
os << std::ends;
#ifdef HAVE_SSTREAM #ifdef HAVE_SSTREAM
PipeComSend( gpCom, os.str().c_str()); PipeComSend( gpCom, os.str().c_str());
#else #else
os << std::ends;
PipeComSend( gpCom, buff ); PipeComSend( gpCom, buff );
#endif #endif
} }

View file

@ -132,9 +132,9 @@ public:
virtual void printOn(std::ostream& _os) const virtual void printOn(std::ostream& _os) const
{ {
if (combien == 0) if (combien == 0)
_os << 100*rate << "% " << std::ends; _os << 100*rate << "% ";
else else
_os << combien << " " << std::ends; _os << combien << " ";
return; return;
} }

View file

@ -164,12 +164,13 @@ public :
{ {
#ifdef HAVE_SSTREAM #ifdef HAVE_SSTREAM
std::ostringstream os; std::ostringstream os;
os << repValue;
#else #else
char buf[1024]; char buf[1024];
std::ostrstream os(buf, 1023); std::ostrstream os(buf, 1023);
#endif
os << repValue; os << repValue;
os << std::ends; os << std::ends;
#endif
return os.str(); return os.str();
} }
@ -221,11 +222,12 @@ std::string eoValueParam<std::pair<double, double> >::getValue(void) const
// use own buffer as MSVC's buffer leaks! // use own buffer as MSVC's buffer leaks!
#ifdef HAVE_SSTREAM #ifdef HAVE_SSTREAM
std::ostringstream os; std::ostringstream os;
os << repValue.first << ' ' << repValue.second;
#else #else
char buff[1024]; char buff[1024];
std::ostrstream os(buff, 1024); std::ostrstream os(buff, 1024);
#endif
os << repValue.first << ' ' << repValue.second << std::ends; os << repValue.first << ' ' << repValue.second << std::ends;
#endif
return os.str(); return os.str();
} }
@ -260,7 +262,9 @@ std::string eoValueParam<std::vector<std::vector<double> > >::getValue(void) con
std::copy(repValue[i].begin(), repValue[i].end(), std::ostream_iterator<double>(os, " ")); std::copy(repValue[i].begin(), repValue[i].end(), std::ostream_iterator<double>(os, " "));
} }
#ifndef HAVE_SSTREAM
os << std::ends; os << std::ends;
#endif
return os.str(); return os.str();
} }
@ -302,7 +306,9 @@ std::string eoValueParam<std::vector<double> >::getValue(void) const
#endif #endif
os << repValue.size() << ' '; os << repValue.size() << ' ';
std::copy(repValue.begin(), repValue.end(), std::ostream_iterator<double>(os, " ")); std::copy(repValue.begin(), repValue.end(), std::ostream_iterator<double>(os, " "));
#ifndef HAVE_SSTREAM
os << std::ends; os << std::ends;
#endif
return os.str(); return os.str();
} }
@ -334,7 +340,9 @@ std::string eoValueParam<std::vector<eoMinimizingFitness> >::getValue(void) cons
#endif #endif
os << repValue.size() << ' '; os << repValue.size() << ' ';
std::copy(repValue.begin(), repValue.end(), std::ostream_iterator<eoMinimizingFitness>(os, " ")); std::copy(repValue.begin(), repValue.end(), std::ostream_iterator<eoMinimizingFitness>(os, " "));
#ifndef HAVE_SSTREAM
os<< std::ends; os<< std::ends;
#endif
return os.str(); return os.str();
} }

View file

@ -70,7 +70,7 @@ void operator()(const eoPop<EOT>& _pop)
for (unsigned i = 0; i < howmany; ++i) for (unsigned i = 0; i < howmany; ++i)
{ {
std::ostringstream os; std::ostringstream os;
os << _pop[i] << std::endl << std::ends; os << _pop[i] << std::endl;
// paranoid: // paranoid:
value() += os.str(); value() += os.str();
@ -130,8 +130,8 @@ public :
unsigned howMany=combien?combien:_pop.size(); unsigned howMany=combien?combien:_pop.size();
for (unsigned i = 0; i < howMany; ++i) for (unsigned i = 0; i < howMany; ++i)
{ {
std::ostringstream os; // leave space for emergency terminate std::ostringstream os;
os << *_pop[i] << std::endl << std::ends; os << *_pop[i] << std::endl;
// paranoid: // paranoid:
value() += os.str(); value() += os.str();