[email protected] | cc7ec7f | 2014-01-25 20:32:51 | [diff] [blame] | 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 | |
Ryan Hamilton | a3ee93a7 | 2018-08-01 22:03:08 | [diff] [blame] | 7 | #ifndef NET_QUIC_QUIC_UTILS_CHROMIUM_H_ |
| 8 | #define NET_QUIC_QUIC_UTILS_CHROMIUM_H_ |
[email protected] | cc7ec7f | 2014-01-25 20:32:51 | [diff] [blame] | 9 | |
rch | 4c967ea | 2016-12-22 01:41:07 | [diff] [blame] | 10 | #include <string> |
| 11 | |
[email protected] | cc7ec7f | 2014-01-25 20:32:51 | [diff] [blame] | 12 | #include "base/logging.h" |
rch | 4c967ea | 2016-12-22 01:41:07 | [diff] [blame] | 13 | #include "net/base/net_export.h" |
Victor Vasiliev | 6bb59d2 | 2019-03-08 21:34:51 | [diff] [blame] | 14 | #include "net/third_party/quiche/src/quic/core/quic_tag.h" |
[email protected] | cc7ec7f | 2014-01-25 20:32:51 | [diff] [blame] | 15 | |
| 16 | namespace net { |
| 17 | |
| 18 | // |
| 19 | // Find*() |
| 20 | // |
| 21 | |
| 22 | // Returns a const reference to the value associated with the given key if it |
| 23 | // exists. Crashes otherwise. |
| 24 | // |
| 25 | // This is intended as a replacement for operator[] as an rvalue (for reading) |
| 26 | // when the key is guaranteed to exist. |
| 27 | // |
| 28 | // operator[] for lookup is discouraged for several reasons: |
| 29 | // * It has a side-effect of inserting missing keys |
| 30 | // * It is not thread-safe (even when it is not inserting, it can still |
| 31 | // choose to resize the underlying storage) |
| 32 | // * It invalidates iterators (when it chooses to resize) |
| 33 | // * It default constructs a value object even if it doesn't need to |
| 34 | // |
| 35 | // This version assumes the key is printable, and includes it in the fatal log |
| 36 | // message. |
| 37 | template <class Collection> |
rjshade | d5ced07 | 2015-12-18 19:26:02 | [diff] [blame] | 38 | const typename Collection::value_type::second_type& FindOrDie( |
| 39 | const Collection& collection, |
| 40 | const typename Collection::value_type::first_type& key) { |
[email protected] | cc7ec7f | 2014-01-25 20:32:51 | [diff] [blame] | 41 | typename Collection::const_iterator it = collection.find(key); |
| 42 | CHECK(it != collection.end()) << "Map key not found: " << key; |
| 43 | return it->second; |
| 44 | } |
| 45 | |
| 46 | // Same as above, but returns a non-const reference. |
| 47 | template <class Collection> |
rjshade | d5ced07 | 2015-12-18 19:26:02 | [diff] [blame] | 48 | typename Collection::value_type::second_type& FindOrDie( |
| 49 | Collection& collection, // NOLINT |
| 50 | const typename Collection::value_type::first_type& key) { |
[email protected] | cc7ec7f | 2014-01-25 20:32:51 | [diff] [blame] | 51 | typename Collection::iterator it = collection.find(key); |
| 52 | CHECK(it != collection.end()) << "Map key not found: " << key; |
| 53 | return it->second; |
| 54 | } |
| 55 | |
| 56 | // Returns a pointer to the const value associated with the given key if it |
| 57 | // exists, or NULL otherwise. |
| 58 | template <class Collection> |
rjshade | d5ced07 | 2015-12-18 19:26:02 | [diff] [blame] | 59 | const typename Collection::value_type::second_type* FindOrNull( |
| 60 | const Collection& collection, |
| 61 | const typename Collection::value_type::first_type& key) { |
[email protected] | cc7ec7f | 2014-01-25 20:32:51 | [diff] [blame] | 62 | typename Collection::const_iterator it = collection.find(key); |
| 63 | if (it == collection.end()) { |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 64 | return nullptr; |
[email protected] | cc7ec7f | 2014-01-25 20:32:51 | [diff] [blame] | 65 | } |
| 66 | return &it->second; |
| 67 | } |
| 68 | |
| 69 | // Same as above but returns a pointer to the non-const value. |
| 70 | template <class Collection> |
rjshade | d5ced07 | 2015-12-18 19:26:02 | [diff] [blame] | 71 | typename Collection::value_type::second_type* FindOrNull( |
| 72 | Collection& collection, // NOLINT |
| 73 | const typename Collection::value_type::first_type& key) { |
[email protected] | cc7ec7f | 2014-01-25 20:32:51 | [diff] [blame] | 74 | typename Collection::iterator it = collection.find(key); |
| 75 | if (it == collection.end()) { |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 76 | return nullptr; |
[email protected] | cc7ec7f | 2014-01-25 20:32:51 | [diff] [blame] | 77 | } |
| 78 | return &it->second; |
| 79 | } |
| 80 | |
rch | 4c967ea | 2016-12-22 01:41:07 | [diff] [blame] | 81 | // Returns the list of QUIC tags represented by the comma separated |
| 82 | // string in |connection_options|. |
Ryan Hamilton | 8d9ee76e | 2018-05-29 23:52:52 | [diff] [blame] | 83 | NET_EXPORT quic::QuicTagVector ParseQuicConnectionOptions( |
| 84 | const std::string& connection_options); |
rch | 4c967ea | 2016-12-22 01:41:07 | [diff] [blame] | 85 | |
[email protected] | cc7ec7f | 2014-01-25 20:32:51 | [diff] [blame] | 86 | } // namespace net |
| 87 | |
Ryan Hamilton | a3ee93a7 | 2018-08-01 22:03:08 | [diff] [blame] | 88 | #endif // NET_QUIC_QUIC_UTILS_CHROMIUM_H_ |