tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

RecentEventsBuffer.h (2894B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #ifndef mozilla_layers_RecentEventsBuffer_h
      8 #define mozilla_layers_RecentEventsBuffer_h
      9 
     10 #include <deque>
     11 
     12 #include "mozilla/TimeStamp.h"
     13 
     14 namespace mozilla {
     15 namespace layers {
     16 /**
     17 * RecentEventsBuffer: maintains an age constrained buffer of events
     18 *
     19 * Intended for use with elements of type InputData, but the only requirement
     20 * is a member "mTimeStamp" of type TimeStamp
     21 */
     22 template <typename Event>
     23 class RecentEventsBuffer {
     24 public:
     25  explicit RecentEventsBuffer(TimeDuration maxAge);
     26 
     27  explicit RecentEventsBuffer(TimeDuration maxAge, size_t minSize);
     28 
     29  void push(Event event);
     30  void clear();
     31 
     32  typedef typename std::deque<Event>::size_type size_type;
     33  size_type size() { return mBuffer.size(); }
     34 
     35  // Delegate to container for iterators
     36  typedef typename std::deque<Event>::iterator iterator;
     37  typedef typename std::deque<Event>::const_iterator const_iterator;
     38  iterator begin() { return mBuffer.begin(); }
     39  iterator end() { return mBuffer.end(); }
     40  const_iterator cbegin() const { return mBuffer.cbegin(); }
     41  const_iterator cend() const { return mBuffer.cend(); }
     42 
     43  // Also delegate for front/back
     44  typedef typename std::deque<Event>::reference reference;
     45  typedef typename std::deque<Event>::const_reference const_reference;
     46  reference front() { return mBuffer.front(); }
     47  reference back() { return mBuffer.back(); }
     48  const_reference front() const { return mBuffer.front(); }
     49  const_reference back() const { return mBuffer.back(); }
     50 
     51 private:
     52  size_t mMinSize;
     53  TimeDuration mMaxAge;
     54  std::deque<Event> mBuffer;
     55 };
     56 
     57 template <typename Event>
     58 RecentEventsBuffer<Event>::RecentEventsBuffer(TimeDuration maxAge)
     59    : mMinSize(0), mMaxAge(maxAge), mBuffer() {}
     60 
     61 template <typename Event>
     62 RecentEventsBuffer<Event>::RecentEventsBuffer(TimeDuration maxAge,
     63                                              size_t minSize)
     64    : mMinSize(minSize), mMaxAge(maxAge), mBuffer() {}
     65 
     66 template <typename Event>
     67 void RecentEventsBuffer<Event>::push(Event event) {
     68  // Events must be pushed in chronological order
     69  MOZ_ASSERT(mBuffer.empty() || mBuffer.back().mTimeStamp <= event.mTimeStamp);
     70 
     71  mBuffer.push_back(event);
     72 
     73  // Flush all events older than the given lifetime
     74  TimeStamp bound = event.mTimeStamp - mMaxAge;
     75  while (mBuffer.size() > mMinSize) {
     76    if (mBuffer.front().mTimeStamp >= bound) {
     77      break;
     78    }
     79    mBuffer.pop_front();
     80  }
     81 }
     82 
     83 template <typename Event>
     84 void RecentEventsBuffer<Event>::clear() {
     85  mBuffer.clear();
     86 }
     87 
     88 }  // namespace layers
     89 }  // namespace mozilla
     90 
     91 #endif  // mozilla_layers_RecentEventsBuffer_h