Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / net / quic / quic_utils_chromium.h
blob0229d1d04002d39ca5e3313081435e11dd60105c
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.
7 #ifndef NET_QUIC_QUIC_UTILS_CHROMIUM_H_
8 #define NET_QUIC_QUIC_UTILS_CHROMIUM_H_
10 #include "base/basictypes.h"
11 #include "base/logging.h"
13 namespace net {
16 // Find*()
19 // Returns a const reference to the value associated with the given key if it
20 // exists. Crashes otherwise.
22 // This is intended as a replacement for operator[] as an rvalue (for reading)
23 // when the key is guaranteed to exist.
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
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;
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;
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;
63 return &it->second;
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;
75 return &it->second;
78 } // namespace net
80 #endif // NET_QUIC_QUIC_UTILS_CHROMIUM_H_