Merge branch 'master' of ssh://localhost:9001/gitroot/eodev/eodev

This commit is contained in:
Caner CANDAN 2010-08-18 12:16:13 +02:00
commit 9632d429dc
11 changed files with 709 additions and 37 deletions

View file

@ -16,7 +16,8 @@ SET (EO_SOURCES eoFunctorStore.cpp
eoPrintable.cpp
eoCtrlCContinue.cpp
eoParetoFitness.cpp
eoScalarFitnessAssembled.cpp)
eoScalarFitnessAssembled.cpp
eoSIGContinue.cpp)
ADD_LIBRARY(eo STATIC ${EO_SOURCES})

38
eo/src/eoSIGContinue.cpp Normal file
View file

@ -0,0 +1,38 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//-----------------------------------------------------------------------------
// eoSIGContinue.cpp
// (c) EEAAX 1996 - GeNeura Team, 1998 - Maarten Keijzer 2000
/*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
Marc.Schoenauer@polytechnique.fr
mak@dhi.dk
Johann Dréo <nojhan@gmail.com>
Caner Candan <caner@candan.fr>
*/
//-----------------------------------------------------------------------------
#ifdef _MSC_VER
// to avoid long name warnings
#pragma warning(disable:4786)
#endif
#include <signal.h>
#include <iostream>
// --- Global variables - but don't know what else to do - MS ---
bool existSIGContinue = false;
bool call_func = false;

90
eo/src/eoSIGContinue.h Normal file
View file

@ -0,0 +1,90 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//-----------------------------------------------------------------------------
// eoSIGContinue.h
// (c) EEAAX 1996 - GeNeura Team, 1998 - Maarten Keijzer 2000
/*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
Marc.Schoenauer@polytechnique.fr
mak@dhi.dk
Johann Dréo <nojhan@gmail.com>
Caner Candan <caner@candan.fr>
*/
//-----------------------------------------------------------------------------
// the same thing can probably be done in MS environement, but I hoave no way
// to test it so at the moment it is commented out when in MSVC
#ifndef eoSIGContinue_h
#define eoSIGContinue_h
#include <signal.h>
#include <eoContinue.h>
extern bool existSIGContinue;
extern bool call_func;
void set_bool(int)
{
call_func = true;
}
/**
Ctrl C handling: this eoContinue tells whether the user pressed Ctrl C
*/
template< class EOT>
class eoSIGContinue: public eoContinue<EOT>
{
public:
/// Ctor : installs the signal handler
eoSIGContinue(int sig, sighandler_t fct)
: _sig(sig), _fct(fct)
{
// First checks that no other eoSIGContinue does exist
if (existSIGContinue)
throw std::runtime_error("A signal handler is already defined!\n");
#ifndef _WINDOWS
#ifdef SIGQUIT
::signal( sig, set_bool );
existSIGContinue = true;
#endif
#endif
}
/** Returns false when the signal has been typed in reached */
virtual bool operator() ( const eoPop<EOT>& _vEO )
{
if (call_func)
{
_fct(_sig);
call_func = false;
}
return true;
}
virtual std::string className(void) const { return "eoSIGContinue"; }
private:
int _sig;
sighandler_t _fct;
};
#endif
// of MSVC comment-out

View file

@ -25,7 +25,9 @@ SET (EOUTILS_SOURCES eoData.cpp
eoStdoutMonitor.cpp
eoUpdater.cpp
make_help.cpp
pipecom.cpp)
pipecom.cpp
eoLogger.cpp
eoParserLogger.cpp)
ADD_LIBRARY(eoutils STATIC ${EOUTILS_SOURCES})

View file

