/thirdparty/breakpad/third_party/protobuf/protobuf/src/google/protobuf/stubs/map-util.h

http://github.com/tomahawk-player/tomahawk · C++ Header · 119 lines · 57 code · 11 blank · 51 comment · 7 complexity · d391071d2b278937c11129b1f8818f74 MD5 · raw file

  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // http://code.google.com/p/protobuf/
  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. // from google3/util/gtl/map-util.h
  31. // Author: Anton Carver
  32. #ifndef GOOGLE_PROTOBUF_STUBS_MAP_UTIL_H__
  33. #define GOOGLE_PROTOBUF_STUBS_MAP_UTIL_H__
  34. #include <google/protobuf/stubs/common.h>
  35. namespace google {
  36. namespace protobuf {
  37. // Perform a lookup in a map or hash_map.
  38. // If the key is present in the map then the value associated with that
  39. // key is returned, otherwise the value passed as a default is returned.
  40. template <class Collection>
  41. const typename Collection::value_type::second_type&
  42. FindWithDefault(const Collection& collection,
  43. const typename Collection::value_type::first_type& key,
  44. const typename Collection::value_type::second_type& value) {
  45. typename Collection::const_iterator it = collection.find(key);
  46. if (it == collection.end()) {
  47. return value;
  48. }
  49. return it->second;
  50. }
  51. // Perform a lookup in a map or hash_map.
  52. // If the key is present a const pointer to the associated value is returned,
  53. // otherwise a NULL pointer is returned.
  54. template <class Collection>
  55. const typename Collection::value_type::second_type*
  56. FindOrNull(const Collection& collection,
  57. const typename Collection::value_type::first_type& key) {
  58. typename Collection::const_iterator it = collection.find(key);
  59. if (it == collection.end()) {
  60. return 0;
  61. }
  62. return &it->second;
  63. }
  64. // Perform a lookup in a map or hash_map whose values are pointers.
  65. // If the key is present a const pointer to the associated value is returned,
  66. // otherwise a NULL pointer is returned.
  67. // This function does not distinguish between a missing key and a key mapped
  68. // to a NULL value.
  69. template <class Collection>
  70. const typename Collection::value_type::second_type
  71. FindPtrOrNull(const Collection& collection,
  72. const typename Collection::value_type::first_type& key) {
  73. typename Collection::const_iterator it = collection.find(key);
  74. if (it == collection.end()) {
  75. return 0;
  76. }
  77. return it->second;
  78. }
  79. // Change the value associated with a particular key in a map or hash_map.
  80. // If the key is not present in the map the key and value are inserted,
  81. // otherwise the value is updated to be a copy of the value provided.
  82. // True indicates that an insert took place, false indicates an update.
  83. template <class Collection, class Key, class Value>
  84. bool InsertOrUpdate(Collection * const collection,
  85. const Key& key, const Value& value) {
  86. pair<typename Collection::iterator, bool> ret =
  87. collection->insert(typename Collection::value_type(key, value));
  88. if (!ret.second) {
  89. // update
  90. ret.first->second = value;
  91. return false;
  92. }
  93. return true;
  94. }
  95. // Insert a new key and value into a map or hash_map.
  96. // If the key is not present in the map the key and value are
  97. // inserted, otherwise nothing happens. True indicates that an insert
  98. // took place, false indicates the key was already present.
  99. template <class Collection, class Key, class Value>
  100. bool InsertIfNotPresent(Collection * const collection,
  101. const Key& key, const Value& value) {
  102. pair<typename Collection::iterator, bool> ret =
  103. collection->insert(typename Collection::value_type(key, value));
  104. return ret.second;
  105. }
  106. } // namespace protobuf
  107. } // namespace google
  108. #endif // GOOGLE_PROTOBUF_STUBS_MAP_UTIL_H__