Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-31 10:12:22

0001 // Protocol Buffers - Google's data interchange format
0002 // Copyright 2008 Google Inc.  All rights reserved.
0003 //
0004 // Use of this source code is governed by a BSD-style
0005 // license that can be found in the LICENSE file or at
0006 // https://developers.google.com/open-source/licenses/bsd
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 // Must be included last.
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 // This class provides access to map field using generated api. It is used for
0033 // internal generated message implementation only. Users should never use this
0034 // directly.
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     // We want to destruct the map in such a way that we can verify
0056     // that we've done that, but also be sure that we've deallocated
0057     // everything (as opposed to leaving an allocation behind with no
0058     // data in it, as would happen if a vector was resize'd to zero.
0059     // Map::Swap with an empty map accomplishes that.
0060     decltype(map_) swapped_map(map_.arena());
0061     map_.InternalSwap(&swapped_map);
0062   }
0063 #endif
0064   // Accessors
0065   const Map<Key, T>& GetMap() const { return map_; }
0066   Map<Key, T>* MutableMap() { return &map_; }
0067 
0068   // Convenient methods for generated message implementation.
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   // map_ is inside an anonymous union so we can explicitly control its
0081   // destruction
0082   union {
0083     Map<Key, T> map_;
0084   };
0085 
0086   friend class google::protobuf::Arena;
0087 };
0088 
0089 // True if IsInitialized() is true for value field in all elements of t. T is
0090 // expected to be message.  It's useful to have this helper here to keep the
0091 // protobuf compiler from ever having to emit loops in IsInitialized() methods.
0092 // We want the C++ compiler to inline this or not as it sees fit.
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 }  // namespace internal
0107 }  // namespace protobuf
0108 }  // namespace google
0109 
0110 #include "google/protobuf/port_undef.inc"
0111 
0112 #endif  // GOOGLE_PROTOBUF_MAP_FIELD_LITE_H__