list properties now prints out valid values.

This commit is contained in:
Maxim Poliakovski 2020-10-14 16:19:11 +02:00
parent 4c0c32c02c
commit 2df2d089d7
2 changed files with 36 additions and 4 deletions

View File

@ -194,7 +194,14 @@ void list_properties() {
cout << get<2>(mach.second) << " supported properties:" << endl << endl;
for (auto& p : get<0>(mach.second)) {
cout << setw(13) << p.first << "\t\t" << PropHelp.at(p.first) << endl << endl;
cout << setw(13) << p.first << "\t\t" << PropHelp.at(p.first)
<< endl;
if (p.second->get_type() == PROP_TYPE_INTEGER) {
cout << setw(13) << "\t\t\t" "Valid values: " <<
dynamic_cast<IntProperty*>(p.second)->get_valid_values_as_str()
<< endl;
}
cout << endl;
}
}

View File

@ -4,6 +4,7 @@
#include <string>
#include <map>
#include <memory>
#include <sstream>
#include <utility>
#include <vector>
@ -25,7 +26,7 @@ enum PropType : int {
enum CheckType : int {
CHECK_TYPE_NONE = 0,
CHECK_TYPE_RANGE = 1,
CHECK_TYPE_CHOICE = 2,
CHECK_TYPE_LIST = 2,
};
/** Abstract base class for properties. */
@ -100,7 +101,7 @@ public:
this->int_val = val;
this->min = std::numeric_limits<uint32_t>::min();
this->max = std::numeric_limits<uint32_t>::max();
this->check_type = CHECK_TYPE_CHOICE;
this->check_type = CHECK_TYPE_LIST;
this->vec = vec;
}
@ -113,6 +114,8 @@ public:
/* perform value check */
if (!this->check_val(result)) {
LOG_F(ERROR, "Invalid property value %d!", result);
LOG_F(ERROR, "Valid values: %s!",
this->get_valid_values_as_str().c_str());
this->set_string(to_string(this->int_val));
} else {
this->int_val = result;
@ -124,6 +127,28 @@ public:
return this->int_val;
}
string get_valid_values_as_str() {
stringstream ss;
switch (this->check_type) {
case CHECK_TYPE_RANGE:
ss << "[" << this->min << "..." << this->max << "]";
return ss.str();
case CHECK_TYPE_LIST: {
bool first = true;
for (auto it = begin(this->vec); it != end(this->vec); ++it) {
if (!first)
ss << ", ";
ss << *it;
first = false;
}
return ss.str();
}
default:
return string("None");
}
}
protected:
bool check_val(uint32_t val) {
switch (this->check_type) {
@ -132,7 +157,7 @@ protected:
return false;
else
return true;
case CHECK_TYPE_CHOICE:
case CHECK_TYPE_LIST:
if (find(this->vec.begin(), this->vec.end(), val) != this->vec.end())
return true;
else