本文整理汇总了Python中cpp_generator.CppGenerator类的典型用法代码示例。如果您正苦于以下问题:Python CppGenerator类的具体用法?Python CppGenerator怎么用?Python CppGenerator使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了CppGenerator类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _generate_handler_declaration_for_command
def _generate_handler_declaration_for_command(self, command, used_enum_names):
if command.is_async:
return self._generate_async_handler_declaration_for_command(command)
lines = []
parameters = ['ErrorString&']
for _parameter in command.call_parameters:
parameter_name = 'in_' + _parameter.parameter_name
if _parameter.is_optional:
parameter_name = 'opt_' + parameter_name
parameters.append("%s %s" % (CppGenerator.cpp_type_for_unchecked_formal_in_parameter(_parameter), parameter_name))
if isinstance(_parameter.type, EnumType) and _parameter.parameter_name not in used_enum_names:
lines.append(self._generate_anonymous_enum_for_parameter(_parameter, command))
used_enum_names.add(_parameter.parameter_name)
for _parameter in command.return_parameters:
parameter_name = 'out_' + _parameter.parameter_name
if _parameter.is_optional:
parameter_name = 'opt_' + parameter_name
parameters.append("%s %s" % (CppGenerator.cpp_type_for_formal_out_parameter(_parameter), parameter_name))
if isinstance(_parameter.type, EnumType) and _parameter.parameter_name not in used_enum_names:
lines.append(self._generate_anonymous_enum_for_parameter(_parameter, command))
used_enum_names.add(_parameter.parameter_name)
command_args = {
'commandName': command.command_name,
'parameters': ", ".join(parameters)
}
lines.append(' virtual void %(commandName)s(%(parameters)s) = 0;' % command_args)
return '\n'.join(lines)
示例2: _generate_async_dispatcher_class_for_domain
def _generate_async_dispatcher_class_for_domain(self, command, domain):
out_parameter_assignments = []
formal_parameters = []
for parameter in command.return_parameters:
param_args = {
'keyedSetMethod': CppGenerator.cpp_setter_method_for_type(parameter.type),
'parameterKey': parameter.parameter_name,
'parameterName': parameter.parameter_name,
'parameterType': CppGenerator.cpp_type_for_stack_in_parameter(parameter),
}
formal_parameters.append('%s %s' % (CppGenerator.cpp_type_for_formal_async_parameter(parameter), parameter.parameter_name))
if parameter.is_optional:
if CppGenerator.should_use_wrapper_for_return_type(parameter.type):
out_parameter_assignments.append(' if (%(parameterName)s.isAssigned())' % param_args)
out_parameter_assignments.append(' jsonMessage->%(keyedSetMethod)s(ASCIILiteral("%(parameterKey)s"), %(parameterName)s.getValue());' % param_args)
else:
out_parameter_assignments.append(' if (%(parameterName)s)' % param_args)
out_parameter_assignments.append(' jsonMessage->%(keyedSetMethod)s(ASCIILiteral("%(parameterKey)s"), %(parameterName)s);' % param_args)
elif parameter.type.is_enum():
out_parameter_assignments.append(' jsonMessage->%(keyedSetMethod)s(ASCIILiteral("%(parameterKey)s"), Inspector::Protocol::getEnumConstantValue(%(parameterName)s));' % param_args)
else:
out_parameter_assignments.append(' jsonMessage->%(keyedSetMethod)s(ASCIILiteral("%(parameterKey)s"), %(parameterName)s);' % param_args)
async_args = {
'domainName': domain.domain_name,
'callbackName': ucfirst(command.command_name) + 'Callback',
'formalParameters': ", ".join(formal_parameters),
'outParameterAssignments': "\n".join(out_parameter_assignments)
}
return Template(CppTemplates.BackendDispatcherImplementationAsyncCommand).substitute(None, **async_args)
示例3: _generate_async_handler_declaration_for_command
def _generate_async_handler_declaration_for_command(self, command):
callbackName = "%sCallback" % ucfirst(command.command_name)
in_parameters = ['ErrorString&']
for _parameter in command.call_parameters:
parameter_name = 'in_' + _parameter.parameter_name
if _parameter.is_optional:
parameter_name = 'opt_' + parameter_name
in_parameters.append("%s %s" % (CppGenerator.cpp_type_for_unchecked_formal_in_parameter(_parameter), parameter_name))
in_parameters.append("Ref<%s>&& callback" % callbackName)
out_parameters = []
for _parameter in command.return_parameters:
out_parameters.append("%s %s" % (CppGenerator.cpp_type_for_formal_async_parameter(_parameter), _parameter.parameter_name))
class_components = ['class']
export_macro = self.model().framework.setting('export_macro', None)
if export_macro:
class_components.append(export_macro)
command_args = {
'classAndExportMacro': ' '.join(class_components),
'callbackName': callbackName,
'commandName': command.command_name,
'inParameters': ", ".join(in_parameters),
'outParameters': ", ".join(out_parameters),
}
return Template(CppTemplates.BackendDispatcherHeaderAsyncCommandDeclaration).substitute(None, **command_args)
示例4: _generate_typedefs_for_domain
def _generate_typedefs_for_domain(self, domain):
primitive_declarations = filter(lambda decl: isinstance(decl.type, AliasedType), domain.type_declarations)
array_declarations = filter(lambda decl: isinstance(decl.type, ArrayType), domain.type_declarations)
if len(primitive_declarations) == 0 and len(array_declarations) == 0:
return ''
sections = []
for declaration in primitive_declarations:
primitive_name = CppGenerator.cpp_name_for_primitive_type(declaration.type.aliased_type)
typedef_lines = []
if len(declaration.description) > 0:
typedef_lines.append('/* %s */' % declaration.description)
typedef_lines.append('typedef %s %s;' % (primitive_name, declaration.type_name))
sections.append('\n'.join(typedef_lines))
for declaration in array_declarations:
element_type = CppGenerator.cpp_protocol_type_for_type(declaration.type.element_type)
typedef_lines = []
if len(declaration.description) > 0:
typedef_lines.append('/* %s */' % declaration.description)
typedef_lines.append('typedef Inspector::Protocol::Array<%s> %s;' % (element_type, declaration.type_name))
sections.append('\n'.join(typedef_lines))
lines = []
lines.append('namespace %s {' % domain.domain_name)
lines.append('\n'.join(sections))
lines.append('} // %s' % domain.domain_name)
return self.wrap_with_guard_for_domain(domain, '\n'.join(lines))
示例5: _generate_forward_declarations_for_binding_traits
def _generate_forward_declarations_for_binding_traits(self):
# A list of (builder_type, needs_runtime_cast)
type_arguments = []
for domain in self.domains_to_generate():
declarations_to_generate = filter(lambda decl: self.type_needs_shape_assertions(decl.type), domain.type_declarations)
for type_declaration in declarations_to_generate:
for type_member in type_declaration.type_members:
if isinstance(type_member.type, EnumType):
type_arguments.append((CppGenerator.cpp_protocol_type_for_type_member(type_member, type_declaration), False))
if isinstance(type_declaration.type, ObjectType):
type_arguments.append((CppGenerator.cpp_protocol_type_for_type(type_declaration.type), Generator.type_needs_runtime_casts(type_declaration.type)))
struct_keywords = ['struct']
function_keywords = ['static void']
export_macro = self.model().framework.setting('export_macro', None)
if export_macro is not None:
struct_keywords.append(export_macro)
#function_keywords[1:1] = [export_macro]
lines = []
for argument in type_arguments:
lines.append('template<> %s BindingTraits<%s> {' % (' '.join(struct_keywords), argument[0]))
if argument[1]:
lines.append('static RefPtr<%s> runtimeCast(RefPtr<Inspector::InspectorValue>&& value);' % argument[0])
lines.append('#if !ASSERT_DISABLED')
lines.append('%s assertValueHasExpectedType(Inspector::InspectorValue*);' % ' '.join(function_keywords))
lines.append('#endif // !ASSERT_DISABLED')
lines.append('};')
return '\n'.join(lines)
示例6: _generate_declarations_for_enum_conversion_methods
def _generate_declarations_for_enum_conversion_methods(self, domains):
sections = []
sections.append('\n'.join([
'namespace %s {' % self.helpers_namespace(),
'',
'template<typename ProtocolEnumType>',
'Optional<ProtocolEnumType> parseEnumValueFromString(const String&);',
]))
def return_type_with_export_macro(cpp_protocol_type):
enum_return_type = 'Optional<%s>' % cpp_protocol_type
result_terms = [enum_return_type]
export_macro = self.model().framework.setting('export_macro', None)
if export_macro is not None:
result_terms[:0] = [export_macro]
return ' '.join(result_terms)
def type_member_is_anonymous_enum_type(type_member):
return isinstance(type_member.type, EnumType) and type_member.type.is_anonymous
for domain in domains:
type_declarations = self.type_declarations_for_domain(domain)
declaration_types = [decl.type for decl in type_declarations]
object_types = [_type for _type in declaration_types if isinstance(_type, ObjectType)]
enum_types = [_type for _type in declaration_types if isinstance(_type, EnumType)]
if len(object_types) + len(enum_types) == 0:
continue
sorted(object_types, key=methodcaller('raw_name'))
sorted(enum_types, key=methodcaller('raw_name'))
domain_lines = []
domain_lines.append("// Enums in the '%s' Domain" % domain.domain_name)
for enum_type in enum_types:
cpp_protocol_type = CppGenerator.cpp_protocol_type_for_type(enum_type)
domain_lines.append('template<>')
domain_lines.append('%s parseEnumValueFromString<%s>(const String&);' % (return_type_with_export_macro(cpp_protocol_type), cpp_protocol_type))
for object_type in object_types:
for enum_member in filter(type_member_is_anonymous_enum_type, object_type.members):
cpp_protocol_type = CppGenerator.cpp_protocol_type_for_type_member(enum_member, object_type.declaration())
domain_lines.append('template<>')
domain_lines.append('%s parseEnumValueFromString<%s>(const String&);' % (return_type_with_export_macro(cpp_protocol_type), cpp_protocol_type))
if len(domain_lines) == 1:
continue # No real declarations to emit, just the domain comment. Skip.
sections.append(self.wrap_with_guard_for_domain(domain, '\n'.join(domain_lines)))
if len(sections) == 1:
return [] # No real sections to emit, just the namespace and template declaration. Skip.
sections.append('} // namespace %s' % self.helpers_namespace())
return ['\n\n'.join(sections)]
示例7: _generate_enum_conversion_methods_for_domain
def _generate_enum_conversion_methods_for_domain(self, domain):
def type_member_is_anonymous_enum_type(type_member):
return isinstance(type_member.type, EnumType) and type_member.type.is_anonymous
def generate_conversion_method_body(enum_type, cpp_protocol_type):
body_lines = []
body_lines.extend([
'template<>',
'Optional<%s> parseEnumValueFromString<%s>(const String& protocolString)' % (cpp_protocol_type, cpp_protocol_type),
'{',
' static const size_t constantValues[] = {',
])
enum_values = enum_type.enum_values()
for enum_value in enum_values:
body_lines.append(' (size_t)%s::%s,' % (cpp_protocol_type, Generator.stylized_name_for_enum_value(enum_value)))
body_lines.extend([
' };',
' for (size_t i = 0; i < %d; ++i)' % len(enum_values),
' if (protocolString == enum_constant_values[constantValues[i]])',
' return (%s)constantValues[i];' % cpp_protocol_type,
'',
' return WTF::nullopt;',
'}',
'',
])
return body_lines
type_declarations = self.type_declarations_for_domain(domain)
declaration_types = [decl.type for decl in type_declarations]
object_types = [_type for _type in declaration_types if isinstance(_type, ObjectType)]
enum_types = [_type for _type in declaration_types if isinstance(_type, EnumType)]
if len(object_types) + len(enum_types) == 0:
return ''
sorted(object_types, key=methodcaller('raw_name'))
sorted(enum_types, key=methodcaller('raw_name'))
lines = []
lines.append("// Enums in the '%s' Domain" % domain.domain_name)
for enum_type in enum_types:
cpp_protocol_type = CppGenerator.cpp_protocol_type_for_type(enum_type)
lines.extend(generate_conversion_method_body(enum_type, cpp_protocol_type))
for object_type in object_types:
for enum_member in filter(type_member_is_anonymous_enum_type, object_type.members):
cpp_protocol_type = CppGenerator.cpp_protocol_type_for_type_member(enum_member, object_type.declaration())
lines.extend(generate_conversion_method_body(enum_member.type, cpp_protocol_type))
if len(lines) == 1:
return '' # No real declarations to emit, just the domain comment.
return self.wrap_with_guard_for_domain(domain, '\n'.join(lines))
示例8: _generate_assertion_for_object_declaration
def _generate_assertion_for_object_declaration(self, object_declaration):
required_members = [member for member in object_declaration.type_members if not member.is_optional]
optional_members = [member for member in object_declaration.type_members if member.is_optional]
should_count_properties = not Generator.type_has_open_fields(object_declaration.type)
lines = []
lines.append('void BindingTraits<%s>::assertValueHasExpectedType(JSON::Value* value)' % (CppGenerator.cpp_protocol_type_for_type(object_declaration.type)))
lines.append("""{
#if ASSERT_DISABLED
UNUSED_PARAM(value);
#else
ASSERT_ARG(value, value);
RefPtr<JSON::Object> object;
bool castSucceeded = value->asObject(object);
ASSERT_UNUSED(castSucceeded, castSucceeded);""")
for type_member in required_members:
args = {
'memberName': type_member.member_name,
'assertMethod': CppGenerator.cpp_assertion_method_for_type_member(type_member, object_declaration)
}
lines.append(""" {
auto %(memberName)sPos = object->find("%(memberName)s"_s);
ASSERT(%(memberName)sPos != object->end());
%(assertMethod)s(%(memberName)sPos->value.get());
}""" % args)
if should_count_properties:
lines.append('')
lines.append(' int foundPropertiesCount = %s;' % len(required_members))
for type_member in optional_members:
args = {
'memberName': type_member.member_name,
'assertMethod': CppGenerator.cpp_assertion_method_for_type_member(type_member, object_declaration)
}
lines.append(""" {
auto %(memberName)sPos = object->find("%(memberName)s"_s);
if (%(memberName)sPos != object->end()) {
%(assertMethod)s(%(memberName)sPos->value.get());""" % args)
if should_count_properties:
lines.append(' ++foundPropertiesCount;')
lines.append(' }')
lines.append(' }')
if should_count_properties:
lines.append(' if (foundPropertiesCount != object->size())')
lines.append(' FATAL("Unexpected properties in object: %s\\n", object->toJSONString().ascii().data());')
lines.append('#endif // !ASSERT_DISABLED')
lines.append('}')
return '\n'.join(lines)
示例9: _generate_dispatcher_implementation_for_event
def _generate_dispatcher_implementation_for_event(self, event, domain):
lines = []
parameter_assignments = []
formal_parameters = []
for parameter in event.event_parameters:
parameter_value = parameter.parameter_name
if parameter.is_optional and not CppGenerator.should_pass_by_copy_for_return_type(parameter.type):
parameter_value = '*' + parameter_value
if parameter.type.is_enum():
parameter_value = 'Inspector::Protocol::%s::getEnumConstantValue(%s)' % (self.helpers_namespace(), parameter_value)
parameter_args = {
'parameterType': CppGenerator.cpp_type_for_stack_out_parameter(parameter),
'parameterName': parameter.parameter_name,
'parameterValue': parameter_value,
'keyedSetMethod': CppGenerator.cpp_setter_method_for_type(parameter.type),
}
if parameter.is_optional:
parameter_assignments.append(' if (%(parameterName)s)' % parameter_args)
parameter_assignments.append(' paramsObject->%(keyedSetMethod)s(ASCIILiteral("%(parameterName)s"), %(parameterValue)s);' % parameter_args)
else:
parameter_assignments.append(' paramsObject->%(keyedSetMethod)s(ASCIILiteral("%(parameterName)s"), %(parameterValue)s);' % parameter_args)
formal_parameters.append('%s %s' % (CppGenerator.cpp_type_for_checked_formal_event_parameter(parameter), parameter.parameter_name))
event_args = {
'domainName': domain.domain_name,
'eventName': event.event_name,
'formalParameters': ", ".join(formal_parameters)
}
lines.append('void %(domainName)sFrontendDispatcher::%(eventName)s(%(formalParameters)s)' % event_args)
lines.append('{')
lines.append(' Ref<InspectorObject> jsonMessage = InspectorObject::create();')
lines.append(' jsonMessage->setString(ASCIILiteral("method"), ASCIILiteral("%(domainName)s.%(eventName)s"));' % event_args)
if len(parameter_assignments) > 0:
lines.append(' Ref<InspectorObject> paramsObject = InspectorObject::create();')
lines.extend(parameter_assignments)
lines.append(' jsonMessage->setObject(ASCIILiteral("params"), WTFMove(paramsObject));')
lines.append('')
lines.append(' m_frontendRouter.sendEvent(jsonMessage->toJSONString());')
lines.append('}')
return "\n".join(lines)
示例10: _generate_unchecked_setter_for_member
def _generate_unchecked_setter_for_member(self, type_member, domain):
setter_args = {
'camelName': ucfirst(type_member.member_name),
'keyedSet': CppGenerator.cpp_setter_method_for_type(type_member.type),
'name': type_member.member_name,
'parameterType': CppGenerator.cpp_type_for_type_member(type_member)
}
lines = []
lines.append('')
lines.append(' void set%(camelName)s(%(parameterType)s value)' % setter_args)
lines.append(' {')
if isinstance(type_member.type, EnumType):
lines.append(' InspectorObjectBase::%(keyedSet)s(ASCIILiteral("%(name)s"), Inspector::Protocol::getEnumConstantValue(static_cast<int>(value)));' % setter_args)
else:
lines.append(' InspectorObjectBase::%(keyedSet)s(ASCIILiteral("%(name)s"), value);' % setter_args)
lines.append(' }')
return '\n'.join(lines)
示例11: TestCppGenerator
class TestCppGenerator(TestCase):
def setUp(self):
self.generator = CppGenerator()
def check_if_generated_code_for_fake_is(self, fake, expected):
generated = self.generator.generate(fake)
self.assertEquals(expected, generated)
def test_cpp_generator_should_generate_namespace_for_unknown_type(self):
fakes = [{'type': 'namespace', 'name': 'ShortStr'}]
expected = 'namespace ShortStr;'
self.check_if_generated_code_for_fake_is(fakes, expected)
def test_cpp_generator_should_generate_class_for_unknown_type(self):
fakes = [{'type': 'class', 'name': 'ShortStr'}]
expected = 'class ShortStr;'
self.check_if_generated_code_for_fake_is(fakes, expected)
def test_cpp_generator_should_generate_variable_declaration(self):
fakes = [{'type': 'field', 'declaration': 'int', 'name': 'ma'}]
expected = 'int ma;'
self.check_if_generated_code_for_fake_is(fakes, expected)
def test_cpp_generator_should_generate_function_declaration(self):
fakes = [
{'type': 'method', 'declaration': 'void (int)', 'name': 'foo'}
]
expected = 'void foo(int);'
self.check_if_generated_code_for_fake_is(fakes, expected)
def test_cpp_generator_should_generate_members_if_provided(self):
fakes = [{
'type': 'class',
'name': 'ShortStr',
'members': [
{
'type': 'method',
'declaration': 'void ()',
'name': 'Foo',
'access': 'public'
},
{
'type': 'method',
'declaration': 'void ()',
'name': 'Bar',
'access': 'public'
}
]
}]
expected = 'class ShortStr{\n'\
'\tpublic:\n'\
'\tvoid Foo();\n'\
'\tvoid Bar();\n'\
'};\n'
self.check_if_generated_code_for_fake_is(fakes, expected)
示例12: _generate_dispatcher_declaration_for_event
def _generate_dispatcher_declaration_for_event(self, event, domain, used_enum_names):
formal_parameters = []
lines = []
for parameter in event.event_parameters:
formal_parameters.append('%s %s' % (CppGenerator.cpp_type_for_checked_formal_event_parameter(parameter), parameter.parameter_name))
if isinstance(parameter.type, EnumType) and parameter.parameter_name not in used_enum_names:
lines.append(self._generate_anonymous_enum_for_parameter(parameter, event))
used_enum_names.add(parameter.parameter_name)
lines.append(" void %s(%s);" % (event.event_name, ", ".join(formal_parameters)))
return "\n".join(lines)
示例13: _generate_unchecked_setter_for_member
def _generate_unchecked_setter_for_member(self, type_member, domain):
setter_args = {
'camelName': ucfirst(type_member.member_name),
'keyedSet': CppGenerator.cpp_setter_method_for_type(type_member.type),
'name': type_member.member_name,
'parameterType': CppGenerator.cpp_type_for_type_member(type_member),
'helpersNamespace': self.helpers_namespace(),
}
lines = []
lines.append('')
lines.append(' void set%(camelName)s(%(parameterType)s value)' % setter_args)
lines.append(' {')
if isinstance(type_member.type, EnumType):
lines.append(' JSON::ObjectBase::%(keyedSet)s("%(name)s"_s, Inspector::Protocol::%(helpersNamespace)s::getEnumConstantValue(value));' % setter_args)
elif CppGenerator.should_use_references_for_type(type_member.type):
lines.append(' JSON::ObjectBase::%(keyedSet)s("%(name)s"_s, WTFMove(value));' % setter_args)
else:
lines.append(' JSON::ObjectBase::%(keyedSet)s("%(name)s"_s, value);' % setter_args)
lines.append(' }')
return '\n'.join(lines)
示例14: _generate_builder_setter_for_member
def _generate_builder_setter_for_member(self, type_member, domain):
setter_args = {
'camelName': ucfirst(type_member.member_name),
'keyedSet': CppGenerator.cpp_setter_method_for_type(type_member.type),
'name': type_member.member_name,
'parameterType': CppGenerator.cpp_type_for_type_member(type_member)
}
lines = []
lines.append('')
lines.append(' Builder<STATE | %(camelName)sSet>& set%(camelName)s(%(parameterType)s value)' % setter_args)
lines.append(' {')
lines.append(' COMPILE_ASSERT(!(STATE & %(camelName)sSet), property_%(name)s_already_set);' % setter_args)
if isinstance(type_member.type, EnumType):
lines.append(' m_result->%(keyedSet)s(ASCIILiteral("%(name)s"), Inspector::Protocol::getEnumConstantValue(static_cast<int>(value)));' % setter_args)
else:
lines.append(' m_result->%(keyedSet)s(ASCIILiteral("%(name)s"), value);' % setter_args)
lines.append(' return castState<%(camelName)sSet>();' % setter_args)
lines.append(' }')
return '\n'.join(lines)
示例15: _generate_handler_declaration_for_command
def _generate_handler_declaration_for_command(self, command):
lines = []
parameters = ['long callId']
for _parameter in command.call_parameters:
parameters.append('%s in_%s' % (CppGenerator.cpp_type_for_unchecked_formal_in_parameter(_parameter), _parameter.parameter_name))
command_args = {
'commandName': command.command_name,
'parameters': ', '.join(parameters),
}
lines.append(' virtual void %(commandName)s(%(parameters)s) = 0;' % command_args)
return '\n'.join(lines)
开发者ID:wolfviking0,项目名称:webcl-webkit,代码行数:12,代码来源:generate_cpp_alternate_backend_dispatcher_header.py