mirror of
https://github.com/love2d/love.git
synced 2026-08-15 15:51:12 +02:00
Show (short) list of possible enum values when an invalid value is encountered (resolves #1318)
All enum errors have (hopefully) been changed to a luax_enumerror, which has a fixed error message. If additionally a list of valid options is passed, it lists that in the error message. For every enum error with few options I've implemented this using a getConstants call. This solution has been designed specifically to reduce the number of template instantiations (as I've been told that was a concern). All new template code happens in places where there already was an instantiation of the relevant StringMap. Of course there is still std::vector<std::string>... --HG-- branch : minor
This commit is contained in:
@@ -64,5 +64,10 @@ bool Audio::getConstant(DistanceModel in, const char *&out)
|
||||
return distanceModels.find(in, out);
|
||||
}
|
||||
|
||||
std::vector<std::string> Audio::getConstants(DistanceModel)
|
||||
{
|
||||
return distanceModels.getNames();
|
||||
}
|
||||
|
||||
} // audio
|
||||
} // love
|
||||
|
||||
@@ -69,6 +69,7 @@ public:
|
||||
|
||||
static bool getConstant(const char *in, DistanceModel &out);
|
||||
static bool getConstant(DistanceModel in, const char *&out);
|
||||
static std::vector<std::string> getConstants(DistanceModel);
|
||||
|
||||
virtual ~Audio() {}
|
||||
|
||||
|
||||
@@ -47,6 +47,12 @@ bool Effect::getConstant(Type in, const char *&out)
|
||||
{
|
||||
return types.find(in, out);
|
||||
}
|
||||
|
||||
std::vector<std::string> Effect::getConstants(Type)
|
||||
{
|
||||
return types.getNames();
|
||||
}
|
||||
|
||||
/*
|
||||
bool Effect::getConstant(const char *in, Phoneme &out)
|
||||
{
|
||||
|
||||
@@ -248,6 +248,7 @@ public:
|
||||
|
||||
static bool getConstant(const char *in, Type &out);
|
||||
static bool getConstant(Type in, const char *&out);
|
||||
static std::vector<std::string> getConstants(Type);
|
||||
static bool getConstant(const char *in, Waveform &out);
|
||||
static bool getConstant(Waveform in, const char *&out);
|
||||
//static bool getConstant(const char *in, Direction &out);
|
||||
|
||||
@@ -48,6 +48,11 @@ bool Filter::getConstant(Type in, const char *&out)
|
||||
return types.find(in, out);
|
||||
}
|
||||
|
||||
std::vector<std::string> Filter::getConstants(Type)
|
||||
{
|
||||
return types.getNames();
|
||||
}
|
||||
|
||||
bool Filter::getConstant(const char *in, Parameter &out, Type t)
|
||||
{
|
||||
return parameterNames[t].find(in, out);
|
||||
|
||||
@@ -109,6 +109,7 @@ public:
|
||||
|
||||
static bool getConstant(const char *in, Type &out);
|
||||
static bool getConstant(Type in, const char *&out);
|
||||
static std::vector<std::string> getConstants(Type);
|
||||
static bool getConstant(const char *in, Parameter &out, Type t);
|
||||
static bool getConstant(Parameter in, const char *&out, Type t);
|
||||
static ParameterType getParameterType(Parameter in);
|
||||
|
||||
@@ -51,6 +51,11 @@ bool Source::getConstant(Type in, const char *&out)
|
||||
return types.find(in, out);
|
||||
}
|
||||
|
||||
std::vector<std::string> Source::getConstants(Type)
|
||||
{
|
||||
return types.getNames();
|
||||
}
|
||||
|
||||
bool Source::getConstant(const char *in, Unit &out)
|
||||
{
|
||||
return units.find(in, out);
|
||||
@@ -61,6 +66,11 @@ bool Source::getConstant(Unit in, const char *&out)
|
||||
return units.find(in, out);
|
||||
}
|
||||
|
||||
std::vector<std::string> Source::getConstants(Unit)
|
||||
{
|
||||
return units.getNames();
|
||||
}
|
||||
|
||||
StringMap<Source::Type, Source::TYPE_MAX_ENUM>::Entry Source::typeEntries[] =
|
||||
{
|
||||
{"static", Source::TYPE_STATIC},
|
||||
|
||||
@@ -126,8 +126,10 @@ public:
|
||||
|
||||
static bool getConstant(const char *in, Type &out);
|
||||
static bool getConstant(Type in, const char *&out);
|
||||
static std::vector<std::string> getConstants(Type);
|
||||
static bool getConstant(const char *in, Unit &out);
|
||||
static bool getConstant(Unit in, const char *&out);
|
||||
static std::vector<std::string> getConstants(Unit);
|
||||
|
||||
protected:
|
||||
|
||||
|
||||
@@ -51,7 +51,7 @@ int w_newSource(lua_State *L)
|
||||
{
|
||||
const char *stypestr = luaL_checkstring(L, 2);
|
||||
if (stypestr && !Source::getConstant(stypestr, stype))
|
||||
return luaL_error(L, "Invalid source type: %s", stypestr);
|
||||
return luax_enumerror(L, "source type", Source::getConstants(stype), stypestr);
|
||||
}
|
||||
|
||||
if (lua_isstring(L, 1) || luax_istype(L, 1, love::filesystem::File::type) || luax_istype(L, 1, love::filesystem::FileData::type))
|
||||
@@ -297,7 +297,7 @@ int w_setDistanceModel(lua_State *L)
|
||||
const char *modelStr = luaL_checkstring(L, 1);
|
||||
Audio::DistanceModel distanceModel;
|
||||
if (!Audio::getConstant(modelStr, distanceModel))
|
||||
return luaL_error(L, "Invalid distance model: %s", modelStr);
|
||||
return luax_enumerror(L, "distance model", Audio::getConstants(distanceModel), modelStr);
|
||||
instance()->setDistanceModel(distanceModel);
|
||||
return 0;
|
||||
}
|
||||
@@ -351,7 +351,7 @@ int w_setEffect(lua_State *L)
|
||||
Effect::Type type = Effect::TYPE_MAX_ENUM;
|
||||
const char *typestr = luaL_checkstring(L, -1);
|
||||
if (!Effect::getConstant(typestr, type))
|
||||
return luaL_error(L, "Invalid Effect type: %s", typestr);
|
||||
return luax_enumerror(L, "effect type", Effect::getConstants(type), typestr);
|
||||
|
||||
lua_pop(L, 1);
|
||||
std::map<Effect::Parameter, float> params;
|
||||
@@ -386,7 +386,7 @@ int w_setEffect(lua_State *L)
|
||||
paramstr = lua_tostring(L, -1);
|
||||
Effect::Waveform waveform;
|
||||
if (!Effect::getConstant(paramstr, waveform))
|
||||
return luaL_error(L, "Invalid waveform type: %s", paramstr);
|
||||
return luax_enumerror(L, "waveform type", paramstr);
|
||||
params[param] = static_cast<int>(waveform);
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -111,7 +111,7 @@ int w_Source_seek(lua_State *L)
|
||||
Source::Unit u = Source::UNIT_SECONDS;
|
||||
const char *unit = lua_isnoneornil(L, 3) ? 0 : lua_tostring(L, 3);
|
||||
if (unit && !t->getConstant(unit, u))
|
||||
return luaL_error(L, "Invalid Source time unit: %s", unit);
|
||||
return luax_enumerror(L, "time unit", Source::getConstants(u), unit);
|
||||
|
||||
t->seek(offset, u);
|
||||
return 0;
|
||||
@@ -124,7 +124,7 @@ int w_Source_tell(lua_State *L)
|
||||
Source::Unit u = Source::UNIT_SECONDS;
|
||||
const char *unit = lua_isnoneornil(L, 2) ? 0 : lua_tostring(L, 2);
|
||||
if (unit && !t->getConstant(unit, u))
|
||||
return luaL_error(L, "Invalid Source time unit: %s", unit);
|
||||
return luax_enumerror(L, "time unit", Source::getConstants(u), unit);
|
||||
|
||||
lua_pushnumber(L, t->tell(u));
|
||||
return 1;
|
||||
@@ -137,7 +137,7 @@ int w_Source_getDuration(lua_State *L)
|
||||
Source::Unit u = Source::UNIT_SECONDS;
|
||||
const char *unit = lua_isnoneornil(L, 2) ? 0 : lua_tostring(L, 2);
|
||||
if (unit && !t->getConstant(unit, u))
|
||||
return luaL_error(L, "Invalid Source time unit: %s", unit);
|
||||
return luax_enumerror(L, "time unit", Source::getConstants(u), unit);
|
||||
|
||||
lua_pushnumber(L, t->getDuration(u));
|
||||
return 1;
|
||||
@@ -363,7 +363,7 @@ int setFilterReadFilter(lua_State *L, int idx, std::map<Filter::Parameter, float
|
||||
Filter::Type type = Filter::TYPE_MAX_ENUM;
|
||||
const char *typestr = luaL_checkstring(L, -1);
|
||||
if (!Filter::getConstant(typestr, type))
|
||||
return luaL_error(L, "Invalid Filter type: %s", typestr);
|
||||
return luax_enumerror(L, "filter type", Filter::getConstants(type), typestr);
|
||||
|
||||
lua_pop(L, 1);
|
||||
params[Filter::FILTER_TYPE] = static_cast<int>(type);
|
||||
|
||||
Reference in New Issue
Block a user