Commit 3fc08f0e authored by twanvl's avatar twanvl

EnumReader now warns about the correct string,

parse_enum function throws if the string can not be parsed
parent b7fdf553
......@@ -43,7 +43,7 @@ enum InstallType
, INSTALL_GLOBAL // install to the global files
};
bool parse_enum(const String&, InstallType&);
void parse_enum(const String&, InstallType&);
bool is_install_local(InstallType type);
/// How to handle filename conflicts
......
......@@ -21,7 +21,7 @@
DECLARE_TYPEOF_COLLECTION(SymbolVariationP);
bool parse_enum(const String&, ImageCombine& out);
void parse_enum(const String&, ImageCombine& out);
// ----------------------------------------------------------------------------- : Utility
......@@ -56,9 +56,7 @@ SCRIPT_FUNCTION(combine_blend) {
SCRIPT_PARAM(GeneratedImageP, image1);
SCRIPT_PARAM(GeneratedImageP, image2);
ImageCombine image_combine;
if (!parse_enum(combine, image_combine)) {
throw ScriptError(_("Not a valid combine mode: '") + combine + _("'"));
}
parse_enum(combine, image_combine);
return new_intrusive3<CombineBlendImage>(image1, image2, image_combine);
}
......@@ -78,9 +76,7 @@ SCRIPT_FUNCTION(set_combine) {
SCRIPT_PARAM(String, combine);
SCRIPT_PARAM_C(GeneratedImageP, input);
ImageCombine image_combine;
if (!parse_enum(combine, image_combine)) {
throw ScriptError(_("Not a valid combine mode: '") + combine + _("'"));
}
parse_enum(combine, image_combine);
return new_intrusive2<SetCombineImage>(input, image_combine);
}
......
......@@ -15,7 +15,7 @@
#include <gfx/color.hpp>
Alignment from_string(const String&);
bool parse_enum(const String&,Direction&);
void parse_enum(const String&,Direction&);
DECLARE_TYPEOF_COLLECTION(ScriptParseError);
......
......@@ -411,3 +411,17 @@ template <> void Reader::handle(FileName& f) {
handle(static_cast<String&>(f));
}
}
// ----------------------------------------------------------------------------- : EnumReader
void EnumReader::warnIfNotDone(Reader* errors_to) {
if (!done) {
// warning: unknown value
errors_to->warning(_ERROR_1_("unrecognized value", read));
}
}
void EnumReader::errorIfNotDone() {
if (!done) {
throw ParseError(_ERROR_1_("unrecognized value", read));
}
}
......@@ -256,15 +256,12 @@ void Reader::handle(IndexMap<K,V>& m) {
template<> void Reader::handle<Enum>(Enum& enum_) { \
EnumReader reader(getValue()); \
reflect_ ## Enum(enum_, reader); \
if (!reader.isDone()) { \
/* warning: unknown value */ \
warning(_ERROR_1_("unrecognized value", value)); \
} \
reader.warnIfNotDone(this); \
} \
bool parse_enum(const String& value, Enum& out) { \
void parse_enum(const String& value, Enum& out) { \
EnumReader reader(value); \
reflect_ ## Enum(out, reader); \
return reader.isDone(); \
reader.errorIfNotDone(); \
}
/// 'Tag' to be used when reflecting enumerations for Reader
......@@ -287,6 +284,8 @@ class EnumReader {
}
inline bool isDone() const { return done; }
void warnIfNotDone(Reader* errors_to);
void errorIfNotDone();
private:
String read; ///< The string to match to a value name
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment