当前位置:   article > 正文

protobuf 之 message 接口摘录_protobuf message

protobuf message
  1. // Abstract interface for protocol messages.
  2. //
  3. // See also MessageLite, which contains most every-day operations. Message
  4. // adds descriptors and reflection on top of that.
  5. //
  6. // The methods of this class that are virtual but not pure-virtual have
  7. // default implementations based on reflection. Message classes which are
  8. // optimized for speed will want to override these with faster implementations,
  9. // but classes optimized for code size may be happy with keeping them. See
  10. // the optimize_for option in descriptor.proto.
  11. class LIBPROTOBUF_EXPORT Message : public MessageLite {
  12. public:
  13. inline Message() {}
  14. virtual ~Message();
  15. // Basic Operations ------------------------------------------------
  16. // Construct a new instance of the same type. Ownership is passed to the
  17. // caller. (This is also defined in MessageLite, but is defined again here
  18. // for return-type covariance.)
  19. virtual Message* New() const = 0;
  20. // Make this message into a copy of the given message. The given message
  21. // must have the same descriptor, but need not necessarily be the same class.
  22. // By default this is just implemented as "Clear(); MergeFrom(from);".
  23. virtual void CopyFrom(const Message& from);
  24. // Merge the fields from the given message into this message. Singular
  25. // fields will be overwritten, except for embedded messages which will
  26. // be merged. Repeated fields will be concatenated. The given message
  27. // must be of the same type as this message (i.e. the exact same class).
  28. virtual void MergeFrom(const Message& from);
  29. // Verifies that IsInitialized() returns true. GOOGLE_CHECK-fails otherwise, with
  30. // a nice error message.
  31. void CheckInitialized() const;
  32. // Slowly build a list of all required fields that are not set.
  33. // This is much, much slower than IsInitialized() as it is implemented
  34. // purely via reflection. Generally, you should not call this unless you
  35. // have already determined that an error exists by calling IsInitialized().
  36. void FindInitializationErrors(vector<string>* errors) const;
  37. // Like FindInitializationErrors, but joins all the strings, delimited by
  38. // commas, and returns them.
  39. string InitializationErrorString() const;
  40. // Clears all unknown fields from this message and all embedded messages.
  41. // Normally, if unknown tag numbers are encountered when parsing a message,
  42. // the tag and value are stored in the message's UnknownFieldSet and
  43. // then written back out when the message is serialized. This allows servers
  44. // which simply route messages to other servers to pass through messages
  45. // that have new field definitions which they don't yet know about. However,
  46. // this behavior can have security implications. To avoid it, call this
  47. // method after parsing.
  48. //
  49. // See Reflection::GetUnknownFields() for more on unknown fields.
  50. virtual void DiscardUnknownFields();
  51. // Computes (an estimate of) the total number of bytes currently used for
  52. // storing the message in memory. The default implementation calls the
  53. // Reflection object's SpaceUsed() method.
  54. virtual int SpaceUsed() const;
  55. // Debugging & Testing----------------------------------------------
  56. // Generates a human readable form of this message, useful for debugging
  57. // and other purposes.
  58. string DebugString() const;
  59. // Like DebugString(), but with less whitespace.
  60. string ShortDebugString() const;
  61. // Like DebugString(), but do not escape UTF-8 byte sequences.
  62. string Utf8DebugString() const;
  63. // Convenience function useful in GDB. Prints DebugString() to stdout.
  64. void PrintDebugString() const;
  65. // Heavy I/O -------------------------------------------------------
  66. // Additional parsing and serialization methods not implemented by
  67. // MessageLite because they are not supported by the lite library.
  68. // Parse a protocol buffer from a file descriptor. If successful, the entire
  69. // input will be consumed.
  70. bool ParseFromFileDescriptor(int file_descriptor);
  71. // Like ParseFromFileDescriptor(), but accepts messages that are missing
  72. // required fields.
  73. bool ParsePartialFromFileDescriptor(int file_descriptor);
  74. // Parse a protocol buffer from a C++ istream. If successful, the entire
  75. // input will be consumed.
  76. bool ParseFromIstream(istream* input);
  77. // Like ParseFromIstream(), but accepts messages that are missing
  78. // required fields.
  79. bool ParsePartialFromIstream(istream* input);
  80. // Serialize the message and write it to the given file descriptor. All
  81. // required fields must be set.
  82. bool SerializeToFileDescriptor(int file_descriptor) const;
  83. // Like SerializeToFileDescriptor(), but allows missing required fields.
  84. bool SerializePartialToFileDescriptor(int file_descriptor) const;
  85. // Serialize the message and write it to the given C++ ostream. All
  86. // required fields must be set.
  87. bool SerializeToOstream(ostream* output) const;
  88. // Like SerializeToOstream(), but allows missing required fields.
  89. bool SerializePartialToOstream(ostream* output) const;
  90. // Reflection-based methods ----------------------------------------
  91. // These methods are pure-virtual in MessageLite, but Message provides
  92. // reflection-based default implementations.
  93. virtual string GetTypeName() const;
  94. virtual void Clear();
  95. virtual bool IsInitialized() const;
  96. virtual void CheckTypeAndMergeFrom(const MessageLite& other);
  97. virtual bool MergePartialFromCodedStream(io::CodedInputStream* input);
  98. virtual int ByteSize() const;
  99. virtual void SerializeWithCachedSizes(io::CodedOutputStream* output) const;
  100. private:
  101. // This is called only by the default implementation of ByteSize(), to
  102. // update the cached size. If you override ByteSize(), you do not need
  103. // to override this. If you do not override ByteSize(), you MUST override
  104. // this; the default implementation will crash.
  105. //
  106. // The method is private because subclasses should never call it; only
  107. // override it. Yes, C++ lets you do that. Crazy, huh?
  108. virtual void SetCachedSize(int size) const;
  109. public:
  110. // Introspection ---------------------------------------------------
  111. // Typedef for backwards-compatibility.
  112. typedef google::protobuf::Reflection Reflection;
  113. // Get a Descriptor for this message's type. This describes what
  114. // fields the message contains, the types of those fields, etc.
  115. const Descriptor* GetDescriptor() const { return GetMetadata().descriptor; }
  116. // Get the Reflection interface for this Message, which can be used to
  117. // read and modify the fields of the Message dynamically (in other words,
  118. // without knowing the message type at compile time). This object remains
  119. // property of the Message.
  120. //
  121. // This method remains virtual in case a subclass does not implement
  122. // reflection and wants to override the default behavior.
  123. virtual const Reflection* GetReflection() const {
  124. return GetMetadata().reflection;
  125. }
  126. protected:
  127. // Get a struct containing the metadata for the Message. Most subclasses only
  128. // need to implement this method, rather than the GetDescriptor() and
  129. // GetReflection() wrappers.
  130. virtual Metadata GetMetadata() const = 0;
  131. private:
  132. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Message);
  133. };

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/430300
推荐阅读
相关标签
  

闽ICP备14008679号