- 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"; get_parameter(cmd_p);
if(p.is_required()) parameters.set_int(p.get_name(), cmd_p.value);
buf << "*";
buf << p.get_name() << " (range: [" << p.get_lower_bound() << ", " << ++it;
p.get_upper_bound() << "] default: " << p.get_default() << ") = ";
p_stdout(buf.str()); // ostringstream buf;
//
bool correct = true; // buf << "\n";
//
ustring input = readline(); // if(p.is_required())
// buf << "*";
int value; //
// buf << p.get_name() << " (range: [" << p.get_lower_bound() << ", " <<
// FIXME semi-hack, it's a bit overkill to tokenize the string // p.get_upper_bound() << "] default: " << p.get_default() << ") = ";
// to find if it's only composed of white spaces... //
// Indedeed there's a pro: by using extensively tokenize() we are more sure // p_stdout(buf.str());
// it's correct ;-) //
if(tokenize(input).size() > 0) // bool correct = true;
{ //
try // ustring input = readline();
{ //
value = string_to<int>(input); // int value;
//
if(value > p.get_upper_bound() || value < p.get_lower_bound()) // // FIXME semi-hack, it's a bit overkill to tokenize the string
{ // // to find if it's only composed of white spaces...
p_stderr(_("\nERROR: Provided value is out of range")); // // Indedeed there's a pro: by using extensively tokenize() we are more sure
correct = false; // // it's correct ;-)
} // if(tokenize(input).size() > 0)
} // {
catch(domain_error e) // try
{ // {
p_stderr(_("\nERROR: Please provide a valid integer value")); // value = string_to<int>(input);
correct = false; //
} // if(value > p.get_upper_bound() || value < p.get_lower_bound())
// {
if(correct) // p_stderr(_("\nERROR: Provided value is out of range"));
parameters.set_int(p.get_name(), value); // correct = false;
} // }
else if(p.is_required()) // }
{ // catch(domain_error e)
p_stderr(_("\nERROR: This is a mandatory attribute; you MUST provide a valid value!")); // {
// p_stderr(_("\nERROR: Please provide a valid integer value"));
correct = false; // correct = false;
} // }
//
if(correct) // 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)
// ++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"; CommandParameter<float> cmd_p(p);
if(p.is_required()) get_parameter(cmd_p);
buf << "*";
buf << p.get_name() << " (range: [" << p.get_lower_bound() << ", " << parameters.set_float(p.get_name(), cmd_p.value);
p.get_upper_bound() << "] default: " << p.get_default() << ") = ";
p_stdout(buf.str()); ++it;
// ostringstream buf;
bool correct = true; //
// buf << "\n";
ustring input = readline(); //
// if(p.is_required())
float value; // buf << "*";
//
// FIXME semi-hack, it's a bit overkill to tokenize the string // buf << p.get_name() << " (range: [" << p.get_lower_bound() << ", " <<
// to find if it's only composed of white spaces... // p.get_upper_bound() << "] default: " << p.get_default() << ") = ";
// Indedeed there's a pro: by using extensively tokenize() we are more sure //
// it's correct ;-) // p_stdout(buf.str());
if(tokenize(input).size() > 0) //
{ // bool correct = true;
try //
{ // ustring input = readline();
value = string_to<float>(input); //
// float value;
if(value > p.get_upper_bound() || value < p.get_lower_bound()) //
{ // // FIXME semi-hack, it's a bit overkill to tokenize the string
p_stderr(_("\nERROR: Provided value is out of range")); // // to find if it's only composed of white spaces...
correct = false; // // Indedeed there's a pro: by using extensively tokenize() we are more sure
} // // it's correct ;-)
} // if(tokenize(input).size() > 0)
catch(domain_error e) // {
{ // try
p_stderr(_("\nERROR: Please provide a valid floating-point value")); // {
correct = false; // value = string_to<float>(input);
} //
// if(value > p.get_upper_bound() || value < p.get_lower_bound())
if(correct) // {
parameters.set_float(p.get_name(), value); // p_stderr(_("\nERROR: Provided value is out of range"));
} // correct = false;
else if(p.is_required()) // }
{ // }
p_stderr(_("\nERROR: This is a mandatory attribute; you MUST provide a valid value!")); // catch(domain_error e)
// {
correct = false; // p_stderr(_("\nERROR: Please provide a valid floating-point value"));
} // correct = false;
// }
if(correct) //
++it; // 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"; CommandParameter<ustring> cmd_p(p);
if(p.is_required()) get_parameter(cmd_p);
buf += "*";
p_stdout(buf + p.get_name() + " = "); parameters.set_string(p.get_name(), cmd_p.value);
buf = readline(); ++it;
// FIXME semi-hack, it's a bit overkill to tokenize the string // ustring buf;
// to find if it's only composed of white spaces... //
// Indedeed there's a pro: by using extensively tokenize() we are more sure // buf = "\n";
// it's correct ;-) //
Tokens tokens = tokenize(buf); // if(p.is_required())
// buf += "*";
if(tokens.size() == 0 && p.is_required()) //
p_stderr(_("\nERROR: This is a mandatory atribute; you MUST provide a valid value!")); // p_stdout(buf + p.get_name() + " = ");
else //
{ // buf = readline();
// FIXME should we insert the entire line here or just a token? //
parameters.set_string(p.get_name(), buf); // // FIXME semi-hack, it's a bit overkill to tokenize the string
++it; // // 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 && 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);