gpt4 book ai didi

c++ - 如何有效地将 if 和 else 用于过滤结构?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:54:46 24 4
gpt4 key购买 nike

为了解析从 JavaScript 获取的函数参数,我需要执行大量检查。例如,一个函数可能需要一个对象作为参数,在 JavaScript 中看起来像这样。

{
Fullscreen: [ 'bool', false ],
Size: [ 'Vector2u', 800, 600 ],
Title: [ 'string', 'Hello World' ],
// more properties...
}

在 C++ 中,我通过遍历所有键并检查它们来解析它。如果其中一项检查失败,则应打印错误消息并跳过此键值对。这就是我目前的实现方式。我希望您不会因某些特定于引擎的调用而分心。

ModuleSettings *module = (ModuleSettings*)HelperScript::Unwrap(args.Data());

if(args.Length() < 1 || !args[0]->IsObject())
return v8::Undefined();
v8::Handle<v8::Object> object = args[0]->ToObject();

auto stg = module->Global->Get<Settings>("settings");

v8::Handle<v8::Array> keys = object->GetPropertyNames();
for(unsigned int i = 0; i < keys->Length(); ++i)
{
string key = *v8::String::Utf8Value(keys->Get(i));
if(!object->Get(v8::String::New(key.c_str()))->IsArray())
{
HelperDebug::Fail("script", "could not parse (" + key + ") setting");
continue;
}
v8::Handle<v8::Array> values = v8::Handle<v8::Array>::Cast(object->Get(v8::String::New(key.c_str())));

if(!values->Has(0) || !values->Get(0)->IsString())
{
HelperDebug::Fail("script", "could not parse (" + key + ") setting");
continue;
}
string type = *v8::String::Utf8Value(values->Get(0));

if(type == "bool")
{
if(!values->Has(1) || !values->Get(1)->IsBoolean())
{
HelperDebug::Fail("script", "could not parse (" + key + ") setting");
continue;
}
stg->Set<bool>(key, values->Get(1)->BooleanValue());
}
else if(type == "Vector2u")
{
if(!values->Has(1) || !values->Has(2) || !values->Get(1)->IsUint32(), !values->Get(2)->IsUint32())
{
HelperDebug::Fail("script", "could not parse (" + key + ") setting");
continue;
}
stg->Set<Vector2u>(key, Vector2u(values->Get(1)->Uint32Value(), values->Get(2)->Uint32Value()));
}
else if(type == "string")
{
if(!values->Has(1) || !values->Get(1)->IsString())
{
HelperDebug::Fail("script", "could not parse (" + key + ") setting");
continue;
}
stg->Set<string>(key, *v8::String::Utf8Value(values->Get(1)));
}
}

如您所见,我定义了在每个过滤器检查失败时发生的时间。

HelperDebug::Fail("script", "could not parse (" + key + ") setting");
continue;

我只想写一次,但我只能想出一种使用 goto 的方法,我想避免这种方法。是否有更好的选择来重构 if else 结构?

最佳答案

我想我会从一组小类开始为每种类型执行验证步骤:

auto v_string = [](v8::Handle<v8::Array> const &v) { 
return v->Has(1) && v->Get(1)->IsString();
}

auto v_Vector2u = [](v8::Handle<v8::Array> const &v) {
return v->Has(1) && v->Has(2) &&
v->Get(1)->IsUint32() && v->Get(2)->IsUint32();
}
// ...

然后我会创建一个从类型名称到该类型验证器的映射:

std::map<std::string, decltyp(v_string)> verifiers;

verifiers["string"] = v_string;
verifiers["Vector2u"] = v_Vector2u;
// ...

然后为了验证一个类型,你会使用这样的东西:

// find the verifier for this type:
auto verifier = verifiers.find(type);

// If we can't find a verifier, reject the data:
if (verifier == verifiers.end())
HelperDebug::Fail("script", "unknown type: " + type);

// found the verifier -- verify the data:
if (!verifier->second(values))
HelperDebug::Fail("script", "could not parse (" + key + ") setting");

关于c++ - 如何有效地将 if 和 else 用于过滤结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17322158/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com