File indexing completed on 2025-01-31 10:12:22
0001
0002
0003
0004
0005
0006
0007
0008 #ifndef GOOGLE_PROTOBUF_MAP_FIELD_LITE_H__
0009 #define GOOGLE_PROTOBUF_MAP_FIELD_LITE_H__
0010
0011 #include <type_traits>
0012
0013 #include "absl/log/absl_check.h"
0014 #include "google/protobuf/internal_visibility.h"
0015 #include "google/protobuf/io/coded_stream.h"
0016 #include "google/protobuf/map.h"
0017 #include "google/protobuf/parse_context.h"
0018 #include "google/protobuf/port.h"
0019 #include "google/protobuf/wire_format_lite.h"
0020
0021
0022 #include "google/protobuf/port_def.inc"
0023
0024 #ifdef SWIG
0025 #error "You cannot SWIG proto headers"
0026 #endif
0027
0028 namespace google {
0029 namespace protobuf {
0030 namespace internal {
0031
0032
0033
0034
0035 template <typename Key, typename T>
0036 class MapFieldLite {
0037 public:
0038 typedef Map<Key, T> MapType;
0039
0040 constexpr MapFieldLite() : map_() {}
0041 explicit MapFieldLite(Arena* arena) : map_(arena) {}
0042 MapFieldLite(ArenaInitialized, Arena* arena) : MapFieldLite(arena) {}
0043
0044 MapFieldLite(InternalVisibility, Arena* arena) : map_(arena) {}
0045 MapFieldLite(InternalVisibility, Arena* arena, const MapFieldLite& from)
0046 : map_(arena) {
0047 MergeFrom(from);
0048 }
0049
0050 #ifdef NDEBUG
0051 ~MapFieldLite() { map_.~Map(); }
0052 #else
0053 ~MapFieldLite() {
0054 ABSL_DCHECK_EQ(map_.arena(), nullptr);
0055
0056
0057
0058
0059
0060 decltype(map_) swapped_map(map_.arena());
0061 map_.InternalSwap(&swapped_map);
0062 }
0063 #endif
0064
0065 const Map<Key, T>& GetMap() const { return map_; }
0066 Map<Key, T>* MutableMap() { return &map_; }
0067
0068
0069 int size() const { return static_cast<int>(map_.size()); }
0070 void Clear() { return map_.clear(); }
0071 void MergeFrom(const MapFieldLite& other) {
0072 internal::MapMergeFrom(map_, other.map_);
0073 }
0074 void Swap(MapFieldLite* other) { map_.swap(other->map_); }
0075 void InternalSwap(MapFieldLite* other) { map_.InternalSwap(&other->map_); }
0076
0077 private:
0078 typedef void DestructorSkippable_;
0079
0080
0081
0082 union {
0083 Map<Key, T> map_;
0084 };
0085
0086 friend class google::protobuf::Arena;
0087 };
0088
0089
0090
0091
0092
0093 template <typename Key, typename T>
0094 bool AllAreInitialized(const MapFieldLite<Key, T>& field) {
0095 const auto& t = field.GetMap();
0096 for (typename Map<Key, T>::const_iterator it = t.begin(); it != t.end();
0097 ++it) {
0098 if (!it->second.IsInitialized()) return false;
0099 }
0100 return true;
0101 }
0102
0103 template <typename MEntry>
0104 struct MapEntryToMapField : MapEntryToMapField<typename MEntry::SuperType> {};
0105
0106 }
0107 }
0108 }
0109
0110 #include "google/protobuf/port_undef.inc"
0111
0112 #endif