| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 | 5 // Some helpers for quic. |
| 6 | 6 |
| 7 #ifndef NET_QUIC_QUIC_UTILS_H_ | 7 #ifndef NET_QUIC_QUIC_UTILS_H_ |
| 8 #define NET_QUIC_QUIC_UTILS_H_ | 8 #define NET_QUIC_QUIC_UTILS_H_ |
| 9 | 9 |
| 10 #include "net/base/int128.h" | 10 #include "net/base/int128.h" |
| 11 #include "net/base/net_export.h" | 11 #include "net/base/net_export.h" |
| 12 #include "net/quic/quic_protocol.h" | 12 #include "net/quic/quic_protocol.h" |
| 13 | 13 |
| 14 namespace net { | 14 namespace net { |
| 15 | 15 |
| 16 // | |
| 17 // Find*() | |
| 18 // | |
| 19 | |
| 20 // Returns a const reference to the value associated with the given key if it | |
| 21 // exists. Crashes otherwise. | |
| 22 // | |
| 23 // This is intended as a replacement for operator[] as an rvalue (for reading) | |
| 24 // when the key is guaranteed to exist. | |
| 25 // | |
| 26 // operator[] for lookup is discouraged for several reasons: | |
| 27 // * It has a side-effect of inserting missing keys | |
| 28 // * It is not thread-safe (even when it is not inserting, it can still | |
| 29 // choose to resize the underlying storage) | |
| 30 // * It invalidates iterators (when it chooses to resize) | |
| 31 // * It default constructs a value object even if it doesn't need to | |
| 32 // | |
| 33 // This version assumes the key is printable, and includes it in the fatal log | |
| 34 // message. | |
| 35 template <class Collection> | |
| 36 const typename Collection::value_type::second_type& | |
| 37 FindOrDie(const Collection& collection, | |
| 38 const typename Collection::value_type::first_type& key) { | |
| 39 typename Collection::const_iterator it = collection.find(key); | |
| 40 CHECK(it != collection.end()) << "Map key not found: " << key; | |
| 41 return it->second; | |
| 42 } | |
| 43 | |
| 44 // Same as above, but returns a non-const reference. | |
| 45 template <class Collection> | |
| 46 typename Collection::value_type::second_type& | |
| 47 FindOrDie(Collection& collection, // NOLINT | |
| 48 const typename Collection::value_type::first_type& key) { | |
| 49 typename Collection::iterator it = collection.find(key); | |
| 50 CHECK(it != collection.end()) << "Map key not found: " << key; | |
| 51 return it->second; | |
| 52 } | |
| 53 | |
| 54 // Returns a pointer to the const value associated with the given key if it | |
| 55 // exists, or NULL otherwise. | |
| 56 template <class Collection> | |
| 57 const typename Collection::value_type::second_type* | |
| 58 FindOrNull(const Collection& collection, | |
| 59 const typename Collection::value_type::first_type& key) { | |
| 60 typename Collection::const_iterator it = collection.find(key); | |
| 61 if (it == collection.end()) { | |
| 62 return 0; | |
| 63 } | |
| 64 return &it->second; | |
| 65 } | |
| 66 | |
| 67 // Same as above but returns a pointer to the non-const value. | |
| 68 template <class Collection> | |
| 69 typename Collection::value_type::second_type* | |
| 70 FindOrNull(Collection& collection, // NOLINT | |
| 71 const typename Collection::value_type::first_type& key) { | |
| 72 typename Collection::iterator it = collection.find(key); | |
| 73 if (it == collection.end()) { | |
| 74 return 0; | |
| 75 } | |
| 76 return &it->second; | |
| 77 } | |
| 78 | |
| 79 class NET_EXPORT_PRIVATE QuicUtils { | 16 class NET_EXPORT_PRIVATE QuicUtils { |
| 80 public: | 17 public: |
| 81 enum Priority { | 18 enum Priority { |
| 82 LOCAL_PRIORITY, | 19 LOCAL_PRIORITY, |
| 83 PEER_PRIORITY, | 20 PEER_PRIORITY, |
| 84 }; | 21 }; |
| 85 | 22 |
| 86 // returns the 64 bit FNV1a hash of the data. See | 23 // Returns the 64 bit FNV1a hash of the data. See |
| 87 // http://www.isthe.com/chongo/tech/comp/fnv/index.html#FNV-param | 24 // http://www.isthe.com/chongo/tech/comp/fnv/index.html#FNV-param |
| 88 static uint64 FNV1a_64_Hash(const char* data, int len); | 25 static uint64 FNV1a_64_Hash(const char* data, int len); |
| 89 | 26 |
| 90 // returns the 128 bit FNV1a hash of the data. See | 27 // returns the 128 bit FNV1a hash of the data. See |
| 91 // http://www.isthe.com/chongo/tech/comp/fnv/index.html#FNV-param | 28 // http://www.isthe.com/chongo/tech/comp/fnv/index.html#FNV-param |
| 92 static uint128 FNV1a_128_Hash(const char* data, int len); | 29 static uint128 FNV1a_128_Hash(const char* data, int len); |
| 93 | 30 |
| 94 // FindMutualTag sets |out_result| to the first tag in the priority list that | 31 // FindMutualTag sets |out_result| to the first tag in the priority list that |
| 95 // is also in the other list and returns true. If there is no intersection it | 32 // is also in the other list and returns true. If there is no intersection it |
| 96 // returns false. | 33 // returns false. |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 // Utility function that returns an IOVector object wrapped around |str|. | 84 // Utility function that returns an IOVector object wrapped around |str|. |
| 148 inline IOVector MakeIOVector(base::StringPiece str) { | 85 inline IOVector MakeIOVector(base::StringPiece str) { |
| 149 IOVector iov; | 86 IOVector iov; |
| 150 iov.Append(const_cast<char*>(str.data()), str.size()); | 87 iov.Append(const_cast<char*>(str.data()), str.size()); |
| 151 return iov; | 88 return iov; |
| 152 } | 89 } |
| 153 | 90 |
| 154 } // namespace net | 91 } // namespace net |
| 155 | 92 |
| 156 #endif // NET_QUIC_QUIC_UTILS_H_ | 93 #endif // NET_QUIC_QUIC_UTILS_H_ |
| OLD | NEW |