OLD | NEW |
| (Empty) |
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 <stdint.h> | |
11 | |
12 #include "base/compiler_specific.h" | |
13 #include "net/quic/quic_time.h" | |
14 | |
15 namespace net { | |
16 | |
17 typedef uint64_t QuicByteCount; | |
18 typedef uint64_t QuicPacketCount; | |
19 | |
20 class NET_EXPORT_PRIVATE QuicBandwidth { | |
21 public: | |
22 // Creates a new QuicBandwidth with an internal value of 0. | |
23 static QuicBandwidth Zero(); | |
24 | |
25 // Create a new QuicBandwidth holding the bits per second. | |
26 static QuicBandwidth FromBitsPerSecond(int64_t bits_per_second); | |
27 | |
28 // Create a new QuicBandwidth holding the kilo bits per second. | |
29 static QuicBandwidth FromKBitsPerSecond(int64_t k_bits_per_second); | |
30 | |
31 // Create a new QuicBandwidth holding the bytes per second. | |
32 static QuicBandwidth FromBytesPerSecond(int64_t bytes_per_second); | |
33 | |
34 // Create a new QuicBandwidth holding the kilo bytes per second. | |
35 static QuicBandwidth FromKBytesPerSecond(int64_t k_bytes_per_second); | |
36 | |
37 // Create a new QuicBandwidth based on the bytes per the elapsed delta. | |
38 static QuicBandwidth FromBytesAndTimeDelta(QuicByteCount bytes, | |
39 QuicTime::Delta delta); | |
40 | |
41 int64_t ToBitsPerSecond() const; | |
42 | |
43 int64_t ToKBitsPerSecond() const; | |
44 | |
45 int64_t ToBytesPerSecond() const; | |
46 | |
47 int64_t ToKBytesPerSecond() const; | |
48 | |
49 QuicByteCount ToBytesPerPeriod(QuicTime::Delta time_period) const; | |
50 | |
51 int64_t ToKBytesPerPeriod(QuicTime::Delta time_period) const; | |
52 | |
53 bool IsZero() const; | |
54 | |
55 QuicTime::Delta TransferTime(QuicByteCount bytes) const; | |
56 | |
57 private: | |
58 explicit QuicBandwidth(int64_t bits_per_second); | |
59 int64_t bits_per_second_; | |
60 | |
61 friend QuicBandwidth operator+(QuicBandwidth lhs, QuicBandwidth rhs); | |
62 friend QuicBandwidth operator-(QuicBandwidth lhs, QuicBandwidth rhs); | |
63 friend QuicBandwidth operator*(QuicBandwidth lhs, float factor); | |
64 }; | |
65 | |
66 // Non-member relational operators for QuicBandwidth. | |
67 inline bool operator==(QuicBandwidth lhs, QuicBandwidth rhs) { | |
68 return lhs.ToBitsPerSecond() == rhs.ToBitsPerSecond(); | |
69 } | |
70 inline bool operator!=(QuicBandwidth lhs, QuicBandwidth rhs) { | |
71 return !(lhs == rhs); | |
72 } | |
73 inline bool operator<(QuicBandwidth lhs, QuicBandwidth rhs) { | |
74 return lhs.ToBitsPerSecond() < rhs.ToBitsPerSecond(); | |
75 } | |
76 inline bool operator>(QuicBandwidth lhs, QuicBandwidth rhs) { | |
77 return rhs < lhs; | |
78 } | |
79 inline bool operator<=(QuicBandwidth lhs, QuicBandwidth rhs) { | |
80 return !(rhs < lhs); | |
81 } | |
82 inline bool operator>=(QuicBandwidth lhs, QuicBandwidth rhs) { | |
83 return !(lhs < rhs); | |
84 } | |
85 | |
86 // Non-member arithmetic operators for QuicBandwidth. | |
87 inline QuicBandwidth operator+(QuicBandwidth lhs, QuicBandwidth rhs) { | |
88 return QuicBandwidth(lhs.bits_per_second_ + rhs.bits_per_second_); | |
89 } | |
90 inline QuicBandwidth operator-(QuicBandwidth lhs, QuicBandwidth rhs) { | |
91 return QuicBandwidth(lhs.bits_per_second_ - rhs.bits_per_second_); | |
92 } | |
93 inline QuicBandwidth operator*(QuicBandwidth lhs, float rhs) { | |
94 return QuicBandwidth(static_cast<int64_t>(lhs.bits_per_second_ * rhs)); | |
95 } | |
96 inline QuicBandwidth operator*(float lhs, QuicBandwidth rhs) { | |
97 return rhs * lhs; | |
98 } | |
99 | |
100 } // namespace net | |
101 #endif // NET_QUIC_QUIC_BANDWIDTH_H_ | |
OLD | NEW |