map_field_lite.h
Go to the documentation of this file.
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>
37 #include <google/protobuf/map.h>
39 #include <google/protobuf/port.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 implentation 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, int default_enum_value = 0>
58 class MapFieldLite {
59  // Define message type for internal repeated field.
60  typedef Derived EntryType;
61 
62  public:
65 
67 
68  explicit MapFieldLite(Arena* arena) : arena_(arena), map_(arena) {
70  }
71 
72  // Accessors
73  const Map<Key, T>& GetMap() const { return map_; }
74  Map<Key, T>* MutableMap() { return &map_; }
75 
76  // Convenient methods for generated message implementation.
77  int size() const { return static_cast<int>(map_.size()); }
78  void Clear() { return map_.clear(); }
79  void MergeFrom(const MapFieldLite& other) {
80  for (typename Map<Key, T>::const_iterator it = other.map_.begin();
81  it != other.map_.end(); ++it) {
82  map_[it->first] = it->second;
83  }
84  }
85  void Swap(MapFieldLite* other) { map_.swap(other->map_); }
86 
87  // Set default enum value only for proto2 map field whose value is enum type.
89  MutableMap()->SetDefaultEnumValue(default_enum_value);
90  }
91 
92  // Used in the implementation of parsing. Caller should take the ownership iff
93  // arena_ is NULL.
94  EntryType* NewEntry() const {
95  if (arena_ == NULL) {
96  return new EntryType();
97  } else {
98  return Arena::CreateMessage<EntryType>(arena_);
99  }
100  }
101  // Used in the implementation of serializing enum value type. Caller should
102  // take the ownership iff arena_ is NULL.
103  EntryType* NewEnumEntryWrapper(const Key& key, const T t) const {
104  return EntryType::EnumWrap(key, t, arena_);
105  }
106  // Used in the implementation of serializing other value types. Caller should
107  // take the ownership iff arena_ is NULL.
108  EntryType* NewEntryWrapper(const Key& key, const T& t) const {
109  return EntryType::Wrap(key, t, arena_);
110  }
111 
112  const char* _InternalParse(const char* ptr, ParseContext* ctx) {
113  typename Derived::template Parser<MapFieldLite, Map<Key, T>> parser(this);
114  return parser._InternalParse(ptr, ctx);
115  }
116 
117  template <typename Metadata>
118  const char* ParseWithEnumValidation(const char* ptr, ParseContext* ctx,
119  bool (*is_valid)(int), uint32 field_num,
120  Metadata* metadata) {
121  typename Derived::template Parser<MapFieldLite, Map<Key, T>> parser(this);
122  return parser.ParseWithEnumValidation(ptr, ctx, is_valid, field_num,
123  metadata);
124  }
125 
126  private:
127  typedef void DestructorSkippable_;
128 
129  Arena* arena_;
131 
132  friend class ::PROTOBUF_NAMESPACE_ID::Arena;
133 };
134 
135 template <typename T, typename Metadata>
137  const char* _InternalParse(const char* ptr, ParseContext* ctx) {
138  return map_field->ParseWithEnumValidation(ptr, ctx, is_valid, field_num,
139  metadata);
140  }
142  bool (*is_valid)(int);
145 };
146 
147 // Helper function because the typenames of maps are horrendous to print. This
148 // leverages compiler type deduction, to keep all type data out of the
149 // generated code
150 template <typename T, typename Metadata>
152  bool (*is_valid)(int),
153  uint32 field_num,
154  Metadata* metadata) {
155  return EnumParseWrapper<T, Metadata>{map_field, is_valid, field_num,
156  metadata};
157 }
158 
159 // True if IsInitialized() is true for value field in all elements of t. T is
160 // expected to be message. It's useful to have this helper here to keep the
161 // protobuf compiler from ever having to emit loops in IsInitialized() methods.
162 // We want the C++ compiler to inline this or not as it sees fit.
163 template <typename Key, typename T>
165  for (typename Map<Key, T>::const_iterator it = t.begin(); it != t.end();
166  ++it) {
167  if (!it->second.IsInitialized()) return false;
168  }
169  return true;
170 }
171 
172 template <typename MEntry>
173 struct MapEntryToMapField : MapEntryToMapField<typename MEntry::SuperType> {};
174 
175 template <typename T, typename Key, typename Value,
176  WireFormatLite::FieldType kKeyFieldType,
177  WireFormatLite::FieldType kValueFieldType, int default_enum_value>
178 struct MapEntryToMapField<MapEntryLite<T, Key, Value, kKeyFieldType,
179  kValueFieldType, default_enum_value>> {
180  typedef MapFieldLite<MapEntryLite<T, Key, Value, kKeyFieldType,
181  kValueFieldType, default_enum_value>,
182  Key, Value, kKeyFieldType, kValueFieldType,
183  default_enum_value>
185 };
186 
187 } // namespace internal
188 } // namespace protobuf
189 } // namespace google
190 
191 #include <google/protobuf/port_undef.inc>
192 
193 #endif // GOOGLE_PROTOBUF_MAP_FIELD_LITE_H__
google::protobuf.internal::MapFieldLite::MutableMap
Map< Key, T > * MutableMap()
Definition: map_field_lite.h:74
google::protobuf.internal::MapFieldLite::DestructorSkippable_
void DestructorSkippable_
Definition: map_field_lite.h:127
wire_format_lite.h
google::protobuf::Map::begin
iterator begin()
Definition: map.h:1033
google::protobuf.internal::MapFieldLite
Definition: map.h:73
google::protobuf.internal::MapEntryToMapField< MapEntryLite< T, Key, Value, kKeyFieldType, kValueFieldType, default_enum_value > >::MapFieldType
MapFieldLite< MapEntryLite< T, Key, Value, kKeyFieldType, kValueFieldType, default_enum_value >, Key, Value, kKeyFieldType, kValueFieldType, default_enum_value > MapFieldType
Definition: map_field_lite.h:184
NULL
NULL
Definition: test_security_zap.cpp:405
google::protobuf.internal::MapFieldLite::MapFieldLite
MapFieldLite()
Definition: map_field_lite.h:66
benchmarks.util.result_uploader.metadata
def metadata
Definition: result_uploader.py:97
google::protobuf.internal::EnumParseWrapper::_InternalParse
const char * _InternalParse(const char *ptr, ParseContext *ctx)
Definition: map_field_lite.h:137
google::protobuf::uint32
uint32_t uint32
Definition: protobuf/src/google/protobuf/stubs/port.h:155
google::protobuf.internal::ParseContext
Definition: parse_context.h:322
google::protobuf.internal::MapFieldLite::map_
Map< Key, T > map_
Definition: map_field_lite.h:130
google::protobuf.internal::MapFieldLite::SetDefaultEnumValue
void SetDefaultEnumValue()
Definition: map_field_lite.h:88
google::protobuf.internal::AllAreInitialized
bool AllAreInitialized(const Type &t)
Definition: generated_message_util.h:99
google::protobuf.internal::MapFieldLite::size
int size() const
Definition: map_field_lite.h:77
Value
Definition: struct.pb.h:304
google::protobuf.internal::MapEntryLite
Definition: map_entry_lite.h:597
port.h
T
#define T(upbtypeconst, upbtype, ctype, default_value)
google::protobuf.internal::MapFieldLite::GetMap
const Map< Key, T > & GetMap() const
Definition: map_field_lite.h:73
google::protobuf.internal::MapFieldLite::_InternalParse
const char * _InternalParse(const char *ptr, ParseContext *ctx)
Definition: map_field_lite.h:112
google::protobuf.internal::MapFieldLite::EntryTypeTrait
EntryType EntryTypeTrait
Definition: map_field_lite.h:64
parse_context.h
google::protobuf.internal::MapFieldLite::MapFieldLite
MapFieldLite(Arena *arena)
Definition: map_field_lite.h:68
map_entry_lite.h
coded_stream.h
google::protobuf.internal::MapFieldLite::Clear
void Clear()
Definition: map_field_lite.h:78
google::protobuf.internal::WireFormatLite::FieldType
FieldType
Definition: wire_format_lite.h:111
google::protobuf.internal::MapFieldLite::arena_
Arena * arena_
Definition: map_field_lite.h:129
google::protobuf.internal::MapFieldLite::MapType
Map< Key, T > MapType
Definition: map_field_lite.h:63
google::protobuf.internal::MapEntryToMapField
Definition: map_field_lite.h:173
map.h
key
const SETUP_TEARDOWN_TESTCONTEXT char * key
Definition: test_wss_transport.cpp:10
google::protobuf.internal::MapFieldLite::ParseWithEnumValidation
const char * ParseWithEnumValidation(const char *ptr, ParseContext *ctx, bool(*is_valid)(int), uint32 field_num, Metadata *metadata)
Definition: map_field_lite.h:118
google::protobuf::Metadata
Definition: src/google/protobuf/message.h:190
google::protobuf.internal::InitEnumParseWrapper
EnumParseWrapper< T, Metadata > InitEnumParseWrapper(T *map_field, bool(*is_valid)(int), uint32 field_num, Metadata *metadata)
Definition: map_field_lite.h:151
google::protobuf.internal::EnumParseWrapper::field_num
uint32 field_num
Definition: map_field_lite.h:143
google::protobuf.internal::MapFieldLite::NewEnumEntryWrapper
EntryType * NewEnumEntryWrapper(const Key &key, const T t) const
Definition: map_field_lite.h:103
google::protobuf.internal::EnumParseWrapper::metadata
Metadata * metadata
Definition: map_field_lite.h:144
google::protobuf::Map::end
iterator end()
Definition: map.h:1034
google::protobuf.internal::EnumParseWrapper
Definition: map_field_lite.h:136
google::protobuf.internal::MapFieldLite::EntryType
Derived EntryType
Definition: map_field_lite.h:60
google::protobuf.internal::EnumParseWrapper::is_valid
bool(* is_valid)(int)
Definition: map_field_lite.h:142
google::protobuf::Map
Definition: map.h:62
internal
Definition: any.pb.h:40
google::protobuf.internal::EnumParseWrapper::map_field
T * map_field
Definition: map_field_lite.h:141
google::protobuf.internal::MapFieldLite::MergeFrom
void MergeFrom(const MapFieldLite &other)
Definition: map_field_lite.h:79
benchmarks.python.py_benchmark.parser
parser
Definition: py_benchmark.py:10
google::protobuf.internal::MapFieldLite::NewEntryWrapper
EntryType * NewEntryWrapper(const Key &key, const T &t) const
Definition: map_field_lite.h:108
it
MapIter it
Definition: php/ext/google/protobuf/map.c:205
google::protobuf.internal::MapFieldLite::NewEntry
EntryType * NewEntry() const
Definition: map_field_lite.h:94
google
Definition: data_proto2_to_proto3_util.h:11
google::protobuf.internal::MapFieldLite::Swap
void Swap(MapFieldLite *other)
Definition: map_field_lite.h:85
Value
struct Value Value
Definition: php/ext/google/protobuf/protobuf.h:667


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:56