Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 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 | |
| 31 | #ifndef GOOGLE_PROTOBUF_IMPLICIT_WEAK_MESSAGE_H__ |
| 32 | #define GOOGLE_PROTOBUF_IMPLICIT_WEAK_MESSAGE_H__ |
| 33 | |
| 34 | #include <google/protobuf/io/coded_stream.h> |
| 35 | #include <google/protobuf/arena.h> |
| 36 | #include <google/protobuf/message_lite.h> |
| 37 | |
| 38 | // This file is logically internal-only and should only be used by protobuf |
| 39 | // generated code. |
| 40 | |
| 41 | namespace google { |
| 42 | namespace protobuf { |
| 43 | namespace internal { |
| 44 | |
| 45 | // An implementation of MessageLite that treats all data as unknown. This type |
| 46 | // acts as a placeholder for an implicit weak field in the case where the true |
| 47 | // message type does not get linked into the binary. |
| 48 | class LIBPROTOBUF_EXPORT ImplicitWeakMessage : public MessageLite { |
| 49 | public: |
| 50 | ImplicitWeakMessage() : arena_(NULL) {} |
| 51 | explicit ImplicitWeakMessage(Arena* arena) : arena_(arena) {} |
| 52 | |
| 53 | static const ImplicitWeakMessage* default_instance(); |
| 54 | |
| 55 | string GetTypeName() const { return ""; } |
| 56 | |
| 57 | MessageLite* New() const { return new ImplicitWeakMessage; } |
| 58 | MessageLite* New(Arena* arena) const { |
| 59 | return Arena::CreateMessage<ImplicitWeakMessage>(arena); |
| 60 | } |
| 61 | |
| 62 | Arena* GetArena() const { return arena_; } |
| 63 | |
| 64 | void Clear() { data_.clear(); } |
| 65 | |
| 66 | bool IsInitialized() const { return true; } |
| 67 | |
| 68 | void CheckTypeAndMergeFrom(const MessageLite& other) { |
| 69 | data_.append(static_cast<const ImplicitWeakMessage&>(other).data_); |
| 70 | } |
| 71 | |
| 72 | bool MergePartialFromCodedStream(io::CodedInputStream* input); |
| 73 | |
| 74 | size_t ByteSizeLong() const { return data_.size(); } |
| 75 | |
| 76 | void SerializeWithCachedSizes(io::CodedOutputStream* output) const { |
| 77 | output->WriteString(data_); |
| 78 | } |
| 79 | |
| 80 | int GetCachedSize() const { return static_cast<int>(data_.size()); } |
| 81 | |
| 82 | typedef void InternalArenaConstructable_; |
| 83 | |
| 84 | private: |
| 85 | Arena* const arena_; |
| 86 | string data_; |
| 87 | GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ImplicitWeakMessage); |
| 88 | }; |
| 89 | |
| 90 | // A type handler for use with implicit weak repeated message fields. |
| 91 | template <typename ImplicitWeakType> |
| 92 | class ImplicitWeakTypeHandler { |
| 93 | public: |
| 94 | typedef ImplicitWeakType Type; |
| 95 | typedef ::google::protobuf::MessageLite WeakType; |
| 96 | static const bool Moveable = false; |
| 97 | |
| 98 | // With implicit weak fields, we need separate NewFromPrototype and |
| 99 | // NewFromPrototypeWeak functions. The former is used when we want to create a |
| 100 | // strong dependency on the message type, and it just delegates to the |
| 101 | // GenericTypeHandler. The latter avoids creating a strong dependency, by |
| 102 | // simply calling MessageLite::New. |
| 103 | static inline ::google::protobuf::MessageLite* NewFromPrototype( |
| 104 | const ::google::protobuf::MessageLite* prototype, ::google::protobuf::Arena* arena = NULL) { |
| 105 | return prototype->New(arena); |
| 106 | } |
| 107 | |
| 108 | static inline void Delete(::google::protobuf::MessageLite* value, Arena* arena) { |
| 109 | if (arena == NULL) { |
| 110 | delete value; |
| 111 | } |
| 112 | } |
| 113 | static inline ::google::protobuf::Arena* GetArena(::google::protobuf::MessageLite* value) { |
| 114 | return value->GetArena(); |
| 115 | } |
| 116 | static inline void* GetMaybeArenaPointer(::google::protobuf::MessageLite* value) { |
| 117 | return value->GetArena(); |
| 118 | } |
| 119 | static inline void Clear(::google::protobuf::MessageLite* value) { |
| 120 | value->Clear(); |
| 121 | } |
| 122 | static void Merge(const ::google::protobuf::MessageLite& from, |
| 123 | ::google::protobuf::MessageLite* to) { |
| 124 | to->CheckTypeAndMergeFrom(from); |
| 125 | } |
| 126 | static inline size_t SpaceUsedLong(const Type& value) { |
| 127 | return value.SpaceUsedLong(); |
| 128 | } |
| 129 | }; |
| 130 | |
| 131 | } // namespace internal |
| 132 | } // namespace protobuf |
| 133 | |
| 134 | } // namespace google |
| 135 | #endif // GOOGLE_PROTOBUF_IMPLICIT_WEAK_MESSAGE_H__ |