@ -4,6 +4,8 @@
// eoHowMany_h.h
// Base class for choosing a number of guys to apply something from a popsize
// (c) Marc Schoenauer, 2000
// (c) Thales group, 2010 (Johann Dréo <johann.dreo@thalesgroup.com>)
/*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
@ -54,7 +56,7 @@
* MS 10/04/2002:
* Added the possibility to have a negative number -
* when treated as a number: returns then (size - combien)
* when treated as a number: returns then (size - count)
* Should not modify anything when a positive number is passed in the ctor
*
* MS 20/06/2002:
@ -66,6 +68,8 @@
#include <sstream>
#include <utils/eoLogger.h>
class eoHowMany : public eoPersistent
{
public:
@ -74,65 +78,71 @@ public:
@param _interpret_as_rate to tell whether the rate actually is a rate
*/
eoHowMany(double _rate = 0.0, bool _interpret_as_rate = true):
rate(_rate), combien(0)
rate(_rate), count(0)
{
if (_interpret_as_rate)
{
if (_rate<0)
{
rate = 1.0+_rate;
if (rate < 0) // was < -1
throw std::logic_error("rate<-1 in eoHowMany!");
}
if (_rate<0)
{
rate = 1.0+_rate;
if (rate < 0) // was < -1
throw std::logic_error("rate<-1 in eoHowMany!");
}
}
else
{
rate = 0.0; // just in case, but shoud be unused
combien = int(_rate); // negative values are allowed here
if (combien != _rate)
std::cerr << "Warning: Number was rounded in eoHowMany";
rate = 0.0; // just in case, but shoud be unused
count = int(_rate); // negative values are allowed here
if (count != _rate)
eo::log << eo::warnings << "Number was rounded in eoHowMany";
}
}
/** Ctor from an int - both from int and unsigned int are needed
* to avoid ambiguity with the Ctor from a double */
eoHowMany(int _combien) : rate(0.0), combien(_combien) {}
eoHowMany(int _count) : rate(0.0), count(_count) {}
/** Ctor from an unsigned int - both from int and unsigned int are needed
* to avoid ambiguity with the Ctor from a double */
eoHowMany(unsigned int _combien) : rate(0.0), combien(_combien) {}
eoHowMany(unsigned int _count) : rate(0.0), count(_count) {}
/// Virtual dtor. They are needed in virtual class hierarchies.
virtual ~eoHowMany() {}
/** Does what it was designed for
* - combien==0 : return rate*_size
* - count==0 : return rate*_size
* - else
* - combien>0 : return combien (regardless of _size)
* - combien<0 : return _size-|combien|
* - count>0 : return count (regardless of _size)
* - count<0 : return _size-|count|
*/
unsigned int operator()(unsigned int _size)
{
if (combien == 0)
if (count == 0)
{
return (unsigned int) (rate * _size);
unsigned int res = static_cast<unsigned int>( std::ceil( rate * _size ) );
if( res == 0 ) {
eo::log << eo::warnings << "Call to a eoHowMany instance returns 0 (rate=" << rate << ", size=" << _size << ")" << std::endl;
}
return res;
}
if (combien < 0)
if (count < 0)
{
unsigned int combloc = -combien;
if (_size<combloc)
throw std::runtime_error("Negative result in eoHowMany");
return _size-combloc;
unsigned int combloc = -count;
if (_size<combloc)
throw std::runtime_error("Negative result in eoHowMany");
return _size-combloc;
}
return unsigned(combien);
return unsigned(count);
}
virtual void printOn(std::ostream& _os) const
{
if (combien == 0)
if (count == 0)
_os << 100*rate << "% ";
else
_os << combien << " ";
_os << count << " ";
return;
}
@ -152,8 +162,8 @@ public:
size_t pos = _value.find('%');
if (pos < _value.size()) // found a %
{
interpret_as_rate = true;
_value.resize(pos); // get rid of %
interpret_as_rate = true;
_value.resize(pos); // get rid of %
}
std::istringstream is(_value);
@ -161,11 +171,11 @@ public:
// now store
if (interpret_as_rate)
{
combien = 0;
rate /= 100.0;
count = 0;
rate /= 100.0;
}
else
combien = int(rate); // and rate will not be used
count = int(rate); // and rate will not be used
// minimal check
if ( rate < 0.0 )
@ -175,16 +185,16 @@ public:
/** The unary - operator: reverses the computation */
eoHowMany operator-()
{
if (!combien) // only rate is used
if (!count) // only rate is used
rate = 1.0-rate;
else
combien = -combien;
count = -count;
return (*this);
}
private :
double rate;
int combien;
int count;
};

146
eo/src/utils/eoLogger.cpp Normal file
View file

