From db6ad656ee5e2dcd1c72e453d9aacce055a73c38 Mon Sep 17 00:00:00 2001 From: Brian Osman Date: Wed, 3 Mar 2021 12:51:31 -0500 Subject: [PATCH] Add Enum::foreach(), change EnumValue to return SKSL_INT This gives a nicer interface (name + SKSL_INT) to getting all the values of an enum (rather than iterating over the symbol table). Will be used in a follow-up CL. Change-Id: I1c104623bd06f770c57b88ca9996d3e8c87a3ca7 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/379057 Commit-Queue: Brian Osman Auto-Submit: Brian Osman Reviewed-by: John Stiles --- src/sksl/ir/SkSLEnum.h | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/sksl/ir/SkSLEnum.h b/src/sksl/ir/SkSLEnum.h index c477a8d8ed..478b841fb2 100644 --- a/src/sksl/ir/SkSLEnum.h +++ b/src/sksl/ir/SkSLEnum.h @@ -57,15 +57,16 @@ public: String code() const { String result = "enum class " + this->typeName() + " {\n"; String separator; - std::vector sortedSymbols; + struct Enumerant { StringFragment name; SKSL_INT value; }; + std::vector sortedSymbols; sortedSymbols.reserve(symbols()->count()); - this->symbols()->foreach([&](StringFragment, const Symbol* symbol) { - sortedSymbols.push_back(symbol); + this->foreach([&](StringFragment name, SKSL_INT value){ + sortedSymbols.push_back({name, value}); }); std::sort(sortedSymbols.begin(), sortedSymbols.end(), - [](const Symbol* a, const Symbol* b) { return EnumValue(a) < EnumValue(b); }); - for (const Symbol* s : sortedSymbols) { - result += separator + " " + s->name() + " = " + to_string(EnumValue(s)); + [](const Enumerant& a, const Enumerant& b) { return a.value < b.value; }); + for (const auto& entry : sortedSymbols) { + result += separator + " " + entry.name + " = " + to_string(entry.value); separator = ",\n"; } result += "\n};"; @@ -76,8 +77,13 @@ public: return this->code(); } + template void foreach(Fn&& fn) const { + this->symbols()->foreach( + [&fn](StringFragment name, const Symbol* symbol) { fn(name, EnumValue(symbol)); }); + } + private: - static int EnumValue(const Symbol* symbol) { + static SKSL_INT EnumValue(const Symbol* symbol) { return symbol->as().initialValue()->as().value(); }