15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved. 25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// found in the LICENSE file. 45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// 55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Some helpers for quic that are for chromium codebase. 65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#ifndef NET_QUIC_QUIC_UTILS_CHROMIUM_H_ 85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#define NET_QUIC_QUIC_UTILS_CHROMIUM_H_ 95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/basictypes.h" 115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/logging.h" 125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace net { 145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// 165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Find*() 175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// 185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Returns a const reference to the value associated with the given key if it 205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// exists. Crashes otherwise. 215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// 225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// This is intended as a replacement for operator[] as an rvalue (for reading) 235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// when the key is guaranteed to exist. 245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// 255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// operator[] for lookup is discouraged for several reasons: 265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// * It has a side-effect of inserting missing keys 275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// * It is not thread-safe (even when it is not inserting, it can still 285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// choose to resize the underlying storage) 295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// * It invalidates iterators (when it chooses to resize) 305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// * It default constructs a value object even if it doesn't need to 315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// 325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// This version assumes the key is printable, and includes it in the fatal log 335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// message. 345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)template <class Collection> 355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)const typename Collection::value_type::second_type& 365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)FindOrDie(const Collection& collection, 375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const typename Collection::value_type::first_type& key) { 385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) typename Collection::const_iterator it = collection.find(key); 395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) CHECK(it != collection.end()) << "Map key not found: " << key; 405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return it->second; 415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Same as above, but returns a non-const reference. 445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)template <class Collection> 455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)typename Collection::value_type::second_type& 465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)FindOrDie(Collection& collection, // NOLINT 475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const typename Collection::value_type::first_type& key) { 485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) typename Collection::iterator it = collection.find(key); 495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) CHECK(it != collection.end()) << "Map key not found: " << key; 505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return it->second; 515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Returns a pointer to the const value associated with the given key if it 545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// exists, or NULL otherwise. 555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)template <class Collection> 565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)const typename Collection::value_type::second_type* 575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)FindOrNull(const Collection& collection, 585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const typename Collection::value_type::first_type& key) { 595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) typename Collection::const_iterator it = collection.find(key); 605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) if (it == collection.end()) { 615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return 0; 625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) } 635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return &it->second; 645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Same as above but returns a pointer to the non-const value. 675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)template <class Collection> 685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)typename Collection::value_type::second_type* 695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)FindOrNull(Collection& collection, // NOLINT 705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const typename Collection::value_type::first_type& key) { 715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) typename Collection::iterator it = collection.find(key); 725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) if (it == collection.end()) { 735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return 0; 745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) } 755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return &it->second; 765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} // namespace net 795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif // NET_QUIC_QUIC_UTILS_CHROMIUM_H_ 81