aboutsummaryrefslogtreecommitdiff
path: root/src/google/protobuf/text_format.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/google/protobuf/text_format.h')
-rw-r--r--src/google/protobuf/text_format.h203
1 files changed, 148 insertions, 55 deletions
diff --git a/src/google/protobuf/text_format.h b/src/google/protobuf/text_format.h
index 2873d339..d6f3e16b 100644
--- a/src/google/protobuf/text_format.h
+++ b/src/google/protobuf/text_format.h
@@ -40,15 +40,13 @@
#include <map>
#include <memory>
-#ifndef _SHARED_PTR_H
-#include <google/protobuf/stubs/shared_ptr.h>
-#endif
#include <string>
#include <vector>
#include <google/protobuf/stubs/common.h>
#include <google/protobuf/descriptor.h>
#include <google/protobuf/message.h>
+#include <google/protobuf/message_lite.h>
namespace google {
namespace protobuf {
@@ -65,21 +63,22 @@ namespace io {
class LIBPROTOBUF_EXPORT TextFormat {
public:
// Outputs a textual representation of the given message to the given
- // output stream.
+ // output stream. Returns false if printing fails.
static bool Print(const Message& message, io::ZeroCopyOutputStream* output);
// Print the fields in an UnknownFieldSet. They are printed by tag number
// only. Embedded messages are heuristically identified by attempting to
- // parse them.
+ // parse them. Returns false if printing fails.
static bool PrintUnknownFields(const UnknownFieldSet& unknown_fields,
io::ZeroCopyOutputStream* output);
// Like Print(), but outputs directly to a string.
- // Note: output will be cleared before prior to printing, and will
- // be left empty even if printing fails.
+ // Note: output will be cleared prior to printing, and will be left empty
+ // even if printing fails. Returns false if printing fails.
static bool PrintToString(const Message& message, string* output);
- // Like PrintUnknownFields(), but outputs directly to a string.
+ // Like PrintUnknownFields(), but outputs directly to a string. Returns false
+ // if printing fails.
static bool PrintUnknownFieldsToString(const UnknownFieldSet& unknown_fields,
string* output);
@@ -92,12 +91,66 @@ class LIBPROTOBUF_EXPORT TextFormat {
int index,
string* output);
- // The default printer that converts scalar values from fields into
- // their string representation.
- // You can derive from this FieldValuePrinter if you want to have
- // fields to be printed in a different way and register it at the
- // Printer.
- class LIBPROTOBUF_EXPORT FieldValuePrinter {
+ 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;
+
+ void PrintString(const string& str) { Print(str.data(), str.size()); }
+
+ template <size_t n>
+ void PrintLiteral(const char (&text)[n]) {
+ Print(text, n - 1); // n includes the terminating zero character.
+ }
+ };
+
+ // The default printer that converts scalar values from fields into their
+ // string representation.
+ // You can derive from this FastFieldValuePrinter if you want to have fields
+ // to be printed in a different way and register it at the Printer.
+ class LIBPROTOBUF_EXPORT FastFieldValuePrinter {
+ public:
+ FastFieldValuePrinter();
+ virtual ~FastFieldValuePrinter();
+ virtual void PrintBool(bool val, BaseTextGenerator* generator) const;
+ virtual void PrintInt32(int32 val, BaseTextGenerator* generator) const;
+ virtual void PrintUInt32(uint32 val, BaseTextGenerator* generator) const;
+ virtual void PrintInt64(int64 val, BaseTextGenerator* generator) const;
+ virtual void PrintUInt64(uint64 val, BaseTextGenerator* generator) const;
+ virtual void PrintFloat(float val, BaseTextGenerator* generator) const;
+ virtual void PrintDouble(double val, BaseTextGenerator* generator) const;
+ virtual void PrintString(const string& val,
+ BaseTextGenerator* generator) const;
+ virtual void PrintBytes(const string& val,
+ 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,
+ BaseTextGenerator* generator) const;
+ virtual void PrintMessageStart(const Message& message, int field_index,
+ int field_count, bool single_line_mode,
+ BaseTextGenerator* generator) const;
+ virtual void PrintMessageEnd(const Message& message, int field_index,
+ int field_count, bool single_line_mode,
+ BaseTextGenerator* generator) const;
+
+ private:
+ GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FastFieldValuePrinter);
+ };
+
+ class LIBPROTOBUF_EXPORT PROTOBUF_RUNTIME_DEPRECATED("Please use FastFieldValuePrinter")
+ FieldValuePrinter {
public:
FieldValuePrinter();
virtual ~FieldValuePrinter();
@@ -124,9 +177,44 @@ class LIBPROTOBUF_EXPORT TextFormat {
bool single_line_mode) const;
private:
+ FastFieldValuePrinter delegate_;
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 {
@@ -162,7 +250,7 @@ class LIBPROTOBUF_EXPORT TextFormat {
single_line_mode_ = single_line_mode;
}
- bool IsInSingleLineMode() {
+ bool IsInSingleLineMode() const {
return single_line_mode_;
}
@@ -183,12 +271,13 @@ class LIBPROTOBUF_EXPORT TextFormat {
// Set true to output UTF-8 instead of ASCII. The only difference
// is that bytes >= 0x80 in string fields will not be escaped,
// because they are assumed to be part of UTF-8 multi-byte
- // sequences. This will change the default FieldValuePrinter.
+ // sequences. This will change the default FastFieldValuePrinter.
void SetUseUtf8StringEscaping(bool as_utf8);
- // Set the default FieldValuePrinter that is used for all fields that
+ // Set the default (Fast)FieldValuePrinter that is used for all fields that
// don't have a field-specific printer registered.
// Takes ownership of the printer.
+ void SetDefaultFieldValuePrinter(const FastFieldValuePrinter* printer);
void SetDefaultFieldValuePrinter(const FieldValuePrinter* printer);
// Sets whether we want to hide unknown fields or not.
@@ -201,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_ =
@@ -221,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.
@@ -233,13 +329,22 @@ class LIBPROTOBUF_EXPORT TextFormat {
truncate_string_field_longer_than_ = truncate_string_field_longer_than;
}
- // Register a custom field-specific FieldValuePrinter for fields
+ // Register a custom field-specific (Fast)FieldValuePrinter for fields
// with a particular FieldDescriptor.
// Returns "true" if the registration succeeded, or "false", if there is
// already a printer for that FieldDescriptor.
// Takes ownership of the printer on successful registration.
bool RegisterFieldValuePrinter(const FieldDescriptor* field,
const FieldValuePrinter* printer);
+ 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
@@ -248,43 +353,39 @@ class LIBPROTOBUF_EXPORT TextFormat {
// Internal Print method, used for writing to the OutputStream via
// the TextGenerator class.
- void Print(const Message& message,
- TextGenerator& generator) const;
+ void Print(const Message& message, TextGenerator* generator) const;
// Print a single field.
- void PrintField(const Message& message,
- const Reflection* reflection,
+ void PrintField(const Message& message, const Reflection* reflection,
const FieldDescriptor* field,
- TextGenerator& generator) const;
+ TextGenerator* generator) const;
// Print a repeated primitive field in short form.
void PrintShortRepeatedField(const Message& message,
const Reflection* reflection,
const FieldDescriptor* field,
- TextGenerator& generator) const;
+ TextGenerator* generator) const;
// 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;
+ TextGenerator* generator) const;
// Outputs a textual representation of the value of the field supplied on
// the message supplied or the default value if not set.
- void PrintFieldValue(const Message& message,
- const Reflection* reflection,
- const FieldDescriptor* field,
- int index,
- TextGenerator& generator) const;
+ void PrintFieldValue(const Message& message, const Reflection* reflection,
+ const FieldDescriptor* field, int index,
+ TextGenerator* generator) const;
// Print the fields in an UnknownFieldSet. They are printed by tag number
// only. Embedded messages are heuristically identified by attempting to
// parse them.
void PrintUnknownFields(const UnknownFieldSet& unknown_fields,
- TextGenerator& generator) const;
+ TextGenerator* generator) const;
- bool PrintAny(const Message& message, TextGenerator& generator) const;
+ bool PrintAny(const Message& message, TextGenerator* generator) const;
int initial_indent_level_;
@@ -302,10 +403,16 @@ class LIBPROTOBUF_EXPORT TextFormat {
int64 truncate_string_field_longer_than_;
- google::protobuf::scoped_ptr<const FieldValuePrinter> default_field_value_printer_;
- typedef std::map<const FieldDescriptor*,
- const FieldValuePrinter*> CustomPrinterMap;
+ std::unique_ptr<const FastFieldValuePrinter> default_field_value_printer_;
+ typedef std::map<const FieldDescriptor*, const FastFieldValuePrinter*>
+ CustomPrinterMap;
CustomPrinterMap custom_printers_;
+
+ typedef std::map<const Descriptor*, const MessagePrinter*>
+ CustomMessagePrinterMap;
+ CustomMessagePrinterMap custom_message_printers_;
+
+ const Finder* finder_;
};
// Parses a text-format protocol message from the given input stream to
@@ -340,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;
@@ -477,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_;