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, getLogErrInfo } = require('../tools/common'); 17const { NapiLog } = require('../tools/NapiLog'); 18function generateEnum(name, data, inNamespace, nameSpaceName, toolNamespace) { 19 let implH = ''; 20 let implCpp = ''; 21 let midInitEnum = ''; 22 let midInitEnumDefine = ''; 23 24 if (data.enumValueType === EnumValueType.ENUM_VALUE_TYPE_STRING) { 25 implH = `\nclass %s {\npublic:\n`.format(name, implH); 26 } else if (data.enumValueType === EnumValueType.ENUM_VALUE_TYPE_NUMBER) { 27 implH = `\nenum class %s {\n`.format(name, implH); 28 } else { 29 NapiLog.logError(`The enum type[%s] is not support.`.format(data.enumValueType), getLogErrInfo()); 30 return {implH: '', implCpp: ''}; 31 } 32 for (let i in data.element) { 33 let v = data.element[i]; 34 if (midInitEnumDefine === '') { 35 midInitEnumDefine += 'std::map<const char *, std::any> enumMap%s;\n'.format(name); 36 } 37 38 if (data.enumValueType === EnumValueType.ENUM_VALUE_TYPE_STRING) { 39 implH += ` static const std::string %s;\n`.format(v.name); 40 implCpp += `\nconst std::string %s::%s = "%s";\n`.format(name, v.name, v.value); 41 midInitEnum += ' %s%s::%senumMap%s["%s"] = "%s";\n'.format(inNamespace, nameSpaceName, toolNamespace, name, v.name, v.value); 42 } else { 43 if (v.value === '') { 44 v.value = 0; 45 } 46 implH += ` %s = %s,\n`.format(v.name, v.value); 47 midInitEnum += ' %s%s::%senumMap%s["%s"] = %s;\n'.format(inNamespace, nameSpaceName, toolNamespace, name, v.name, v.value); 48 } 49 } 50 midInitEnum += ' pxt->CreateEnumObject("%s", %s%s::%senumMap%s);\n'.format(name, inNamespace, nameSpaceName, toolNamespace, name); 51 implH += `};\n`; 52 let result = { 53 implH: implH, 54 implCpp: implCpp, 55 midInitEnum: midInitEnum, 56 midInitEnumDefine: midInitEnumDefine 57 }; 58 return result; 59} 60module.exports = { 61 generateEnum 62};