// Protocol Buffers - Google's data interchange format // Copyright 2008 Google Inc. All rights reserved. // https://developers.google.com/protocol-buffers/ // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are // met: // // * Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above // copyright notice, this list of conditions and the following disclaimer // in the documentation and/or other materials provided with the // distribution. // * Neither the name of Google Inc. nor the names of its // contributors may be used to endorse or promote products derived from // this software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // Author: [email protected] (Kenton Varda) // Based on original Protocol Buffers design by // Sanjay Ghemawat, Jeff Dean, and others. // // Utility class for writing text to a ZeroCopyOutputStream. #ifndef GOOGLE_PROTOBUF_IO_PRINTER_H__ #define GOOGLE_PROTOBUF_IO_PRINTER_H__ #include <map> #include <string> #include <vector> #include <google/protobuf/stubs/common.h> // Must be included last. #include <google/protobuf/port_def.inc> namespace google { namespace protobuf { namespace io { class ZeroCopyOutputStream; // zero_copy_stream.h // Records annotations about a Printer's output. class PROTOBUF_EXPORT AnnotationCollector { … }; // 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. template <typename AnnotationProto> class AnnotationProtoCollector : public AnnotationCollector { … }; // 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 in debug builds. This helps greatly in // debugging code which uses it. // // If a Printer is constructed with an AnnotationCollector, it will provide it // with annotations that connect the Printer's output to paths that can identify // various descriptors. In the above example, if person_ is a descriptor that // identifies Bob, we can associate the output string "My name is Bob." with // a source path pointing to that descriptor with: // // printer.Annotate("name", person_); // // The AnnotationCollector will be sent an annotation linking the output range // covering "Bob" to the logical path provided by person_. Tools may use // this association to (for example) link "Bob" in the output back to the // source file that defined the person_ descriptor identifying Bob. // // Annotate can only examine variables substituted during the last call to // Print. It is invalid to refer to a variable that was used multiple times // in a single Print call. // // In full generality, one may specify a range of output text using a beginning // substitution variable and an ending variable. The resulting annotation will // span from the first character of the substituted value for the beginning // variable to the last character of the substituted value for the ending // variable. For example, the Annotate call above is equivalent to this one: // // printer.Annotate("name", "name", person_); // // This is useful if multiple variables combine to form a single span of output // that should be annotated with the same source path. For example: // // Printer printer(output, '$'); // map<string, string> vars; // vars["first"] = "Alice"; // vars["last"] = "Smith"; // printer.Print(vars, "My name is $first$ $last$."); // printer.Annotate("first", "last", person_); // // This code would associate the span covering "Alice Smith" in the output with // the person_ descriptor. // // Note that the beginning variable must come before (or overlap with, in the // case of zero-sized substitution values) the ending variable. // // It is also sometimes useful to use variables with zero-sized values as // markers. This avoids issues with multiple references to the same variable // and also allows annotation ranges to span literal text from the Print // templates: // // Printer printer(output, '$'); // map<string, string> vars; // vars["foo"] = "bar"; // vars["function"] = "call"; // vars["mark"] = ""; // printer.Print(vars, "$function$($foo$,$foo$)$mark$"); // printer.Annotate("function", "mark", call_); // // This code associates the span covering "call(bar,bar)" in the output with the // call_ descriptor. class PROTOBUF_EXPORT Printer { … }; } // namespace io } // namespace protobuf } // namespace google #include <google/protobuf/port_undef.inc> #endif // GOOGLE_PROTOBUF_IO_PRINTER_H__