Home | History | Annotate | Download | only in quic
      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 
     19  public:
     20   // Creates a new QuicBandwidth with an internal value of 0.
     21   static QuicBandwidth Zero();
     22 
     23   // Create a new QuicBandwidth holding the bits per second.
     24   static QuicBandwidth FromBitsPerSecond(int64 bits_per_second);
     25 
     26   // Create a new QuicBandwidth holding the kilo bits per second.
     27   static QuicBandwidth FromKBitsPerSecond(int64 k_bits_per_second);
     28 
     29   // Create a new QuicBandwidth holding the bytes per second.
     30   static QuicBandwidth FromBytesPerSecond(int64 bytes_per_second);
     31 
     32   // Create a new QuicBandwidth holding the kilo bytes per second.
     33   static QuicBandwidth FromKBytesPerSecond(int64 k_bytes_per_second);
     34 
     35   // Create a new QuicBandwidth based on the bytes per the elapsed delta.
     36   static QuicBandwidth FromBytesAndTimeDelta(QuicByteCount bytes,
     37                                              QuicTime::Delta delta);
     38 
     39   int64 ToBitsPerSecond() const;
     40 
     41   int64 ToKBitsPerSecond() const;
     42 
     43   int64 ToBytesPerSecond() const;
     44 
     45   int64 ToKBytesPerSecond() const;
     46 
     47   QuicByteCount ToBytesPerPeriod(QuicTime::Delta time_period) const;
     48 
     49   int64 ToKBytesPerPeriod(QuicTime::Delta time_period) const;
     50 
     51   bool IsZero() const;
     52 
     53   QuicBandwidth Add(const QuicBandwidth& delta) const;
     54 
     55   QuicBandwidth Subtract(const QuicBandwidth& delta) const;
     56 
     57   QuicBandwidth Scale(float scale_factor) const;
     58 
     59  private:
     60   explicit QuicBandwidth(int64 bits_per_second);
     61   int64 bits_per_second_;
     62 };
     63 
     64 // Non-member relational operators for QuicBandwidth.
     65 inline bool operator==(QuicBandwidth lhs, QuicBandwidth rhs) {
     66   return lhs.ToBitsPerSecond() == rhs.ToBitsPerSecond();
     67 }
     68 inline bool operator!=(QuicBandwidth lhs, QuicBandwidth rhs) {
     69   return !(lhs == rhs);
     70 }
     71 inline bool operator<(QuicBandwidth lhs, QuicBandwidth rhs) {
     72   return lhs.ToBitsPerSecond() < rhs.ToBitsPerSecond();
     73 }
     74 inline bool operator>(QuicBandwidth lhs, QuicBandwidth rhs) {
     75   return rhs < lhs;
     76 }
     77 inline bool operator<=(QuicBandwidth lhs, QuicBandwidth rhs) {
     78   return !(rhs < lhs);
     79 }
     80 inline bool operator>=(QuicBandwidth lhs, QuicBandwidth rhs) {
     81   return !(lhs < rhs);
     82 }
     83 
     84 }  // namespace net
     85 #endif  // NET_QUIC_QUIC_BANDWIDTH_H_
     86