| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 // | |
| 5 // Some helpers for quic that are for chromium codebase. | |
| 6 | |
| 7 #ifndef NET_QUIC_QUIC_UTILS_CHROMIUM_H_ | |
| 8 #define NET_QUIC_QUIC_UTILS_CHROMIUM_H_ | |
| 9 | |
| 10 #include "base/basictypes.h" | |
| 11 #include "base/logging.h" | |
| 12 | |
| 13 namespace net { | |
| 14 | |
| 15 // | |
| 16 // Find*() | |
| 17 // | |
| 18 | |
| 19 // Returns a const reference to the value associated with the given key if it | |
| 20 // exists. Crashes otherwise. | |
| 21 // | |
| 22 // This is intended as a replacement for operator[] as an rvalue (for reading) | |
| 23 // when the key is guaranteed to exist. | |
| 24 // | |
| 25 // operator[] for lookup is discouraged for several reasons: | |
| 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 | |
| 28 // choose to resize the underlying storage) | |
| 29 // * It invalidates iterators (when it chooses to resize) | |
| 30 // * It default constructs a value object even if it doesn't need to | |
| 31 // | |
| 32 // This version assumes the key is printable, and includes it in the fatal log | |
| 33 // message. | |
| 34 template <class Collection> | |
| 35 const typename Collection::value_type::second_type& | |
| 36 FindOrDie(const Collection& collection, | |
| 37 const typename Collection::value_type::first_type& key) { | |
| 38 typename Collection::const_iterator it = collection.find(key); | |
| 39 CHECK(it != collection.end()) << "Map key not found: " << key; | |
| 40 return it->second; | |
| 41 } | |
| 42 | |
| 43 // Same as above, but returns a non-const reference. | |
| 44 template <class Collection> | |
| 45 typename Collection::value_type::second_type& | |
| 46 FindOrDie(Collection& collection, // NOLINT | |
| 47 const typename Collection::value_type::first_type& key) { | |
| 48 typename Collection::iterator it = collection.find(key); | |
| 49 CHECK(it != collection.end()) << "Map key not found: " << key; | |
| 50 return it->second; | |
| 51 } | |
| 52 | |
| 53 // Returns a pointer to the const value associated with the given key if it | |
| 54 // exists, or NULL otherwise. | |
| 55 template <class Collection> | |
| 56 const typename Collection::value_type::second_type* | |
| 57 FindOrNull(const Collection& collection, | |
| 58 const typename Collection::value_type::first_type& key) { | |
| 59 typename Collection::const_iterator it = collection.find(key); | |
| 60 if (it == collection.end()) { | |
| 61 return 0; | |
| 62 } | |
| 63 return &it->second; | |
| 64 } | |
| 65 | |
| 66 // Same as above but returns a pointer to the non-const value. | |
| 67 template <class Collection> | |
| 68 typename Collection::value_type::second_type* | |
| 69 FindOrNull(Collection& collection, // NOLINT | |
| 70 const typename Collection::value_type::first_type& key) { | |
| 71 typename Collection::iterator it = collection.find(key); | |
| 72 if (it == collection.end()) { | |
| 73 return 0; | |
| 74 } | |
| 75 return &it->second; | |
| 76 } | |
| 77 | |
| 78 } // namespace net | |
| 79 | |
| 80 #endif // NET_QUIC_QUIC_UTILS_CHROMIUM_H_ | |
| OLD | NEW |