1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3 4# Copyright (c) 2024 Huawei Device Co., Ltd. 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17from __future__ import absolute_import 18 19import os 20import file_parser 21import make_file_base 22 23# pylint:disable=huawei-redefined-outer-name 24 25# ------------------------------------------------------------------------------------------------------# 26 27def make_function_body(cls): 28 content = ' // ' + cls.get_name() + ' methods.\n' 29 30 flag = False 31 funcs = cls.get_virtual_funcs() 32 for func in funcs: 33 if flag: 34 content += '\n\n' 35 else: 36 flag = True 37 38 content += ' ' + func.get_cpp_proto() + ' override;\n' 39 40 return content 41 42# ------------------------------------------------------------------------------------------------------# 43 44def make_functions_body(cls, header): 45 content = '' 46 parent_cls = cls 47 while True: 48 content += make_function_body(parent_cls) 49 50 parent_clsname = parent_cls.get_parent_name() 51 if file_parser.is_base_class(parent_clsname): 52 break 53 54 parent_cls = header.get_class(parent_clsname) 55 if parent_cls is None: 56 raise Exception('Class does not exist: ' + parent_clsname) 57 if len(content) > 0: 58 content += '\n' 59 60 return content 61 62# ------------------------------------------------------------------------------------------------------# 63 64def make_ctocpp_header_file(header, dir_path, dir_name, class_name): 65 cls = header.get_class(class_name) 66 if cls is None: 67 raise Exception('Class does not exist: ' + class_name) 68 69 content = make_file_base.get_copyright() 70 content += '\n' 71 72 content += \ 73""" 74#ifndef $GUARD$ 75#define $GUARD$ 76#pragma once 77""" 78 content += '\n' 79 80 # build the function body 81 funcs_body = make_functions_body(cls, header) 82 83 # include standard headers 84 if funcs_body.find('std::map') > 0 or funcs_body.find('std::multimap') > 0: 85 content += '#include <map>\n' 86 if funcs_body.find('std::vector') > 0: 87 content += '#include <vector>\n' 88 89 result = make_file_base.make_include_file(cls, 'CToCpp', header, dir_name, class_name) 90 content += result['content'] 91 content += '\n' 92 93 content += 'namespace OHOS::ArkWeb {\n\n' 94 95 content += '// Wrap a C structure with a C++ class.\n' 96 content += '// This class may be instantiated and accessed wrapper-side only.\n' 97 content += make_file_base.make_class_define(cls, 'CToCpp', result['base_name'], class_name, funcs_body) 98 content += '\n' 99 100 content += '} // OHOS::ArkWeb\n\n' 101 102 content += \ 103""" 104#endif // $GUARD$ 105""" 106 107 # add the guard string 108 def_name = make_file_base.make_def_file(cls, class_name) 109 guard = def_name.upper() + '_CTOCPP_H_' 110 content = content.replace('$GUARD$', guard) 111 112 absolute_dir = os.path.join(os.path.join(dir_path, dir_name), 'ctocpp') 113 absolute_path = os.path.join(absolute_dir, file_parser.get_capi_name(class_name, False) + '_ctocpp.h') 114 115 return (content, absolute_path) 116