• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2017 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef SKSL_ENUM
9 #define SKSL_ENUM
10 
11 #include "SkSLProgramElement.h"
12 
13 #include <algorithm>
14 
15 namespace SkSL {
16 
17 struct Enum : public ProgramElement {
EnumEnum18     Enum(int offset, StringFragment typeName, std::shared_ptr<SymbolTable> symbols)
19     : INHERITED(offset, kEnum_Kind)
20     , fTypeName(typeName)
21     , fSymbols(std::move(symbols)) {}
22 
cloneEnum23     std::unique_ptr<ProgramElement> clone() const override {
24         return std::unique_ptr<ProgramElement>(new Enum(fOffset, fTypeName, fSymbols));
25     }
26 
descriptionEnum27     String description() const override {
28         String result = "enum class " + fTypeName + " {\n";
29         String separator;
30         std::vector<const Symbol*> sortedSymbols;
31         for (const auto& pair : *fSymbols) {
32             sortedSymbols.push_back(pair.second);
33         }
34         std::sort(sortedSymbols.begin(), sortedSymbols.end(),
35                   [](const Symbol* a, const Symbol* b) { return a->fName < b->fName; });
36         for (const auto& s : sortedSymbols) {
37             result += separator + "    " + s->fName + " = " +
38                       ((Variable*) s)->fInitialValue->description();
39             separator = ",\n";
40         }
41         result += "\n};";
42         return result;
43     }
44 
45     bool fBuiltin = false;
46     const StringFragment fTypeName;
47     const std::shared_ptr<SymbolTable> fSymbols;
48 
49     typedef ProgramElement INHERITED;
50 };
51 
52 } // namespace
53 
54 #endif
55