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