| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 // | 4 // |
| 5 // Some helpers for quic that are for chromium codebase. | 5 // Some helpers for quic that are for chromium codebase. |
| 6 | 6 |
| 7 #ifndef NET_QUIC_QUIC_UTILS_CHROMIUM_H_ | 7 #ifndef NET_QUIC_QUIC_UTILS_CHROMIUM_H_ |
| 8 #define NET_QUIC_QUIC_UTILS_CHROMIUM_H_ | 8 #define NET_QUIC_QUIC_UTILS_CHROMIUM_H_ |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 // operator[] for lookup is discouraged for several reasons: | 25 // operator[] for lookup is discouraged for several reasons: |
| 26 // * It has a side-effect of inserting missing keys | 26 // * It has a side-effect of inserting missing keys |
| 27 // * It is not thread-safe (even when it is not inserting, it can still | 27 // * It is not thread-safe (even when it is not inserting, it can still |
| 28 // choose to resize the underlying storage) | 28 // choose to resize the underlying storage) |
| 29 // * It invalidates iterators (when it chooses to resize) | 29 // * It invalidates iterators (when it chooses to resize) |
| 30 // * It default constructs a value object even if it doesn't need to | 30 // * It default constructs a value object even if it doesn't need to |
| 31 // | 31 // |
| 32 // This version assumes the key is printable, and includes it in the fatal log | 32 // This version assumes the key is printable, and includes it in the fatal log |
| 33 // message. | 33 // message. |
| 34 template <class Collection> | 34 template <class Collection> |
| 35 const typename Collection::value_type::second_type& | 35 const typename Collection::value_type::second_type& FindOrDie( |
| 36 FindOrDie(const Collection& collection, | 36 const Collection& collection, |
| 37 const typename Collection::value_type::first_type& key) { | 37 const typename Collection::value_type::first_type& key) { |
| 38 typename Collection::const_iterator it = collection.find(key); | 38 typename Collection::const_iterator it = collection.find(key); |
| 39 CHECK(it != collection.end()) << "Map key not found: " << key; | 39 CHECK(it != collection.end()) << "Map key not found: " << key; |
| 40 return it->second; | 40 return it->second; |
| 41 } | 41 } |
| 42 | 42 |
| 43 // Same as above, but returns a non-const reference. | 43 // Same as above, but returns a non-const reference. |
| 44 template <class Collection> | 44 template <class Collection> |
| 45 typename Collection::value_type::second_type& | 45 typename Collection::value_type::second_type& FindOrDie( |
| 46 FindOrDie(Collection& collection, // NOLINT | 46 Collection& collection, // NOLINT |
| 47 const typename Collection::value_type::first_type& key) { | 47 const typename Collection::value_type::first_type& key) { |
| 48 typename Collection::iterator it = collection.find(key); | 48 typename Collection::iterator it = collection.find(key); |
| 49 CHECK(it != collection.end()) << "Map key not found: " << key; | 49 CHECK(it != collection.end()) << "Map key not found: " << key; |
| 50 return it->second; | 50 return it->second; |
| 51 } | 51 } |
| 52 | 52 |
| 53 // Returns a pointer to the const value associated with the given key if it | 53 // Returns a pointer to the const value associated with the given key if it |
| 54 // exists, or NULL otherwise. | 54 // exists, or NULL otherwise. |
| 55 template <class Collection> | 55 template <class Collection> |
| 56 const typename Collection::value_type::second_type* | 56 const typename Collection::value_type::second_type* FindOrNull( |
| 57 FindOrNull(const Collection& collection, | 57 const Collection& collection, |
| 58 const typename Collection::value_type::first_type& key) { | 58 const typename Collection::value_type::first_type& key) { |
| 59 typename Collection::const_iterator it = collection.find(key); | 59 typename Collection::const_iterator it = collection.find(key); |
| 60 if (it == collection.end()) { | 60 if (it == collection.end()) { |
| 61 return 0; | 61 return 0; |
| 62 } | 62 } |
| 63 return &it->second; | 63 return &it->second; |
| 64 } | 64 } |
| 65 | 65 |
| 66 // Same as above but returns a pointer to the non-const value. | 66 // Same as above but returns a pointer to the non-const value. |
| 67 template <class Collection> | 67 template <class Collection> |
| 68 typename Collection::value_type::second_type* | 68 typename Collection::value_type::second_type* FindOrNull( |
| 69 FindOrNull(Collection& collection, // NOLINT | 69 Collection& collection, // NOLINT |
| 70 const typename Collection::value_type::first_type& key) { | 70 const typename Collection::value_type::first_type& key) { |
| 71 typename Collection::iterator it = collection.find(key); | 71 typename Collection::iterator it = collection.find(key); |
| 72 if (it == collection.end()) { | 72 if (it == collection.end()) { |
| 73 return 0; | 73 return 0; |
| 74 } | 74 } |
| 75 return &it->second; | 75 return &it->second; |
| 76 } | 76 } |
| 77 | 77 |
| 78 } // namespace net | 78 } // namespace net |
| 79 | 79 |
| 80 #endif // NET_QUIC_QUIC_UTILS_CHROMIUM_H_ | 80 #endif // NET_QUIC_QUIC_UTILS_CHROMIUM_H_ |
| OLD | NEW |