refactor: more constexpr lookup tables (#38895)

* refactor: more constexpr lookup tables (#38886)

* refactor: use constexpr lookup table in electron_api_web_contents.cc

* refactor: make KeyboardCodeFromStr() private

it is only used as a helper to KeyboardCodeFromStr()

* chore: savepoint

* chore: make lint happy

* fixup! refactor: make KeyboardCodeFromStr() private

* refactor: use constexpr lookup table in electron_url_loader_factory

* refactor: use constexpr lookup table in electron_api_tray

* refactor: use constexpr lookup table in web_contents_preferences.cc

* refactor: use constexpr lookup table in content_converter

* chore: fix clang-format

clang-format gets confused by ifdefs in the middle of an initializer list
This commit is contained in:
Charles Kerr
2023-06-27 01:49:32 -05:00
committed by GitHub
parent 26e6252ac8
commit 236272bf9f
7 changed files with 351 additions and 460 deletions

View File

@@ -9,6 +9,7 @@
#include <string>
#include <utility>
#include "base/containers/fixed_flat_map.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/uuid.h"
@@ -45,22 +46,17 @@ struct Converter<electron::ProtocolType> {
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
electron::ProtocolType* out) {
std::string type;
if (!ConvertFromV8(isolate, val, &type))
return false;
if (type == "buffer")
*out = electron::ProtocolType::kBuffer;
else if (type == "string")
*out = electron::ProtocolType::kString;
else if (type == "file")
*out = electron::ProtocolType::kFile;
else if (type == "http")
*out = electron::ProtocolType::kHttp;
else if (type == "stream")
*out = electron::ProtocolType::kStream;
else // note "free" is internal type, not allowed to be passed from user
return false;
return true;
using Val = electron::ProtocolType;
static constexpr auto Lookup =
base::MakeFixedFlatMapSorted<base::StringPiece, Val>({
// note "free" is internal type, not allowed to be passed from user
{"buffer", Val::kBuffer},
{"file", Val::kFile},
{"http", Val::kHttp},
{"stream", Val::kStream},
{"string", Val::kString},
});
return FromV8WithLookup(isolate, val, Lookup, out);
}
};