map_field_lite.h 7.0 KB

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