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 3 // found in the LICENSE file. 4 // 5 // QuicBandwidth represents a bandwidth, stored in bits per second resolution. 6 7 #ifndef NET_QUIC_QUIC_BANDWIDTH_H_ 8 #define NET_QUIC_QUIC_BANDWIDTH_H_ 9 10 #include "base/basictypes.h" 11 #include "net/quic/quic_time.h" 12 13 namespace net { 14 15 typedef uint64 QuicByteCount; 16 17 class NET_EXPORT_PRIVATE QuicBandwidth { 18 public: 19 // Creates a new QuicBandwidth with an internal value of 0. 20 static QuicBandwidth Zero(); 21 22 // Create a new QuicBandwidth holding the bits per second. 23 static QuicBandwidth FromBitsPerSecond(int64 bits_per_second); 24 25 // Create a new QuicBandwidth holding the kilo bits per second. 26 static QuicBandwidth FromKBitsPerSecond(int64 k_bits_per_second); 27 28 // Create a new QuicBandwidth holding the bytes per second. 29 static QuicBandwidth FromBytesPerSecond(int64 bytes_per_second); 30 31 // Create a new QuicBandwidth holding the kilo bytes per second. 32 static QuicBandwidth FromKBytesPerSecond(int64 k_bytes_per_second); 33 34 // Create a new QuicBandwidth based on the bytes per the elapsed delta. 35 static QuicBandwidth FromBytesAndTimeDelta(QuicByteCount bytes, 36 QuicTime::Delta delta); 37 38 int64 ToBitsPerSecond() const; 39 40 int64 ToKBitsPerSecond() const; 41 42 int64 ToBytesPerSecond() const; 43 44 int64 ToKBytesPerSecond() const; 45 46 QuicByteCount ToBytesPerPeriod(QuicTime::Delta time_period) const; 47 48 int64 ToKBytesPerPeriod(QuicTime::Delta time_period) const; 49 50 bool IsZero() const; 51 52 QuicBandwidth Add(const QuicBandwidth& delta) const; 53 54 QuicBandwidth Subtract(const QuicBandwidth& delta) const; 55 56 QuicBandwidth Scale(float scale_factor) const; 57 58 QuicTime::Delta TransferTime(QuicByteCount bytes) const; 59 60 private: 61 explicit QuicBandwidth(int64 bits_per_second); 62 int64 bits_per_second_; 63 }; 64 65 // Non-member relational operators for QuicBandwidth. 66 inline bool operator==(QuicBandwidth lhs, QuicBandwidth rhs) { 67 return lhs.ToBitsPerSecond() == rhs.ToBitsPerSecond(); 68 } 69 inline bool operator!=(QuicBandwidth lhs, QuicBandwidth rhs) { 70 return !(lhs == rhs); 71 } 72 inline bool operator<(QuicBandwidth lhs, QuicBandwidth rhs) { 73 return lhs.ToBitsPerSecond() < rhs.ToBitsPerSecond(); 74 } 75 inline bool operator>(QuicBandwidth lhs, QuicBandwidth rhs) { 76 return rhs < lhs; 77 } 78 inline bool operator<=(QuicBandwidth lhs, QuicBandwidth rhs) { 79 return !(rhs < lhs); 80 } 81 inline bool operator>=(QuicBandwidth lhs, QuicBandwidth rhs) { 82 return !(lhs < rhs); 83 } 84 85 } // namespace net 86 #endif // NET_QUIC_QUIC_BANDWIDTH_H_ 87