@ -0,0 +1,146 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//-----------------------------------------------------------------------------
// eoLogger.h
// (c) Thales group, 2010
/*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Authors:
Johann Dréo <johann.dreo@thalesgroup.com>
Caner Candan <caner.candan@thalesgroup.com>
*/
//-----------------------------------------------------------------------------
#include <unistd.h>
#include <fcntl.h>
#include <cstdlib>
#include <cstdio> // used to define EOF
#include <iostream>
#include <sstream>
#include <exception>
#include <stdexcept>
#include <locale>
#include "eoLogger.h"
eoLogger eo::log;
eoLogger::eoLogger()
: std::ostream(&_obuf),
_selectedLevel(eo::progress), _contexLevel(eo::quiet),
_fd(2), _obuf(_fd, _contexLevel, _selectedLevel)
{
_standard_io_streams[&std::cout] = 1;
_standard_io_streams[&std::clog] = 2;
_standard_io_streams[&std::cerr] = 2;
addLevel("quiet", eo::quiet);
addLevel("errors", eo::errors);
addLevel("warnings", eo::warnings);
addLevel("progress", eo::progress);
addLevel("logging", eo::logging);
addLevel("debug", eo::debug);
}
eoLogger::~eoLogger()
{}
std::string eoLogger::className() const
{
return ("eoLogger");
}
void eoLogger::addLevel(std::string name, eo::Levels level)
{
_levels[name] = level;
_sortedLevels.push_back(name);
}
void eoLogger::printLevels() const
{
std::cout << "Available verbose levels:" << std::endl;
for (std::vector<std::string>::const_iterator it = _sortedLevels.begin(), end = _sortedLevels.end();
it != end; ++it)
{
std::cout << "\t" << *it << std::endl;
}
::exit(0);
}
eoLogger& operator<<(eoLogger& l, const eo::Levels lvl)
{
l._contexLevel = lvl;
return l;
}
eoLogger& operator<<(eoLogger& l, eo::file f)
{
l._fd = ::open(f._f.c_str(), O_WRONLY | O_APPEND | O_CREAT, 0644);
return l;
}
eoLogger& operator<<(eoLogger& l, eo::setlevel v)
{
l._selectedLevel = (v._lvl < 0 ? l._levels[v._v] : v._lvl);
return l;
}
eoLogger& operator<<(eoLogger& l, std::ostream& os)
{
if (l._standard_io_streams.find(&os) != l._standard_io_streams.end())
{
l._fd = l._standard_io_streams[&os];
}
return l;
}
eoLogger::outbuf::outbuf(const int& fd,
const eo::Levels& contexlvl,
const eo::Levels& selectedlvl)
: _fd(fd), _contexLevel(contexlvl), _selectedLevel(selectedlvl)
{}
int eoLogger::outbuf::overflow(int_type c)
{
if (_selectedLevel >= _contexLevel)
{
if (_fd >= 0 && c != EOF)
{
ssize_t num;
num = ::write(_fd, &c, 1);
}
}
return c;
}
namespace eo
{
file::file(const std::string f)
: _f(f)
{}
setlevel::setlevel(const std::string v)
: _v(v), _lvl((Levels)-1)
{}
setlevel::setlevel(const Levels lvl)
: _v(std::string("")), _lvl(lvl)
{}
}

234
eo/src/utils/eoLogger.h Normal file
View file

