printer.h

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

#include <google/protobuf/io/printer.h>
namespace google::protobuf::io

Utility class for writing text to a ZeroCopyOutputStream.

Classes in this file

Records annotations about a Printer's output.
Records annotations about a Printer's output to the given protocol buffer, assuming that the buffer has an ::Annotation message exposing path, source_file, begin and end fields.

class AnnotationCollector

#include <google/protobuf/io/printer.h>
namespace google::protobuf::io

Records annotations about a Printer's output.

Known subclasses:

Members

typedef
std::pair< std::pair< size_t, size_t >, std::string > Annotation
Annotation is a offset range and a payload pair.
virtual void
AddAnnotation(size_t begin_offset, size_t end_offset, const std::string & file_path, const std::vector< int > & path) = 0
Records that the bytes in file_path beginning with begin_offset and ending before end_offset are associated with the SourceCodeInfo-style path.
virtual void
AddAnnotationNew(Annotation & )
virtual
~AnnotationCollector()

virtual void AnnotationCollector::AddAnnotationNew(
        Annotation & )

Just a vector of range, payload pairs stored in a context should suffice.

template class AnnotationProtoCollector: public AnnotationCollector

#include <google/protobuf/io/printer.h>
namespace google::protobuf::io

template <typename >

Records annotations about a Printer's output to the given protocol buffer, assuming that the buffer has an ::Annotation message exposing path, source_file, begin and end fields.

Members

explicit
AnnotationProtoCollector(AnnotationProto * annotation_proto)
annotation_proto is the protocol buffer to which new Annotations should be added. more...
virtual void
AddAnnotation(size_t begin_offset, size_t end_offset, const std::string & file_path, const std::vector< int > & path)
virtual void
AddAnnotationNew(Annotation & a)

explicit AnnotationProtoCollector::AnnotationProtoCollector(
        AnnotationProto * annotation_proto)

annotation_proto is the protocol buffer to which new Annotations should be added.

It is not owned by the AnnotationProtoCollector.

class Printer

#include <google/protobuf/io/printer.h>
namespace google::protobuf::io

Members

Printer(ZeroCopyOutputStream * output, char variable_delimiter)
Create a printer that writes text to the given output stream. more...
Printer(ZeroCopyOutputStream * output, char variable_delimiter, AnnotationCollector * annotation_collector)
Create a printer that writes text to the given output stream. more...
~Printer()
template void
Annotate(const char * varname, const SomeDescriptor * descriptor)
Link a substitution variable emitted by the last call to Print to the object described by descriptor.
template void
Annotate(const char * begin_varname, const char * end_varname, const SomeDescriptor * descriptor)
Link the output range defined by the substitution variables as emitted by the last call to Print to the object described by descriptor. more...
void
Annotate(const char * varname, const std::string & file_name)
Link a substitution variable emitted by the last call to Print to the file with path file_name.
void
Annotate(const char * begin_varname, const char * end_varname, const std::string & file_name)
Link the output range defined by the substitution variables as emitted by the last call to Print to the file with path file_name. more...
void
Print(const std::map< std::string, std::string > & variables, const char * text)
Print some text after applying variable substitutions. more...
template void
Print(const char * text, const Args &... args)
Like the first Print(), except the substitutions are given as parameters.
void
Indent()
Indent text by two spaces. more...
void
Outdent()
Reduces the current indent level by two spaces, or crashes if the indent level is zero.
void
PrintRaw(const std::string & data)
Write a string to the output buffer. more...
void
PrintRaw(const char * data)
Write a zero-delimited string to output buffer. more...
void
WriteRaw(const char * data, int size)
Write some bytes to the output buffer. more...
void
FormatInternal(const std::vector< std::string > & args, const std::map< std::string, std::string > & vars, const char * format)
FormatInternal is a helper function not meant to use directly, use compiler::cpp::Formatter instead. more...
bool
failed() const
True if any write to the underlying stream failed. more...

Printer::Printer(
        ZeroCopyOutputStream * output,
        char variable_delimiter)

Create a printer that writes text to the given output stream.

Use the given character as the delimiter for variables.


Printer::Printer(
        ZeroCopyOutputStream * output,
        char variable_delimiter,
        AnnotationCollector * annotation_collector)

Create a printer that writes text to the given output stream.

Use the given character as the delimiter for variables. If annotation_collector is not null, Printer will provide it with annotations about code written to the stream. annotation_collector is not owned by Printer.


template void Printer::Annotate(
        const char * begin_varname,
        const char * end_varname,
        const SomeDescriptor * descriptor)

Link the output range defined by the substitution variables as emitted by the last call to Print to the object described by descriptor.

The range begins at begin_varname's value and ends after the last character of the value substituted for end_varname.


void Printer::Annotate(
        const char * begin_varname,
        const char * end_varname,
        const std::string & file_name)

Link the output range defined by the substitution variables as emitted by the last call to Print to the file with path file_name.

The range begins at begin_varname's value and ends after the last character of the value substituted for end_varname.


void Printer::Print(
        const std::map< std::string, std::string > & variables,
        const char * text)

Print some text after applying variable substitutions.

If a particular variable in the text is not defined, this will crash. Variables to be substituted are identified by their names surrounded by delimiter characters (as given to the constructor). The variable bindings are defined by the given map.


void Printer::Indent()

Indent text by two spaces.

After calling Indent(), two spaces will be inserted at the beginning of each line of text. Indent() may be called multiple times to produce deeper indents.


void Printer::PrintRaw(
        const std::string & data)

Write a string to the output buffer.

This method does not look for newlines to add indentation.


void Printer::PrintRaw(
        const char * data)

Write a zero-delimited string to output buffer.

This method does not look for newlines to add indentation.


void Printer::WriteRaw(
        const char * data,
        int size)

Write some bytes to the output buffer.

This method does not look for newlines to add indentation.


void Printer::FormatInternal(
        const std::vector< std::string > & args,
        const std::map< std::string, std::string > & vars,
        const char * format)

FormatInternal is a helper function not meant to use directly, use compiler::cpp::Formatter instead.

This function is meant to support formatting text using named variables (eq. "$foo$) from a lookup map (vars) and variables directly supplied by arguments (eq "$1$" meaning first argument which is the zero index element of args).


bool Printer::failed() const

True if any write to the underlying stream failed.

(We don't just crash in this case because this is an I/O failure, not a programming error.)