My favorites | English | Sign in

Protocol Buffers

printer.h

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

Utility class for writing text to a ZeroCopyOutputStream.

Classes in this file

This simple utility class assists in code generation.

class Printer

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

This simple utility class assists in code generation.

It basically allows the caller to define a set of variables and then output some text with variable substitutions. Example usage:

Printer printer(output, '$');
map<string, string> vars;
vars["name"] = "Bob";
printer.Print(vars, "My name is $name$.");

The above writes "My name is Bob." to the output stream.

Printer aggressively enforces correct usage, crashing (with assert failures) in the case of undefined variables. This helps greatly in debugging code which uses it. This class is not intended to be used by production servers.

Members

Printer(ZeroCopyOutputStream * output, char variable_delimiter)
Create a printer that writes text to the given output stream. more...
~Printer()
void
Print(const map< string, string > & variables, const char * text)
Print some text after applying variable substitutions. more...
void
Print(const char * text)
Like the first Print(), except the substitutions are given as parameters.
void
Print(const char * text, const char * variable, const string & value)
Like the first Print(), except the substitutions are given as parameters.
void
Print(const char * text, const char * variable1, const string & value1, const char * variable2, const string & value2)
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.
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.


void Printer::Print(
        const map< string, 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.


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.)