blob: 4932bfa23808f4621e748a70bb75ff95b106d49d [file] [log] [blame]
[email protected]cc7ec7f2014-01-25 20:32:511// 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 Hamiltona3ee93a72018-08-01 22:03:087#ifndef NET_QUIC_QUIC_UTILS_CHROMIUM_H_
8#define NET_QUIC_QUIC_UTILS_CHROMIUM_H_
[email protected]cc7ec7f2014-01-25 20:32:519
rch4c967ea2016-12-22 01:41:0710#include <string>
11
[email protected]cc7ec7f2014-01-25 20:32:5112#include "base/logging.h"
rch4c967ea2016-12-22 01:41:0713#include "net/base/net_export.h"
Victor Vasiliev6bb59d22019-03-08 21:34:5114#include "net/third_party/quiche/src/quic/core/quic_tag.h"
[email protected]cc7ec7f2014-01-25 20:32:5115
16namespace 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.
37template <class Collection>
rjshaded5ced072015-12-18 19:26:0238const typename Collection::value_type::second_type& FindOrDie(
39 const Collection& collection,
40 const typename Collection::value_type::first_type& key) {
[email protected]cc7ec7f2014-01-25 20:32:5141 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.
47template <class Collection>
rjshaded5ced072015-12-18 19:26:0248typename Collection::value_type::second_type& FindOrDie(
49 Collection& collection, // NOLINT
50 const typename Collection::value_type::first_type& key) {
[email protected]cc7ec7f2014-01-25 20:32:5151 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.
58template <class Collection>
rjshaded5ced072015-12-18 19:26:0259const typename Collection::value_type::second_type* FindOrNull(
60 const Collection& collection,
61 const typename Collection::value_type::first_type& key) {
[email protected]cc7ec7f2014-01-25 20:32:5162 typename Collection::const_iterator it = collection.find(key);
63 if (it == collection.end()) {
Raul Tambre94493c652019-03-11 17:18:3564 return nullptr;
[email protected]cc7ec7f2014-01-25 20:32:5165 }
66 return &it->second;
67}
68
69// Same as above but returns a pointer to the non-const value.
70template <class Collection>
rjshaded5ced072015-12-18 19:26:0271typename Collection::value_type::second_type* FindOrNull(
72 Collection& collection, // NOLINT
73 const typename Collection::value_type::first_type& key) {
[email protected]cc7ec7f2014-01-25 20:32:5174 typename Collection::iterator it = collection.find(key);
75 if (it == collection.end()) {
Raul Tambre94493c652019-03-11 17:18:3576 return nullptr;
[email protected]cc7ec7f2014-01-25 20:32:5177 }
78 return &it->second;
79}
80
rch4c967ea2016-12-22 01:41:0781// Returns the list of QUIC tags represented by the comma separated
82// string in |connection_options|.
Ryan Hamilton8d9ee76e2018-05-29 23:52:5283NET_EXPORT quic::QuicTagVector ParseQuicConnectionOptions(
84 const std::string& connection_options);
rch4c967ea2016-12-22 01:41:0785
[email protected]cc7ec7f2014-01-25 20:32:5186} // namespace net
87
Ryan Hamiltona3ee93a72018-08-01 22:03:0888#endif // NET_QUIC_QUIC_UTILS_CHROMIUM_H_