tor-browser

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

SMILTimeValue.h (4704B)


      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 DOM_SMIL_SMILTIMEVALUE_H_
      8 #define DOM_SMIL_SMILTIMEVALUE_H_
      9 
     10 #include "mozilla/SMILTypes.h"
     11 #include "nsDebug.h"
     12 
     13 namespace mozilla {
     14 
     15 /*----------------------------------------------------------------------
     16 * SMILTimeValue class
     17 *
     18 * A tri-state time value.
     19 *
     20 * First a quick overview of the SMIL time data types:
     21 *
     22 * SMILTime          -- a timestamp in milliseconds.
     23 * SMILTimeValue     -- (this class) a timestamp that can take the additional
     24 *                      states 'indefinite' and 'unresolved'
     25 * SMILInstanceTime  -- an SMILTimeValue used for constructing intervals. It
     26 *                      contains additional fields to govern reset behavior
     27 *                      and track timing dependencies (e.g. syncbase timing).
     28 * SMILInterval      -- a pair of SMILInstanceTimes that defines a begin and
     29 *                      an end time for animation.
     30 * SMILTimeValueSpec -- a component of a begin or end attribute, such as the
     31 *                      '5s' or 'a.end+2m' in begin="5s; a.end+2m". Acts as
     32 *                      a broker between an SMILTimedElement and its
     33 *                      SMILInstanceTimes by generating new instance times
     34 *                      and handling changes to existing times.
     35 *
     36 * Objects of this class may be in one of three states:
     37 *
     38 * 1) The time is resolved and has a definite millisecond value
     39 * 2) The time is resolved and indefinite
     40 * 3) The time is unresolved
     41 *
     42 * In summary:
     43 *
     44 * State      | GetMillis     | IsDefinite | IsIndefinite | IsResolved
     45 * -----------+---------------+------------+--------------+------------
     46 * Definite   | SMILTimeValue | true       | false        | true
     47 * -----------+---------------+------------+--------------+------------
     48 * Indefinite | --            | false      | true         | true
     49 * -----------+---------------+------------+--------------+------------
     50 * Unresolved | --            | false      | false        | false
     51 *
     52 */
     53 
     54 class SMILTimeValue {
     55 public:
     56  // Creates an unresolved time value
     57  SMILTimeValue()
     58      : mMilliseconds(kUnresolvedMillis), mState(STATE_UNRESOLVED) {}
     59 
     60  // Creates a resolved time value
     61  explicit SMILTimeValue(SMILTime aMillis)
     62      : mMilliseconds(aMillis), mState(STATE_DEFINITE) {}
     63 
     64  // Named constructor to create an indefinite time value
     65  static SMILTimeValue Indefinite() {
     66    SMILTimeValue value;
     67    value.SetIndefinite();
     68    return value;
     69  }
     70 
     71  static SMILTimeValue Zero() { return SMILTimeValue(SMILTime(0L)); }
     72 
     73  bool IsIndefinite() const { return mState == STATE_INDEFINITE; }
     74  void SetIndefinite() {
     75    mState = STATE_INDEFINITE;
     76    mMilliseconds = kUnresolvedMillis;
     77  }
     78 
     79  bool IsResolved() const { return mState != STATE_UNRESOLVED; }
     80  void SetUnresolved() {
     81    mState = STATE_UNRESOLVED;
     82    mMilliseconds = kUnresolvedMillis;
     83  }
     84 
     85  bool IsDefinite() const { return mState == STATE_DEFINITE; }
     86  SMILTime GetMillis() const {
     87    MOZ_ASSERT(mState == STATE_DEFINITE,
     88               "GetMillis() called for unresolved or indefinite time");
     89 
     90    return mState == STATE_DEFINITE ? mMilliseconds : kUnresolvedMillis;
     91  }
     92 
     93  bool IsZero() const {
     94    return mState == STATE_DEFINITE ? mMilliseconds == 0 : false;
     95  }
     96 
     97  void SetMillis(SMILTime aMillis) {
     98    mState = STATE_DEFINITE;
     99    mMilliseconds = aMillis;
    100  }
    101 
    102  /*
    103   * EnsureNonZero ensures values such as 0.0001s are not represented as 0
    104   * for values where 0 is invalid.
    105   */
    106  enum class Rounding : uint8_t { EnsureNonZero, Nearest };
    107 
    108  void SetMillis(double aMillis, Rounding aRounding);
    109 
    110  int8_t CompareTo(const SMILTimeValue& aOther) const;
    111 
    112  bool operator==(const SMILTimeValue& aOther) const {
    113    return CompareTo(aOther) == 0;
    114  }
    115 
    116  bool operator!=(const SMILTimeValue& aOther) const {
    117    return CompareTo(aOther) != 0;
    118  }
    119 
    120  bool operator<(const SMILTimeValue& aOther) const {
    121    return CompareTo(aOther) < 0;
    122  }
    123 
    124  bool operator>(const SMILTimeValue& aOther) const {
    125    return CompareTo(aOther) > 0;
    126  }
    127 
    128  bool operator<=(const SMILTimeValue& aOther) const {
    129    return CompareTo(aOther) <= 0;
    130  }
    131 
    132  bool operator>=(const SMILTimeValue& aOther) const {
    133    return CompareTo(aOther) >= 0;
    134  }
    135 
    136 private:
    137  static const SMILTime kUnresolvedMillis;
    138 
    139  SMILTime mMilliseconds;
    140  enum { STATE_DEFINITE, STATE_INDEFINITE, STATE_UNRESOLVED } mState;
    141 };
    142 
    143 }  // namespace mozilla
    144 
    145 #endif  // DOM_SMIL_SMILTIMEVALUE_H_