1
0
mirror of https://github.com/TomHarte/CLK.git synced 2026-04-21 02:17:08 +00:00

Applies parsed arguments.

This commit is contained in:
Thomas Harte
2020-03-18 22:31:32 -04:00
parent 311458f41f
commit 615ea2f573
5 changed files with 119 additions and 59 deletions
+7 -7
View File
@@ -113,13 +113,13 @@ class Enum {
@returns A @c std::string name for the enum value @c e.
*/
template <typename Type> static const std::string &to_string(Type e) {
return to_string(typeid(Type), size_t(e));
return to_string(typeid(Type), int(e));
}
/*!
@returns A @c std::string name for the enum value @c e from the enum with type_info @c type.
*/
static const std::string &to_string(std::type_index type, size_t e) {
static const std::string &to_string(std::type_index type, int e) {
const auto entry = members_by_type_.find(type);
if(entry == members_by_type_.end()) return empty_string_;
return entry->second[e];
@@ -150,15 +150,15 @@ class Enum {
}
/*!
@returns A value for the name @c str in the enum with type_info @c type , or @c std::string::npos if
@returns A value for the name @c str in the enum with type_info @c type , or @c -1 if
the name is not found.
*/
static size_t from_string(std::type_index type, const std::string &str) {
static int from_string(std::type_index type, const std::string &str) {
const auto entry = members_by_type_.find(type);
if(entry == members_by_type_.end()) return std::string::npos;
if(entry == members_by_type_.end()) return -1;
const auto iterator = std::find(entry->second.begin(), entry->second.end(), str);
if(iterator == entry->second.end()) return std::string::npos;
return size_t(iterator - entry->second.begin());
if(iterator == entry->second.end()) return -1;
return int(iterator - entry->second.begin());
}
private:
+50 -5
View File
@@ -8,6 +8,8 @@
#include "Struct.h"
#include <algorithm>
// MARK: - Setters
template <> bool Reflection::set(Struct &target, const std::string &name, int value) {
@@ -38,13 +40,11 @@ template <> bool Reflection::set(Struct &target, const std::string &name, const
return false;
}
const auto enum_value = Reflection::Enum::from_string(*target_type, value);
if(enum_value == std::string::npos) {
const int enum_value = Reflection::Enum::from_string(*target_type, value);
if(enum_value < 0) {
return false;
}
int int_value = int(enum_value);
target.set(name, &int_value);
target.set(name, &enum_value);
return true;
}
@@ -54,9 +54,54 @@ template <> bool Reflection::set(Struct &target, const std::string &name, const
return set<const std::string &>(target, name, string);
}
template <> bool Reflection::set(Struct &target, const std::string &name, bool value) {
const auto target_type = target.type_of(name);
if(!target_type) return false;
if(*target_type == typeid(bool)) {
target.set(name, &value);;
}
return false;
}
// MARK: - Fuzzy setter
bool Reflection::fuzzy_set(Struct &target, const std::string &name, const std::string &value) {
const auto target_type = target.type_of(name);
if(!target_type) return false;
// If the target is a registered enum, ttry to convert the value. Failing that,
// try to match without case sensitivity.
if(Reflection::Enum::size(*target_type)) {
const int from_string = Reflection::Enum::from_string(*target_type, value);
if(from_string >= 0) {
target.set(name, &from_string);
return true;
}
const auto all_values = Reflection::Enum::all_values(*target_type);
const auto value_location = std::find_if(all_values.begin(), all_values.end(),
[&value] (const auto &entry) {
if(value.size() != entry.size()) return false;
const char *v = value.c_str();
const char *e = entry.c_str();
while(*v) {
if(tolower(*v) != tolower(*e)) return false;
++v;
++e;
}
return true;
});
if(value_location != all_values.end()) {
const int offset = int(value_location - all_values.begin());
target.set(name, &offset);
return true;
}
return false;
}
return false;
}
+7
View File
@@ -56,6 +56,13 @@ template <> bool set(Struct &target, const std::string &name, int value);
template <> bool set(Struct &target, const std::string &name, const std::string &value);
template <> bool set(Struct &target, const std::string &name, const char *value);
/*!
Setting a bool:
* to a bool, copies the value.
*/
template <> bool set(Struct &target, const std::string &name, bool value);
/*!
Fuzzy-set attempts to set any property based on a string value. This is intended to allow input provided by the user.