aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/google/protobuf/text_format.h
diff options
context:
space:
mode:
authorGravatar Jisi Liu <jisi.liu@gmail.com>2017-07-18 15:38:30 -0700
committerGravatar Jisi Liu <jisi.liu@gmail.com>2017-07-18 15:38:30 -0700
commit09354db1434859a31a3c81abebcc4018d42f2715 (patch)
treeb87c7cdc2255e6c8062ab92b4082665cd698d753 /src/google/protobuf/text_format.h
parent9053033a5076f82cf18b823c31f352e95e5bfd8d (diff)
Merge from Google internal for 3.4 release
Diffstat (limited to 'src/google/protobuf/text_format.h')
-rw-r--r--src/google/protobuf/text_format.h108
1 files changed, 77 insertions, 31 deletions
diff --git a/src/google/protobuf/text_format.h b/src/google/protobuf/text_format.h
index 560fd391..cd0e307e 100644
--- a/src/google/protobuf/text_format.h
+++ b/src/google/protobuf/text_format.h
@@ -65,21 +65,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 prior to printing, and will be left empty
- // even if printing fails.
+ // 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,11 +93,57 @@ 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 BaseTextGenerator {
+ public:
+ virtual ~BaseTextGenerator();
+ // 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,
+ 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);
+ };
+
+// DEPRECATED: Please use FastFieldValuePrinter.
class LIBPROTOBUF_EXPORT FieldValuePrinter {
public:
FieldValuePrinter();
@@ -124,6 +171,7 @@ class LIBPROTOBUF_EXPORT TextFormat {
bool single_line_mode) const;
private:
+ FastFieldValuePrinter delegate_;
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldValuePrinter);
};
@@ -183,12 +231,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.
@@ -233,13 +282,15 @@ 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);
private:
// Forward declaration of an internal class used to print the text
@@ -248,43 +299,38 @@ 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, 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,9 +348,9 @@ 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;
+ google::protobuf::scoped_ptr<const FastFieldValuePrinter> default_field_value_printer_;
+ typedef std::map<const FieldDescriptor*, const FastFieldValuePrinter*>
+ CustomPrinterMap;
CustomPrinterMap custom_printers_;
};