1# Copyright (c) 2011 The Chromium Embedded Framework Authors. All rights 2# reserved. Use of this source code is governed by a BSD-style license that 3# can be found in the LICENSE file. 4 5from __future__ import absolute_import 6from cef_parser import * 7 8 9def make_cpptoc_header(header, clsname): 10 cls = header.get_class(clsname) 11 if cls is None: 12 raise Exception('Class does not exist: ' + clsname) 13 14 dllside = cls.is_library_side() 15 16 directory = cls.get_file_directory() 17 defname = '' 18 if not directory is None: 19 defname += directory + '_' 20 defname += get_capi_name(clsname[3:], False) 21 defname = defname.upper() 22 23 capiname = cls.get_capi_name() 24 25 result = get_copyright() 26 27 result += '#ifndef CEF_LIBCEF_DLL_CPPTOC_'+defname+'_CPPTOC_H_\n'+ \ 28 '#define CEF_LIBCEF_DLL_CPPTOC_'+defname+'_CPPTOC_H_\n' + \ 29 '#pragma once\n' 30 31 if dllside: 32 result += """ 33#if !defined(BUILDING_CEF_SHARED) 34#error This file can be included DLL-side only 35#endif 36""" 37 else: 38 result += """ 39#if !defined(WRAPPING_CEF_SHARED) 40#error This file can be included wrapper-side only 41#endif 42""" 43 44 # include the headers for this class 45 result += '\n#include "include/'+cls.get_file_name()+'"\n' \ 46 '#include "include/capi/'+cls.get_capi_file_name()+'"\n' 47 48 # include headers for any forward declared classes that are not in the same file 49 declares = cls.get_forward_declares() 50 for declare in declares: 51 dcls = header.get_class(declare) 52 if dcls.get_file_name() != cls.get_file_name(): 53 result += '#include "include/'+dcls.get_file_name()+'"\n' \ 54 '#include "include/capi/'+dcls.get_capi_file_name()+'"\n' 55 56 base_class_name = header.get_base_class_name(clsname) 57 base_scoped = True if base_class_name == 'CefBaseScoped' else False 58 if base_scoped: 59 template_file = 'cpptoc_scoped.h' 60 template_class = 'CefCppToCScoped' 61 else: 62 template_file = 'cpptoc_ref_counted.h' 63 template_class = 'CefCppToCRefCounted' 64 65 result += '#include "libcef_dll/cpptoc/' + template_file + '"' 66 result += '\n\n// Wrap a C++ class with a C structure.\n' 67 68 if dllside: 69 result += '// This class may be instantiated and accessed DLL-side only.\n' 70 else: 71 result += '// This class may be instantiated and accessed wrapper-side only.\n' 72 73 result += 'class '+clsname+'CppToC\n'+ \ 74 ' : public ' + template_class + '<'+clsname+'CppToC, '+clsname+', '+capiname+'> {\n'+ \ 75 ' public:\n'+ \ 76 ' '+clsname+'CppToC();\n'+ \ 77 ' virtual ~'+clsname+'CppToC();\n'+ \ 78 '};\n\n' 79 80 result += '#endif // CEF_LIBCEF_DLL_CPPTOC_' + defname + '_CPPTOC_H_' 81 82 return result 83 84 85def write_cpptoc_header(header, clsname, dir): 86 # give the output file the same directory offset as the input file 87 cls = header.get_class(clsname) 88 dir = os.path.dirname(os.path.join(dir, cls.get_file_name())) 89 file = os.path.join(dir, get_capi_name(clsname[3:], False) + '_cpptoc.h') 90 91 newcontents = make_cpptoc_header(header, clsname) 92 return (file, newcontents) 93 94 95# test the module 96if __name__ == "__main__": 97 import sys 98 99 # verify that the correct number of command-line arguments are provided 100 if len(sys.argv) < 3: 101 sys.stderr.write('Usage: ' + sys.argv[0] + ' <infile> <classname>') 102 sys.exit() 103 104 # create the header object 105 header = obj_header() 106 header.add_file(sys.argv[1]) 107 108 # dump the result to stdout 109 sys.stdout.write(make_cpptoc_header(header, sys.argv[2])) 110