• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2* Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development Co., Ltd.
3* Licensed under the Apache License, Version 2.0 (the "License");
4* you may not use this file except in compliance with the License.
5* You may obtain a copy of the License at
6*
7* http://www.apache.org/licenses/LICENSE-2.0
8*
9* Unless required by applicable law or agreed to in writing, software
10* distributed under the License is distributed on an "AS IS" BASIS,
11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12* See the License for the specific language governing permissions and
13* limitations under the License.
14*/
15const re = require("../tools/re");
16const { EnumValueType } = require("../tools/common");
17const { NapiLog } = require("../tools/NapiLog");
18function generateEnum(name, data) {
19    let implH = ""
20    let implCpp = ""
21    let midInitEnum = ""
22
23    if (data.enumValueType == EnumValueType.ENUM_VALUE_TYPE_STRING) {
24        implH = `\nclass %s {\npublic:\n`.format(name, implH)
25    } else if (data.enumValueType == EnumValueType.ENUM_VALUE_TYPE_NUMBER){
26        implH = `\nenum %s {\n`.format(name, implH)
27    } else {
28        NapiLog.logError(`The enum type[%s] is not support`.format(data.enumValueType));
29        return {implH: "", implCpp: ""}
30    }
31    for (let i in data.element) {
32        let v = data.element[i]
33        if(midInitEnum == "") {
34            midInitEnum += '    std::map<const char *, std::any> enumMap%s;\n'.format(name)
35        }
36
37        if (data.enumValueType == EnumValueType.ENUM_VALUE_TYPE_STRING) {
38            implH += `    static const std::string %s;\n`.format(v.name)
39            implCpp += `\nconst std::string %s::%s = "%s";\n`.format(name, v.name, v.value)
40            midInitEnum += '    enumMap%s["%s"] = "%s";\n'.format(name, v.name, v.value)
41        } else {
42            if (v.value == '') {
43                v.value = 0
44            }
45            implH += `    %s = %s,\n`.format(v.name, v.value)
46            midInitEnum += '    enumMap%s["%s"] = %s;\n'.format(name, v.name, v.value)
47        }
48    }
49    midInitEnum += '    pxt->CreateEnumObject("%s", enumMap%s);\n'.format(name, name)
50    implH += `};\n`
51    let result = {
52        implH: implH,
53        implCpp: implCpp,
54        midInitEnum: midInitEnum
55    }
56    return result
57}
58module.exports = {
59    generateEnum
60}