- Added some more templates in preparation of the ADD wizards. This will minimize redundant code in the forthcoming methods

git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@758 3ecf2c5c-341e-0410-92b4-d18e462d057c
This commit is contained in:
elvez 2006-07-12 23:52:09 +00:00
parent 4bece17f36
commit 456cef0fd3
2 changed files with 300 additions and 120 deletions

View File

@ -42,6 +42,53 @@ using Glib::ustring;
#include "smartp.tcc" #include "smartp.tcc"
namespace sgpem
{
template <typename T>
class CommandParameter
{
public:
CommandParameter(const ustring& _description,
const T& _low_bound,
const T& _up_bound,
bool _required,
const T& _preset);
CommandParameter(const PolicyParameters::Parameter<T>& pparam);
ustring description;
T low_bound;
T up_bound;
bool required;
T preset;
T value;
};
template <typename T>
CommandParameter<T>::CommandParameter(const ustring& _description,
const T& _low_bound,
const T& _up_bound,
bool _required,
const T& _preset) :
description(_description), low_bound(_low_bound),
up_bound(_up_bound), required(_required), preset(_preset),
value(_preset)
{
}
template <typename T>
CommandParameter<T>::CommandParameter(const PolicyParameters::Parameter<T>& pparam) :
description(pparam.get_name()),
low_bound(pparam.get_lower_bound()),
up_bound(pparam.get_upper_bound()), required(pparam.is_required()),
preset(pparam.get_default()), value(pparam.get_value())
{
}
}
TextSimulation::~TextSimulation() TextSimulation::~TextSimulation()
{} {}
@ -89,6 +136,7 @@ TextSimulation::show(const vector<T*>& entities)
} }
} }
// Specializations need to go explicitly inside the namespace. why?
namespace sgpem namespace sgpem
{ {
template <> template <>
@ -118,6 +166,106 @@ namespace sgpem
} }
} }
template <typename T>
void
TextSimulation::get_parameter(CommandParameter<T>& parameter)
{
bool correct = true;
do
{
ostringstream buf;
buf << "\n";
if(parameter.required)
buf << "*";
buf << parameter.description << " (range: [" << parameter.low_bound << ", " <<
parameter.up_bound << "] current: " << parameter.value << ") = ";
p_stdout(buf.str());
ustring input = readline();
T value;
// FIXME semi-hack, it's a bit overkill to tokenize the string
// to find if it's only composed of white spaces...
// Indedeed there's a pro: by using extensively tokenize() we are more sure
// it's correct ;-)
if(tokenize(input).size() > 0)
{
try
{
value = string_to<T>(input);
if(value > parameter.up_bound || value < parameter.low_bound)
{
p_stderr(_("\nERROR: Provided value is out of range"));
correct = false;
}
}
catch(domain_error e)
{
p_stderr(_("\nERROR: Please provide a valid integer value"));
correct = false;
}
if(correct)
parameter.value = value;
}
else if(parameter.required)
{
p_stderr(_("\nERROR: This is a mandatory attribute; you MUST provide a valid value!"));
correct = false;
}
}
while(!correct);
}
// Specializations need to go explicitly inside the namespace. why?
namespace sgpem
{
template <>
void
TextSimulation::get_parameter<ustring>(CommandParameter<ustring>& parameter)
{
bool loop = true;
while(loop)
{
ustring buf;
buf = "\n";
if(parameter.required)
buf += "*";
p_stdout(buf + parameter.description +
" (current: \"" + parameter.value + "\") = ");
buf = readline();
// FIXME semi-hack, it's a bit overkill to tokenize the string
// to find if it's only composed of white spaces...
// Indedeed there's a pro: by using extensively tokenize() we are more sure
// it's correct ;-)
Tokens tokens = tokenize(buf);
if(tokens.size() == 0 && parameter.required)
p_stderr(_("\nERROR: This is a mandatory atribute; you MUST provide a valid value!"));
else
{
// FIXME should we assign the entire line here or just a token?
parameter.value = buf;
loop = false;
}
}
}
}
void void
TextSimulation::on_run(const Tokens& arguments) TextSimulation::on_run(const Tokens& arguments)
{ {
@ -186,58 +334,66 @@ TextSimulation::on_configure_cpu_policy(const Tokens& arguments)
for(IntParams::iterator it = int_params.begin(); it != int_params.end();) for(IntParams::iterator it = int_params.begin(); it != int_params.end();)
{ {
PolicyParameters::Parameter<int> &p = it->second; PolicyParameters::Parameter<int> &p = it->second;
ostringstream buf; CommandParameter<int> cmd_p(p);
buf << "\n";
if(p.is_required())
buf << "*";
buf << p.get_name() << " (range: [" << p.get_lower_bound() << ", " <<
p.get_upper_bound() << "] default: " << p.get_default() << ") = ";
p_stdout(buf.str()); get_parameter(cmd_p);
bool correct = true;
ustring input = readline();
int value;
// FIXME semi-hack, it's a bit overkill to tokenize the string
// to find if it's only composed of white spaces...
// Indedeed there's a pro: by using extensively tokenize() we are more sure
// it's correct ;-)
if(tokenize(input).size() > 0)
{
try
{
value = string_to<int>(input);
if(value > p.get_upper_bound() || value < p.get_lower_bound()) parameters.set_int(p.get_name(), cmd_p.value);
{
p_stderr(_("\nERROR: Provided value is out of range"));
correct = false;
}
}
catch(domain_error e)
{
p_stderr(_("\nERROR: Please provide a valid integer value"));
correct = false;
}
if(correct) ++it;
parameters.set_int(p.get_name(), value);
}
else if(p.is_required())
{
p_stderr(_("\nERROR: This is a mandatory attribute; you MUST provide a valid value!"));
correct = false;
}
if(correct) // ostringstream buf;
++it; //
// buf << "\n";
//
// if(p.is_required())
// buf << "*";
//
// buf << p.get_name() << " (range: [" << p.get_lower_bound() << ", " <<
// p.get_upper_bound() << "] default: " << p.get_default() << ") = ";
//
// p_stdout(buf.str());
//
// bool correct = true;
//
// ustring input = readline();
//
// int value;
//
// // FIXME semi-hack, it's a bit overkill to tokenize the string
// // to find if it's only composed of white spaces...
// // Indedeed there's a pro: by using extensively tokenize() we are more sure
// // it's correct ;-)
// if(tokenize(input).size() > 0)
// {
// try
// {
// value = string_to<int>(input);
//
// if(value > p.get_upper_bound() || value < p.get_lower_bound())
// {
// p_stderr(_("\nERROR: Provided value is out of range"));
// correct = false;
// }
// }
// catch(domain_error e)
// {
// p_stderr(_("\nERROR: Please provide a valid integer value"));
// correct = false;
// }
//
// if(correct)
// parameters.set_int(p.get_name(), value);
// }
// else if(p.is_required())
// {
// p_stderr(_("\nERROR: This is a mandatory attribute; you MUST provide a valid value!"));
//
// correct = false;
// }
//
// if(correct)
// ++it;
} }
p_stdout(_("\nFloating-point arguments:\n")); p_stdout(_("\nFloating-point arguments:\n"));
@ -252,58 +408,66 @@ TextSimulation::on_configure_cpu_policy(const Tokens& arguments)
for(FloatParams::iterator it = float_params.begin(); it != float_params.end();) for(FloatParams::iterator it = float_params.begin(); it != float_params.end();)
{ {
PolicyParameters::Parameter<float> &p = it->second; PolicyParameters::Parameter<float> &p = it->second;
ostringstream buf;
buf << "\n";
if(p.is_required())
buf << "*";
buf << p.get_name() << " (range: [" << p.get_lower_bound() << ", " <<
p.get_upper_bound() << "] default: " << p.get_default() << ") = ";
p_stdout(buf.str()); CommandParameter<float> cmd_p(p);
bool correct = true;
ustring input = readline();
float value;
// FIXME semi-hack, it's a bit overkill to tokenize the string
// to find if it's only composed of white spaces...
// Indedeed there's a pro: by using extensively tokenize() we are more sure
// it's correct ;-)
if(tokenize(input).size() > 0)
{
try
{
value = string_to<float>(input);
if(value > p.get_upper_bound() || value < p.get_lower_bound()) get_parameter(cmd_p);
{
p_stderr(_("\nERROR: Provided value is out of range"));
correct = false;
}
}
catch(domain_error e)
{
p_stderr(_("\nERROR: Please provide a valid floating-point value"));
correct = false;
}
if(correct) parameters.set_float(p.get_name(), cmd_p.value);
parameters.set_float(p.get_name(), value);
} ++it;
else if(p.is_required()) // ostringstream buf;
{ //
p_stderr(_("\nERROR: This is a mandatory attribute; you MUST provide a valid value!")); // buf << "\n";
//
correct = false; // if(p.is_required())
} // buf << "*";
//
if(correct) // buf << p.get_name() << " (range: [" << p.get_lower_bound() << ", " <<
++it; // p.get_upper_bound() << "] default: " << p.get_default() << ") = ";
//
// p_stdout(buf.str());
//
// bool correct = true;
//
// ustring input = readline();
//
// float value;
//
// // FIXME semi-hack, it's a bit overkill to tokenize the string
// // to find if it's only composed of white spaces...
// // Indedeed there's a pro: by using extensively tokenize() we are more sure
// // it's correct ;-)
// if(tokenize(input).size() > 0)
// {
// try
// {
// value = string_to<float>(input);
//
// if(value > p.get_upper_bound() || value < p.get_lower_bound())
// {
// p_stderr(_("\nERROR: Provided value is out of range"));
// correct = false;
// }
// }
// catch(domain_error e)
// {
// p_stderr(_("\nERROR: Please provide a valid floating-point value"));
// correct = false;
// }
//
// if(correct)
// parameters.set_float(p.get_name(), value);
// }
// else if(p.is_required())
// {
// p_stderr(_("\nERROR: This is a mandatory attribute; you MUST provide a valid value!"));
//
// correct = false;
// }
//
// if(correct)
// ++it;
} }
p_stdout(_("\nString arguments:\n")); p_stdout(_("\nString arguments:\n"));
@ -313,31 +477,40 @@ TextSimulation::on_configure_cpu_policy(const Tokens& arguments)
for(StringParams::iterator it = string_params.begin(); it != string_params.end();) for(StringParams::iterator it = string_params.begin(); it != string_params.end();)
{ {
PolicyParameters::Parameter<ustring> &p = it->second; PolicyParameters::Parameter<ustring> &p = it->second;
ustring buf;
buf = "\n";
if(p.is_required())
buf += "*";
p_stdout(buf + p.get_name() + " = "); CommandParameter<ustring> cmd_p(p);
get_parameter(cmd_p);
parameters.set_string(p.get_name(), cmd_p.value);
++it;
buf = readline(); // ustring buf;
//
// FIXME semi-hack, it's a bit overkill to tokenize the string // buf = "\n";
// to find if it's only composed of white spaces... //
// Indedeed there's a pro: by using extensively tokenize() we are more sure // if(p.is_required())
// it's correct ;-) // buf += "*";
Tokens tokens = tokenize(buf); //
// p_stdout(buf + p.get_name() + " = ");
if(tokens.size() == 0 && p.is_required()) //
p_stderr(_("\nERROR: This is a mandatory atribute; you MUST provide a valid value!")); // buf = readline();
else //
{ // // FIXME semi-hack, it's a bit overkill to tokenize the string
// FIXME should we insert the entire line here or just a token? // // to find if it's only composed of white spaces...
parameters.set_string(p.get_name(), buf); // // Indedeed there's a pro: by using extensively tokenize() we are more sure
++it; // // it's correct ;-)
} // Tokens tokens = tokenize(buf);
//
// if(tokens.size() == 0 && p.is_required())
// p_stderr(_("\nERROR: This is a mandatory atribute; you MUST provide a valid value!"));
// else
// {
// // FIXME should we insert the entire line here or just a token?
// parameters.set_string(p.get_name(), buf);
// ++it;
// }
} }
} }

View File

@ -37,6 +37,11 @@
#include <utility> #include <utility>
#include <map> #include <map>
namespace sgpem
{
template <typename T>
class CommandParameter;
}
namespace sgpem namespace sgpem
{ {
@ -125,6 +130,8 @@ namespace sgpem
bool check_arguments_num(const Tokens& arguments, unsigned int num); bool check_arguments_num(const Tokens& arguments, unsigned int num);
template <typename T> template <typename T>
void show(const std::vector<T*>& entities); void show(const std::vector<T*>& entities);
template <typename T>
void get_parameter(CommandParameter<T>& parameter);
void on_run(const Tokens& arguments); void on_run(const Tokens& arguments);
void on_pause(const Tokens& arguments); void on_pause(const Tokens& arguments);
void on_stop(const Tokens& arguments); void on_stop(const Tokens& arguments);