• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_function_body_block(cls):
10  impl = '  // ' + cls.get_name() + ' methods.\n'
11
12  funcs = cls.get_virtual_funcs()
13  for func in funcs:
14    impl += '  ' + func.get_cpp_proto()
15    if cls.is_client_side():
16      impl += ' override;\n'
17    else:
18      impl += ' override;\n'
19
20  return impl
21
22
23def make_function_body(header, cls):
24  impl = make_function_body_block(cls)
25
26  cur_cls = cls
27  while True:
28    parent_name = cur_cls.get_parent_name()
29    if is_base_class(parent_name):
30      break
31    else:
32      parent_cls = header.get_class(parent_name)
33      if parent_cls is None:
34        raise Exception('Class does not exist: ' + parent_name)
35      if len(impl) > 0:
36        impl += '\n'
37      impl += make_function_body_block(parent_cls)
38    cur_cls = header.get_class(parent_name)
39
40  return impl
41
42
43def make_ctocpp_header(header, clsname):
44  cls = header.get_class(clsname)
45  if cls is None:
46    raise Exception('Class does not exist: ' + clsname)
47
48  clientside = cls.is_client_side()
49
50  directory = cls.get_file_directory()
51  defname = ''
52  if not directory is None:
53    defname += directory + '_'
54  defname += get_capi_name(clsname[3:], False)
55  defname = defname.upper()
56
57  capiname = cls.get_capi_name()
58
59  result = get_copyright()
60
61  result += '#ifndef CEF_LIBCEF_DLL_CTOCPP_'+defname+'_CTOCPP_H_\n'+ \
62            '#define CEF_LIBCEF_DLL_CTOCPP_'+defname+'_CTOCPP_H_\n' + \
63            '#pragma once\n'
64
65  if clientside:
66    result += """
67#if !defined(BUILDING_CEF_SHARED)
68#error This file can be included DLL-side only
69#endif
70"""
71  else:
72    result += """
73#if !defined(WRAPPING_CEF_SHARED)
74#error This file can be included wrapper-side only
75#endif
76"""
77
78  # build the function body
79  func_body = make_function_body(header, cls)
80
81  # include standard headers
82  if func_body.find('std::map') > 0 or func_body.find('std::multimap') > 0:
83    result += '\n#include <map>'
84  if func_body.find('std::vector') > 0:
85    result += '\n#include <vector>'
86
87  # include the headers for this class
88  result += '\n#include "include/'+cls.get_file_name()+'"'+ \
89            '\n#include "include/capi/'+cls.get_capi_file_name()+'"\n'
90
91  # include headers for any forward declared classes that are not in the same file
92  declares = cls.get_forward_declares()
93  for declare in declares:
94    dcls = header.get_class(declare)
95    if dcls.get_file_name() != cls.get_file_name():
96      result += '#include "include/'+dcls.get_file_name()+'"\n' \
97                '#include "include/capi/'+dcls.get_capi_file_name()+'"\n'
98
99  base_class_name = header.get_base_class_name(clsname)
100  base_scoped = True if base_class_name == 'CefBaseScoped' else False
101  if base_scoped:
102    template_file = 'ctocpp_scoped.h'
103    template_class = 'CefCToCppScoped'
104  else:
105    template_file = 'ctocpp_ref_counted.h'
106    template_class = 'CefCToCppRefCounted'
107
108  result += '#include "libcef_dll/ctocpp/' + template_file + '"'
109  result += '\n\n// Wrap a C structure with a C++ class.\n'
110
111  if clientside:
112    result += '// This class may be instantiated and accessed DLL-side only.\n'
113  else:
114    result += '// This class may be instantiated and accessed wrapper-side only.\n'
115
116  result +=   'class '+clsname+'CToCpp\n'+ \
117              '    : public ' + template_class + '<'+clsname+'CToCpp, '+clsname+', '+capiname+'> {\n'+ \
118              ' public:\n'+ \
119              '  '+clsname+'CToCpp();\n'+ \
120              '  virtual ~'+clsname+'CToCpp();\n\n'
121
122  result += func_body
123  result += '};\n\n'
124
125  result += '#endif  // CEF_LIBCEF_DLL_CTOCPP_' + defname + '_CTOCPP_H_'
126
127  return result
128
129
130def write_ctocpp_header(header, clsname, dir):
131  # give the output file the same directory offset as the input file
132  cls = header.get_class(clsname)
133  dir = os.path.dirname(os.path.join(dir, cls.get_file_name()))
134  file = os.path.join(dir, get_capi_name(clsname[3:], False) + '_ctocpp.h')
135
136  newcontents = make_ctocpp_header(header, clsname)
137  return (file, newcontents)
138
139
140# test the module
141if __name__ == "__main__":
142  import sys
143
144  # verify that the correct number of command-line arguments are provided
145  if len(sys.argv) < 3:
146    sys.stderr.write('Usage: ' + sys.argv[0] + ' <infile> <classname>')
147    sys.exit()
148
149  # create the header object
150  header = obj_header()
151  header.add_file(sys.argv[1])
152
153  # dump the result to stdout
154  sys.stdout.write(make_ctocpp_header(header, sys.argv[2]))
155