def forward_declare( node: ProtoNode, root: ProtoNode, output: OutputFile, ) -> None: """Generates code forward-declaring entities in a message's namespace.""" if node.type() != ProtoNode.Type.MESSAGE: return namespace = node.cpp_namespace(root) output.write_line() output.write_line(f'namespace {namespace} {{') # Define an enum defining each of the message's fields and their numbers. output.write_line('enum class Fields {') with output.indent(): for field in node.fields(): output.write_line(f'{field.enum_name()} = {field.number()},') output.write_line('};') # Declare the message's encoder class and all of its enums. output.write_line() output.write_line('class Encoder;') for child in node.children(): if child.type() == ProtoNode.Type.ENUM: output.write_line() generate_code_for_enum(child, node, output) output.write_line(f'}} // namespace {namespace}')
def generate_code_for_package(file_descriptor_proto, package: ProtoNode, output: OutputFile) -> None: """Generates code for a single .pb.h file corresponding to a .proto file.""" assert package.type() == ProtoNode.Type.PACKAGE output.write_line(f'// {os.path.basename(output.name())} automatically ' f'generated by {PLUGIN_NAME} {PLUGIN_VERSION}') output.write_line('#pragma once\n') output.write_line('#include <cstddef>') output.write_line('#include <cstdint>\n') output.write_line('#include "pw_protobuf/codegen.h"') for imported_file in file_descriptor_proto.dependency: generated_header = _proto_filename_to_generated_header(imported_file) output.write_line(f'#include "{generated_header}"') if package.cpp_namespace(): file_namespace = package.cpp_namespace() if file_namespace.startswith('::'): file_namespace = file_namespace[2:] output.write_line(f'\nnamespace {file_namespace} {{') for node in package: forward_declare(node, package, output) # Define all top-level enums. for node in package.children(): if node.type() == ProtoNode.Type.ENUM: output.write_line() generate_code_for_enum(node, package, output) # Run through all messages in the file, generating a class for each. for node in package: if node.type() == ProtoNode.Type.MESSAGE: output.write_line() generate_code_for_message(node, package, output) # Run a second pass through the classes, this time defining all of the # methods which were previously only declared. for node in package: if node.type() == ProtoNode.Type.MESSAGE: define_not_in_class_methods(node, package, output) if package.cpp_namespace(): output.write_line(f'\n}} // namespace {package.cpp_namespace()}')