code_generator.h

This section contains reference documentation for working with protocol buffer classes in C++.

#include <google/protobuf/compiler/code_generator.h>
namespace google::protobuf::compiler

Defines the abstract interface implemented by each of the language-specific code generators.

Classes in this file

The abstract interface to a class which generates code implementing a particular proto file in a particular language.
CodeGenerators generate one or more files in a given directory.

File Members

These definitions are not part of any class.
typedef
GeneratorContext OutputDirectory
The type GeneratorContext was once called OutputDirectory. more...
void
ParseGeneratorParameter(const std::string & , std::vector< std::pair< std::string, std::string > > * )
Several code generators treat the parameter argument as holding a list of options separated by commas. more...
std::string
StripProto(const std::string & filename)
Strips ".proto" or ".protodevel" from the end of a filename.

typedef compiler::OutputDirectory

The type GeneratorContext was once called OutputDirectory.

This typedef provides backward compatibility.


void compiler::ParseGeneratorParameter(
        const std::string & ,
        std::vector< std::pair< std::string, std::string > > * )

Several code generators treat the parameter argument as holding a list of options separated by commas.

This helper function parses a set of comma-delimited name/value pairs: e.g., "foo=bar,baz,qux=corge" parses to the pairs:

("foo", "bar"), ("baz", ""), ("qux", "corge")

class CodeGenerator

#include <google/protobuf/compiler/code_generator.h>
namespace google::protobuf::compiler

The abstract interface to a class which generates code implementing a particular proto file in a particular language.

A number of these may be registered with CommandLineInterface to support various languages.

Known subclasses:

Members

enum
Feature
Sync with plugin.proto. more...
CodeGenerator()
virtual
~CodeGenerator()
virtual bool
Generate(const FileDescriptor * file, const std::string & parameter, GeneratorContext * generator_context, std::string * error) const = 0
Generates code for the given proto file, generating one or more files in the given output directory. more...
virtual bool
GenerateAll(const std::vector< const FileDescriptor * > & files, const std::string & parameter, GeneratorContext * generator_context, std::string * error) const
Generates code for all given proto files. more...
virtual uint64_t
GetSupportedFeatures() const
Implement this to indicate what features this code generator supports. more...
virtual bool
HasGenerateAll() const
This is no longer used, but this class is part of the opensource protobuf library, so it has to remain to keep vtables the same for the current version of the library. more...

enum CodeGenerator::Feature {
  FEATURE_PROTO3_OPTIONAL = = 1
}

Sync with plugin.proto.

FEATURE_PROTO3_OPTIONAL

virtual bool CodeGenerator::Generate(
        const FileDescriptor * file,
        const std::string & parameter,
        GeneratorContext * generator_context,
        std::string * error) const = 0

Generates code for the given proto file, generating one or more files in the given output directory.

A parameter to be passed to the generator can be specified on the command line. This is intended to be used to pass generator specific parameters. It is empty if no parameter was given. ParseGeneratorParameter (below), can be used to accept multiple parameters within the single parameter command line flag.

Returns true if successful. Otherwise, sets *error to a description of the problem (e.g. "invalid parameter") and returns false.


virtual bool CodeGenerator::GenerateAll(
        const std::vector< const FileDescriptor * > & files,
        const std::string & parameter,
        GeneratorContext * generator_context,
        std::string * error) const

Generates code for all given proto files.

WARNING: The canonical code generator design produces one or two output files per input .proto file, and we do not wish to encourage alternate designs.

A parameter is given as passed on the command line, as in |Generate()| above.

Returns true if successful. Otherwise, sets *error to a description of the problem (e.g. "invalid parameter") and returns false.


virtual uint64_t CodeGenerator::GetSupportedFeatures() const

Implement this to indicate what features this code generator supports.

This should be a bitwise OR of features from the Features enum in plugin.proto.


virtual bool CodeGenerator::HasGenerateAll() const

This is no longer used, but this class is part of the opensource protobuf library, so it has to remain to keep vtables the same for the current version of the library.

When protobufs does a api breaking change, the method can be removed.

class GeneratorContext

#include <google/protobuf/compiler/code_generator.h>
namespace google::protobuf::compiler

CodeGenerators generate one or more files in a given directory.

This abstract interface represents the directory to which the CodeGenerator is to write and other information about the context in which the Generator runs.

Members

GeneratorContext()
virtual
~GeneratorContext()
virtual io::ZeroCopyOutputStream *
Open(const std::string & filename) = 0
Opens the given file, truncating it if it exists, and returns a ZeroCopyOutputStream that writes to the file. more...
virtual io::ZeroCopyOutputStream *
OpenForAppend(const std::string & filename)
Similar to Open() but the output will be appended to the file if exists.
virtual io::ZeroCopyOutputStream *
OpenForInsert(const std::string & filename, const std::string & insertion_point)
Creates a ZeroCopyOutputStream which will insert code into the given file at the given insertion point. more...
virtual io::ZeroCopyOutputStream *
OpenForInsertWithGeneratedCodeInfo(const std::string & filename, const std::string & insertion_point, const google::protobuf::GeneratedCodeInfo & info)
Similar to OpenForInsert, but if info is non-empty, will open (or create) filename.pb.meta and insert info at the appropriate place with the necessary shifts. more...
virtual void
ListParsedFiles(std::vector< const FileDescriptor * > * output)
Returns a vector of FileDescriptors for all the files being compiled in this run. more...
virtual void
GetCompilerVersion(Version * version) const
Retrieves the version number of the protocol compiler associated with this GeneratorContext.

virtual io::ZeroCopyOutputStream *
    GeneratorContext::Open(
        const std::string & filename) = 0

Opens the given file, truncating it if it exists, and returns a ZeroCopyOutputStream that writes to the file.

The caller takes ownership of the returned object. This method never fails (a dummy stream will be returned instead).

The filename given should be relative to the root of the source tree. E.g. the C++ generator, when generating code for "foo/bar.proto", will generate the files "foo/bar.pb.h" and "foo/bar.pb.cc"; note that "foo/" is included in these filenames. The filename is not allowed to contain "." or ".." components.


virtual io::ZeroCopyOutputStream *
    GeneratorContext::OpenForInsert(
        const std::string & filename,
        const std::string & insertion_point)

Creates a ZeroCopyOutputStream which will insert code into the given file at the given insertion point.

See plugin.proto (plugin.pb.h) for more information on insertion points. The default implementation assert-fails – it exists only for backwards-compatibility.

WARNING: This feature is currently EXPERIMENTAL and is subject to change.


virtual io::ZeroCopyOutputStream *
    GeneratorContext::OpenForInsertWithGeneratedCodeInfo(
        const std::string & filename,
        const std::string & insertion_point,
        const google::protobuf::GeneratedCodeInfo & info)

Similar to OpenForInsert, but if info is non-empty, will open (or create) filename.pb.meta and insert info at the appropriate place with the necessary shifts.

The default implementation ignores info.

WARNING: This feature will be REMOVED in the near future.


virtual void GeneratorContext::ListParsedFiles(
        std::vector< const FileDescriptor * > * output)

Returns a vector of FileDescriptors for all the files being compiled in this run.

Useful for languages, such as Go, that treat files differently when compiled as a set rather than individually.