implicit_weak_message.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #ifndef GOOGLE_PROTOBUF_IMPLICIT_WEAK_MESSAGE_H__
  31. #define GOOGLE_PROTOBUF_IMPLICIT_WEAK_MESSAGE_H__
  32. #include <google/protobuf/io/coded_stream.h>
  33. #include <google/protobuf/arena.h>
  34. #include <google/protobuf/message_lite.h>
  35. // This file is logically internal-only and should only be used by protobuf
  36. // generated code.
  37. namespace google {
  38. namespace protobuf {
  39. namespace internal {
  40. // An implementation of MessageLite that treats all data as unknown. This type
  41. // acts as a placeholder for an implicit weak field in the case where the true
  42. // message type does not get linked into the binary.
  43. class LIBPROTOBUF_EXPORT ImplicitWeakMessage : public MessageLite {
  44. public:
  45. ImplicitWeakMessage() : arena_(NULL) {}
  46. explicit ImplicitWeakMessage(Arena* arena) : arena_(arena) {}
  47. static const ImplicitWeakMessage* default_instance();
  48. string GetTypeName() const { return ""; }
  49. MessageLite* New() const { return new ImplicitWeakMessage; }
  50. MessageLite* New(Arena* arena) const {
  51. return Arena::CreateMessage<ImplicitWeakMessage>(arena);
  52. }
  53. Arena* GetArena() const { return arena_; }
  54. void Clear() { data_.clear(); }
  55. bool IsInitialized() const { return true; }
  56. void CheckTypeAndMergeFrom(const MessageLite& other) {
  57. data_.append(static_cast<const ImplicitWeakMessage&>(other).data_);
  58. }
  59. bool MergePartialFromCodedStream(io::CodedInputStream* input);
  60. size_t ByteSizeLong() const { return data_.size(); }
  61. void SerializeWithCachedSizes(io::CodedOutputStream* output) const {
  62. output->WriteString(data_);
  63. }
  64. int GetCachedSize() const { return static_cast<int>(data_.size()); }
  65. typedef void InternalArenaConstructable_;
  66. private:
  67. Arena* const arena_;
  68. string data_;
  69. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ImplicitWeakMessage);
  70. };
  71. // A type handler for use with implicit weak repeated message fields.
  72. template <typename ImplicitWeakType>
  73. class ImplicitWeakTypeHandler {
  74. public:
  75. typedef ImplicitWeakType Type;
  76. typedef ::google::protobuf::MessageLite WeakType;
  77. static const bool Moveable = false;
  78. // With implicit weak fields, we need separate NewFromPrototype and
  79. // NewFromPrototypeWeak functions. The former is used when we want to create a
  80. // strong dependency on the message type, and it just delegates to the
  81. // GenericTypeHandler. The latter avoids creating a strong dependency, by
  82. // simply calling MessageLite::New.
  83. static inline ::google::protobuf::MessageLite* NewFromPrototype(
  84. const ::google::protobuf::MessageLite* prototype, ::google::protobuf::Arena* arena = NULL) {
  85. return prototype->New(arena);
  86. }
  87. static inline void Delete(::google::protobuf::MessageLite* value, Arena* arena) {
  88. if (arena == NULL) {
  89. delete value;
  90. }
  91. }
  92. static inline ::google::protobuf::Arena* GetArena(::google::protobuf::MessageLite* value) {
  93. return value->GetArena();
  94. }
  95. static inline void* GetMaybeArenaPointer(::google::protobuf::MessageLite* value) {
  96. return value->GetArena();
  97. }
  98. static inline void Clear(::google::protobuf::MessageLite* value) {
  99. value->Clear();
  100. }
  101. static void Merge(const ::google::protobuf::MessageLite& from,
  102. ::google::protobuf::MessageLite* to) {
  103. to->CheckTypeAndMergeFrom(from);
  104. }
  105. static inline size_t SpaceUsedLong(const Type& value) {
  106. return value.SpaceUsedLong();
  107. }
  108. };
  109. } // namespace internal
  110. } // namespace protobuf
  111. } // namespace google
  112. #endif // GOOGLE_PROTOBUF_IMPLICIT_WEAK_MESSAGE_H__