@ -0,0 +1,234 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//-----------------------------------------------------------------------------
// eoLogger.h
// (c) Thales group, 2010
/*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Authors:
Johann Dréo <johann.dreo@thalesgroup.com>
Caner Candan <caner.candan@thalesgroup.com>
*/
//-----------------------------------------------------------------------------
/** Here's an example explaning how to use eoLogger:
#include <iostream>
#include <utils/eoLogger.h>
#include <utils/eoParserLogger.h>
int main(int ac, char** av)
{
// We are declaring first an overload of eoParser class using Logger
// component.
eoParserLogger parser(ac, av);
// This call is important to allow -v parameter to change user level.
make_verbose(parser);
// At this time we are switching to warning message and messages
// which are going to follow it are going to be warnings message too.
// These messages can be displayed only if the user level (sets with
// eo::setlevel function) is set to eo::warnings.
eo::log << eo::warnings;
// With the following eo::file function we are defining that
// all future logs are going to this new file resource which is
// test.txt
eo::log << eo::file("test.txt") << "In FILE" << std::endl;
// Now we are changing again the resources destination to cout which
// is the standard output.
eo::log << std::cout << "In COUT" << std::endl;
// Here are 2 differents examples of how to set the errors user level
// in using either a string or an identifier.
eo::log << eo::setlevel("errors");
eo::log << eo::setlevel(eo::errors);
// Now we are writting a message, that will be displayed only if we are above the "quiet" level
eo::log << eo::quiet << "1) Must be in quiet mode to see that" << std::endl;
// And so on...
eo::log << eo::setlevel(eo::warnings) << eo::warnings << "2) Must be in warnings mode to see that" << std::endl;
eo::log << eo::setlevel(eo::logging);
eo::log << eo::errors;
eo::log << "3) Must be in errors mode to see that";
eo::log << std::endl;
eo::log << eo::debug << 4 << ')'
<< " Must be in debug mode to see that\n";
return 0;
}
*/
#ifndef eoLogger_h
# define eoLogger_h
# include <map>
# include <vector>
# include <string>
# include <iosfwd>
# include "eoObject.h"
//-----------------------------------------------------------------------------
namespace eo
{
/**
* Levels contents all the available levels in eoLogger
*/
enum Levels {quiet = 0,
errors,
warnings,
progress,
logging,
debug};
/**
* file
* this structure combined with the friend operator<< below is an easy way to select a file like output.
*/
struct file
{
file(const std::string f);
const std::string _f;
};
/**
* setlevel
* this structure combined with the friend operator<< below is an easy way to set a verbose level.
*/
struct setlevel
{
setlevel(const std::string v);
setlevel(const Levels lvl);
const std::string _v;
const Levels _lvl;
};
}
/**
* eoLogger
* Class providing a verbose management through EO
* Use of a global variable eo::log to easily use the logger like std::cout
*/
class eoLogger : public eoObject,
public std::ostream
{
public:
eoLogger();
~eoLogger();
virtual std::string className() const;
void addLevel(std::string name, eo::Levels level);
void printLevels() const;
private:
/**
* outbuf
* this class inherits from std::streambuf which is used by eoLogger to write the buffer in an output stream
*/
class outbuf : public std::streambuf
{
public:
outbuf(const int& fd,
const eo::Levels& contexlvl,
const eo::Levels& selectedlvl);
protected:
virtual int overflow(int_type c);
private:
const int& _fd;
const eo::Levels& _contexLevel;
const eo::Levels& _selectedLevel;
};
private:
/**
* MapLevel is the type used by the map member _levels.
*/
typedef std::map<std::string, eo::Levels> MapLevel;
public:
/**
* operator<< used there to set a verbose mode.
*/
friend eoLogger& operator<<(eoLogger&, const eo::Levels);
/**
* operator<< used there to set a filename through the class file.
*/
friend eoLogger& operator<<(eoLogger&, eo::file);
/**
* operator<< used there to set a verbose level through the class setlevel.
*/
friend eoLogger& operator<<(eoLogger&, eo::setlevel);
/**
* operator<< used there to be able to use std::cout to say that we wish to redirect back the buffer to a standard output.
*/
friend eoLogger& operator<<(eoLogger&, std::ostream&);
private:
/**
* _selectedLevel is the member storing verbose level setted by the user thanks to operator()
*/
eo::Levels _selectedLevel;
eo::Levels _contexLevel;
/**
* _fd in storing the file descriptor at this place we can disable easily the buffer in
* changing the value at -1. It is used by operator <<.
*/
int _fd;
/**
* _obuf std::ostream mandates to use a buffer. _obuf is a outbuf inheriting of std::streambuf.
*/
outbuf _obuf;
/**
* _levels contains all the existing level order by position
*/
MapLevel _levels;
/**
* _levelsOrder is just a list to keep the order of levels
*/
std::vector<std::string> _sortedLevels;
/**
* _standard_io_streams
*/
std::map< std::ostream*, int > _standard_io_streams;
};
namespace eo
{
/**
* log is an external global variable defined to easily use a same way than std::cout to write a log.
*/
extern eoLogger log;
}
#endif // !eoLogger_h

View file

