map_field_lite.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 <google/protobuf/map.h>
  33. #include <google/protobuf/map_entry_lite.h>
  34. #include <google/protobuf/wire_format_lite.h>
  35. namespace google {
  36. namespace protobuf {
  37. namespace internal {
  38. // This class provides access to map field using generated api. It is used for
  39. // internal generated message implentation only. Users should never use this
  40. // directly.
  41. template <typename Derived, typename Key, typename T,
  42. WireFormatLite::FieldType key_wire_type,
  43. WireFormatLite::FieldType value_wire_type, int default_enum_value = 0>
  44. class MapFieldLite {
  45. // Define message type for internal repeated field.
  46. typedef Derived EntryType;
  47. public:
  48. typedef Map<Key, T> MapType;
  49. typedef EntryType EntryTypeTrait;
  50. MapFieldLite() : arena_(NULL) { SetDefaultEnumValue(); }
  51. explicit MapFieldLite(Arena* arena) : arena_(arena), map_(arena) {
  52. SetDefaultEnumValue();
  53. }
  54. // Accessors
  55. const Map<Key, T>& GetMap() const { return map_; }
  56. Map<Key, T>* MutableMap() { return &map_; }
  57. // Convenient methods for generated message implementation.
  58. int size() const { return static_cast<int>(map_.size()); }
  59. void Clear() { return map_.clear(); }
  60. void MergeFrom(const MapFieldLite& other) {
  61. for (typename Map<Key, T>::const_iterator it = other.map_.begin();
  62. it != other.map_.end(); ++it) {
  63. map_[it->first] = it->second;
  64. }
  65. }
  66. void Swap(MapFieldLite* other) { map_.swap(other->map_); }
  67. // Set default enum value only for proto2 map field whose value is enum type.
  68. void SetDefaultEnumValue() {
  69. MutableMap()->SetDefaultEnumValue(default_enum_value);
  70. }
  71. // Used in the implementation of parsing. Caller should take the ownership iff
  72. // arena_ is NULL.
  73. EntryType* NewEntry() const {
  74. if (arena_ == NULL) {
  75. return new EntryType();
  76. } else {
  77. return Arena::CreateMessage<EntryType>(arena_);
  78. }
  79. }
  80. // Used in the implementation of serializing enum value type. Caller should
  81. // take the ownership iff arena_ is NULL.
  82. EntryType* NewEnumEntryWrapper(const Key& key, const T t) const {
  83. return EntryType::EnumWrap(key, t, arena_);
  84. }
  85. // Used in the implementation of serializing other value types. Caller should
  86. // take the ownership iff arena_ is NULL.
  87. EntryType* NewEntryWrapper(const Key& key, const T& t) const {
  88. return EntryType::Wrap(key, t, arena_);
  89. }
  90. private:
  91. typedef void DestructorSkippable_;
  92. Arena* arena_;
  93. Map<Key, T> map_;
  94. friend class ::google::protobuf::Arena;
  95. };
  96. // True if IsInitialized() is true for value field in all elements of t. T is
  97. // expected to be message. It's useful to have this helper here to keep the
  98. // protobuf compiler from ever having to emit loops in IsInitialized() methods.
  99. // We want the C++ compiler to inline this or not as it sees fit.
  100. template <typename Key, typename T>
  101. bool AllAreInitialized(const Map<Key, T>& t) {
  102. for (typename Map<Key, T>::const_iterator it = t.begin(); it != t.end();
  103. ++it) {
  104. if (!it->second.IsInitialized()) return false;
  105. }
  106. return true;
  107. }
  108. template <typename MEntry>
  109. struct MapEntryToMapField : MapEntryToMapField<typename MEntry::SuperType> {};
  110. template <typename T, typename Key, typename Value,
  111. WireFormatLite::FieldType kKeyFieldType,
  112. WireFormatLite::FieldType kValueFieldType, int default_enum_value>
  113. struct MapEntryToMapField<MapEntryLite<T, Key, Value, kKeyFieldType,
  114. kValueFieldType, default_enum_value> > {
  115. typedef MapFieldLite<MapEntryLite<T, Key, Value, kKeyFieldType,
  116. kValueFieldType, default_enum_value>,
  117. Key, Value, kKeyFieldType, kValueFieldType,
  118. default_enum_value>
  119. MapFieldType;
  120. };
  121. } // namespace internal
  122. } // namespace protobuf
  123. } // namespace google
  124. #endif // GOOGLE_PROTOBUF_MAP_FIELD_LITE_H__