| 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_MAP_FIELD_LITE_H__ |
| 32 |
#define GOOGLE_PROTOBUF_MAP_FIELD_LITE_H__ |
| 33 |
|
| 34 |
#include <type_traits> |
| 35 |
#include <google/protobuf/parse_context.h> |
| 36 |
#include <google/protobuf/io/coded_stream.h> |
| 37 |
#include <google/protobuf/map.h> |
| 38 |
#include <google/protobuf/map_entry_lite.h> |
| 39 |
#include <google/protobuf/port.h> |
| 40 |
#include <google/protobuf/wire_format_lite.h> |
| 41 |
|
| 42 |
#include <google/protobuf/port_def.inc> |
| 43 |
|
| 44 |
#ifdef SWIG |
| 45 |
#error "You cannot SWIG proto headers" |
| 46 |
#endif |
| 47 |
|
| 48 |
namespace google { |
| 49 |
namespace protobuf { |
| 50 |
namespace internal { |
| 51 |
|
| 52 |
// This class provides access to map field using generated api. It is used for |
| 53 |
// internal generated message implementation only. Users should never use this |
| 54 |
// directly. |
| 55 |
template <typename Derived, typename Key, typename T, |
| 56 |
WireFormatLite::FieldType key_wire_type, |
| 57 |
WireFormatLite::FieldType value_wire_type> |
| 58 |
class MapFieldLite { |
| 59 |
// Define message type for internal repeated field. |
| 60 |
typedef Derived EntryType; |
| 61 |
|
| 62 |
public: |
| 63 |
typedef Map<Key, T> MapType; |
| 64 |
typedef EntryType EntryTypeTrait; |
| 65 |
|
| 66 |
constexpr MapFieldLite() {} |
| 67 |
|
| 68 |
explicit MapFieldLite(Arena* arena) : map_(arena) {} |
| 69 |
|
| 70 |
// Accessors |
| 71 |
const Map<Key, T>& GetMap() const { return map_; } |
| 72 |
Map<Key, T>* MutableMap() { return &map_; } |
| 73 |
|
| 74 |
// Convenient methods for generated message implementation. |
| 75 |
int size() const { return static_cast<int>(map_.size()); } |
| 76 |
void Clear() { return map_.clear(); } |
| 77 |
void MergeFrom(const MapFieldLite& other) { |
| 78 |
for (typename Map<Key, T>::const_iterator it = other.map_.begin(); |
| 79 |
it != other.map_.end(); ++it) { |
| 80 |
map_[it->first] = it->second; |
| 81 |
} |
| 82 |
} |
| 83 |
void Swap(MapFieldLite* other) { map_.swap(other->map_); } |
| 84 |
|
| 85 |
// Used in the implementation of parsing. Caller should take the ownership iff |
| 86 |
// arena_ is NULL. |
| 87 |
EntryType* NewEntry() const { |
| 88 |
return Arena::CreateMessage<EntryType>(map_.arena()); |
| 89 |
} |
| 90 |
// Used in the implementation of serializing enum value type. Caller should |
| 91 |
// take the ownership iff arena_ is NULL. |
| 92 |
EntryType* NewEnumEntryWrapper(const Key& key, const T t) const { |
| 93 |
return EntryType::EnumWrap(key, t, map_.arena_); |
| 94 |
} |
| 95 |
// Used in the implementation of serializing other value types. Caller should |
| 96 |
// take the ownership iff arena_ is NULL. |
| 97 |
EntryType* NewEntryWrapper(const Key& key, const T& t) const { |
| 98 |
return EntryType::Wrap(key, t, map_.arena_); |
| 99 |
} |
| 100 |
|
| 101 |
const char* _InternalParse(const char* ptr, ParseContext* ctx) { |
| 102 |
typename Derived::template Parser<MapFieldLite, Map<Key, T>> parser(this); |
| 103 |
return parser._InternalParse(ptr, ctx); |
| 104 |
} |
| 105 |
|
| 106 |
template <typename UnknownType> |
| 107 |
const char* ParseWithEnumValidation(const char* ptr, ParseContext* ctx, |
| 108 |
bool (*is_valid)(int), uint32 field_num, |
| 109 |
InternalMetadata* metadata) { |
| 110 |
typename Derived::template Parser<MapFieldLite, Map<Key, T>> parser(this); |
| 111 |
return parser.template ParseWithEnumValidation<UnknownType>( |
| 112 |
ptr, ctx, is_valid, field_num, metadata); |
| 113 |
} |
| 114 |
|
| 115 |
private: |
| 116 |
typedef void DestructorSkippable_; |
| 117 |
|
| 118 |
Map<Key, T> map_; |
| 119 |
|
| 120 |
friend class ::PROTOBUF_NAMESPACE_ID::Arena; |
| 121 |
}; |
| 122 |
|
| 123 |
template <typename UnknownType, typename T> |
| 124 |
struct EnumParseWrapper { |
| 125 |
const char* _InternalParse(const char* ptr, ParseContext* ctx) { |
| 126 |
return map_field->template ParseWithEnumValidation<UnknownType>( |
| 127 |
ptr, ctx, is_valid, field_num, metadata); |
| 128 |
} |
| 129 |
T* map_field; |
| 130 |
bool (*is_valid)(int); |
| 131 |
uint32 field_num; |
| 132 |
InternalMetadata* metadata; |
| 133 |
}; |
| 134 |
|
| 135 |
// Helper function because the typenames of maps are horrendous to print. This |
| 136 |
// leverages compiler type deduction, to keep all type data out of the |
| 137 |
// generated code |
| 138 |
template <typename UnknownType, typename T> |
| 139 |
EnumParseWrapper<UnknownType, T> InitEnumParseWrapper( |
| 140 |
T* map_field, bool (*is_valid)(int), uint32 field_num, |
| 141 |
InternalMetadata* metadata) { |
| 142 |
return EnumParseWrapper<UnknownType, T>{map_field, is_valid, field_num, |
| 143 |
metadata}; |
| 144 |
} |
| 145 |
|
| 146 |
// True if IsInitialized() is true for value field in all elements of t. T is |
| 147 |
// expected to be message. It's useful to have this helper here to keep the |
| 148 |
// protobuf compiler from ever having to emit loops in IsInitialized() methods. |
| 149 |
// We want the C++ compiler to inline this or not as it sees fit. |
| 150 |
template <typename Derived, typename Key, typename T, |
| 151 |
WireFormatLite::FieldType key_wire_type, |
| 152 |
WireFormatLite::FieldType value_wire_type> |
| 153 |
bool AllAreInitialized(const MapFieldLite<Derived, Key, T, key_wire_type, |
| 154 |
value_wire_type>& field) { |
| 155 |
const auto& t = field.GetMap(); |
| 156 |
for (typename Map<Key, T>::const_iterator it = t.begin(); it != t.end(); |
| 157 |
++it) { |
| 158 |
if (!it->second.IsInitialized()) return false; |
| 159 |
} |
| 160 |
return true; |
| 161 |
} |
| 162 |
|
| 163 |
template <typename MEntry> |
| 164 |
struct MapEntryToMapField : MapEntryToMapField<typename MEntry::SuperType> {}; |
| 165 |
|
| 166 |
template <typename T, typename Key, typename Value, |
| 167 |
WireFormatLite::FieldType kKeyFieldType, |
| 168 |
WireFormatLite::FieldType kValueFieldType> |
| 169 |
struct MapEntryToMapField< |
| 170 |
MapEntryLite<T, Key, Value, kKeyFieldType, kValueFieldType>> { |
| 171 |
typedef MapFieldLite< |
| 172 |
MapEntryLite<T, Key, Value, kKeyFieldType, kValueFieldType>, Key, Value, |
| 173 |
kKeyFieldType, kValueFieldType> |
| 174 |
MapFieldType; |
| 175 |
}; |
| 176 |
|
| 177 |
} // namespace internal |
| 178 |
} // namespace protobuf |
| 179 |
} // namespace google |
| 180 |
|
| 181 |
#include <google/protobuf/port_undef.inc> |
| 182 |
|
| 183 |
#endif // GOOGLE_PROTOBUF_MAP_FIELD_LITE_H__ |