@ -0,0 +1,50 @@
/* (c) Thales group, 2010
This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your option) any
later version.
This library is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along
with this library; if not, write to the Free Software Foundation, Inc., 59
Temple Place, Suite 330, Boston, MA 02111-1307 USA
Contact: http://eodev.sourceforge.net
Authors:
Johann Dreo <johann.dreo@thalesgroup.com>
Caner Candan <caner.candan@thalesgroup.com>
*/
#include "eoParserLogger.h"
eoParserLogger::eoParserLogger(unsigned _argc, char** _argv,
std::string _programDescription /*= ""*/
,
std::string _lFileParamName /*= "param-file"*/,
char _shortHand /*= 'p'*/)
: eoParser(_argc, _argv, _programDescription, _lFileParamName, _shortHand),
_verbose("quiet", "verbose", "Set the verbose level", 'v'),
_printVerboseLevels(false, "print-verbose-levels", "Print verbose levels", 'l')
{
processParam(_verbose);
processParam(_printVerboseLevels);
if (!_printVerboseLevels.value())
return;
eo::log.printLevels();
}
eoParserLogger::~eoParserLogger()
{}
//! make_verbose gets level of verbose and sets it in eoLogger
void make_verbose(eoParserLogger& _parser)
{
eo::log << eo::setlevel(_parser._verbose.value());
}

View file

@ -0,0 +1,57 @@
/* (c) Thales group, 2010
This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your option) any
later version.
This library is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along
with this library; if not, write to the Free Software Foundation, Inc., 59
Temple Place, Suite 330, Boston, MA 02111-1307 USA
Contact: http://eodev.sourceforge.net
Authors:
Johann Dreo <johann.dreo@thalesgroup.com>
Caner Candan <caner.candan@thalesgroup.com>
*/
#ifndef EO_PARSER_LOGGER_H
#define EO_PARSER_LOGGER_H
#include "eoParser.h"
#include "eoLogger.h"
/**
* A parser that use the advanced logging system (@see eoLogger)
*/
class eoParserLogger : public eoParser
{
public:
eoParserLogger(unsigned _argc, char** _argv,
std::string _programDescription = "",
std::string _lFileParamName = "param-file",
char _shortHand = 'p');
~eoParserLogger();
private:
friend void make_verbose(eoParserLogger&);
eoValueParam<std::string> _verbose;
eoValueParam<bool> _printVerboseLevels;
};
void make_verbose(eoParserLogger&);
#endif // !EO_PARSER_LOGGER_H
// Local Variables:
// coding: iso-8859-1
// mode: C++
// c-file-offsets: ((c . 0))
// c-file-style: "Stroustrup"
// fill-column: 80
// End:

View file

@ -67,6 +67,7 @@ SET (TEST_LIST t-eoParetoFitness
t-eoExtendedVelocity
# t-eoFrontSorter
# t-eoEpsMOEA
t-eoLogger
)
FOREACH (test ${TEST_LIST})

43
eo/test/t-eoLogger.cpp Normal file
View file

@ -0,0 +1,43 @@
//-----------------------------------------------------------------------------
// t-eoLogger.cpp
//-----------------------------------------------------------------------------
#include <iostream>
#include <utils/eoLogger.h>
#include <utils/eoParserLogger.h>
//-----------------------------------------------------------------------------
int main(int ac, char** av)
{
eoParserLogger parser(ac, av);
make_verbose(parser);
eo::log << eo::setlevel(eo::debug);
eo::log << eo::warnings;
eo::log << eo::file("test.txt") << "In FILE" << std::endl;
eo::log << std::cout << "In COUT" << std::endl;
eo::log << eo::setlevel("errors");
eo::log << eo::setlevel(eo::errors);
eo::log << eo::quiet << "1) Must be in quiet mode" << std::endl;
eo::log << eo::setlevel(eo::warnings) << eo::warnings << "2) Must be in warnings mode" << std::endl;
eo::log << eo::setlevel(eo::logging);
eo::log << eo::errors;
eo::log << "3) Must be in errors mode";
eo::log << std::endl;
eo::log << eo::debug << 4 << ')'
<< " Must be in debug mode\n";
return 0;
}
//-----------------------------------------------------------------------------