From 0400cca3236de1ca303af38bf81eab332d042b7c Mon Sep 17 00:00:00 2001 From: Adam Cozzette Date: Tue, 13 Mar 2018 16:37:29 -0700 Subject: Integrated internal changes from Google --- src/google/protobuf/text_format.h | 92 +++++++++++++++++++++++++++++---------- 1 file changed, 69 insertions(+), 23 deletions(-) (limited to 'src/google/protobuf/text_format.h') diff --git a/src/google/protobuf/text_format.h b/src/google/protobuf/text_format.h index a2670d6b..d6f3e16b 100644 --- a/src/google/protobuf/text_format.h +++ b/src/google/protobuf/text_format.h @@ -40,9 +40,6 @@ #include #include -#ifndef _SHARED_PTR_H -#include -#endif #include #include @@ -97,6 +94,10 @@ class LIBPROTOBUF_EXPORT TextFormat { class LIBPROTOBUF_EXPORT BaseTextGenerator { public: virtual ~BaseTextGenerator(); + + virtual void Indent() {} + virtual void Outdent() {} + // Print text to the output stream. virtual void Print(const char* text, size_t size) = 0; @@ -129,6 +130,10 @@ class LIBPROTOBUF_EXPORT TextFormat { BaseTextGenerator* generator) const; virtual void PrintEnum(int32 val, const string& name, BaseTextGenerator* generator) const; + virtual void PrintFieldName(const Message& message, int field_index, + int field_count, const Reflection* reflection, + const FieldDescriptor* field, + BaseTextGenerator* generator) const; virtual void PrintFieldName(const Message& message, const Reflection* reflection, const FieldDescriptor* field, @@ -176,6 +181,40 @@ class LIBPROTOBUF_EXPORT TextFormat { GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldValuePrinter); }; + class LIBPROTOBUF_EXPORT MessagePrinter { + public: + MessagePrinter() {} + virtual ~MessagePrinter() {} + virtual void Print(const Message& message, bool single_line_mode, + BaseTextGenerator* generator) const = 0; + + private: + GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MessagePrinter); + }; + + // Interface that Printers or Parsers can use to find extensions, or types + // referenced in Any messages. + class LIBPROTOBUF_EXPORT Finder { + public: + virtual ~Finder(); + + // Try to find an extension of *message by fully-qualified field + // name. Returns NULL if no extension is known for this name or number. + // The base implementation uses the extensions already known by the message. + virtual const FieldDescriptor* FindExtension( + Message* message, + const string& name) const; + + // Find the message type for an Any proto. + // Returns NULL if no message is known for this name. + // The base implementation only accepts prefixes of type.googleprod.com/ or + // type.googleapis.com/, and searches the DescriptorPool of the parent + // message. + virtual const Descriptor* FindAnyType(const Message& message, + const string& prefix, + const string& name) const; + }; + // Class for those users which require more fine-grained control over how // a protobuffer message is printed out. class LIBPROTOBUF_EXPORT Printer { @@ -251,9 +290,11 @@ class LIBPROTOBUF_EXPORT TextFormat { hide_unknown_fields_ = hide; } - // If print_message_fields_in_index_order is true, print fields of a proto - // message using the order defined in source code instead of the field - // number. By default, use the field number order. + // If print_message_fields_in_index_order is true, fields of a proto message + // will be printed using the order defined in source code instead of the + // field number, extensions will be printed at the end of the message + // and their relative order is determined by the extension number. + // By default, use the field number order. void SetPrintMessageFieldsInIndexOrder( bool print_message_fields_in_index_order) { print_message_fields_in_index_order_ = @@ -271,6 +312,11 @@ class LIBPROTOBUF_EXPORT TextFormat { expand_any_ = expand; } + // Set how parser finds message for Any payloads. + void SetFinder(Finder* finder) { + finder_ = finder; + } + // If non-zero, we truncate all string fields that are longer than this // threshold. This is useful when the proto message has very long strings, // e.g., dump of encoded image file. @@ -293,6 +339,13 @@ class LIBPROTOBUF_EXPORT TextFormat { bool RegisterFieldValuePrinter(const FieldDescriptor* field, const FastFieldValuePrinter* printer); + // Register a custom message-specific MessagePrinter for messages with a + // particular Descriptor. + // Returns "true" if the registration succeeded, or "false" if there is + // already a printer for that Descriptor. + bool RegisterMessagePrinter(const Descriptor* descriptor, + const MessagePrinter* printer); + private: // Forward declaration of an internal class used to print the text // output to the OutputStream (see text_format.cc for implementation). @@ -315,7 +368,8 @@ class LIBPROTOBUF_EXPORT TextFormat { // Print the name of a field -- i.e. everything that comes before the // ':' for a single name/value pair. - void PrintFieldName(const Message& message, const Reflection* reflection, + void PrintFieldName(const Message& message, int field_index, + int field_count, const Reflection* reflection, const FieldDescriptor* field, TextGenerator* generator) const; @@ -349,10 +403,16 @@ class LIBPROTOBUF_EXPORT TextFormat { int64 truncate_string_field_longer_than_; - google::protobuf::scoped_ptr default_field_value_printer_; + std::unique_ptr default_field_value_printer_; typedef std::map CustomPrinterMap; CustomPrinterMap custom_printers_; + + typedef std::map + CustomMessagePrinterMap; + CustomMessagePrinterMap custom_message_printers_; + + const Finder* finder_; }; // Parses a text-format protocol message from the given input stream to @@ -387,20 +447,6 @@ class LIBPROTOBUF_EXPORT TextFormat { const FieldDescriptor* field, Message* message); - // Interface that TextFormat::Parser can use to find extensions. - // This class may be extended in the future to find more information - // like fields, etc. - class LIBPROTOBUF_EXPORT Finder { - public: - virtual ~Finder(); - - // Try to find an extension of *message by fully-qualified field - // name. Returns NULL if no extension is known for this name or number. - virtual const FieldDescriptor* FindExtension( - Message* message, - const string& name) const = 0; - }; - // A location in the parsed text. struct ParseLocation { int line; @@ -524,7 +570,7 @@ class LIBPROTOBUF_EXPORT TextFormat { ParserImpl* parser_impl); io::ErrorCollector* error_collector_; - Finder* finder_; + const Finder* finder_; ParseInfoTree* parse_info_tree_; bool allow_partial_; bool allow_case_insensitive_field_; -- cgit v1.2.3