tor-browser

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

SVGTransformList.h (4449B)


      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_SVG_SVGTRANSFORMLIST_H_
      8 #define DOM_SVG_SVGTRANSFORMLIST_H_
      9 
     10 #include "gfxMatrix.h"
     11 #include "mozilla/dom/SVGTransform.h"
     12 #include "nsTArray.h"
     13 
     14 namespace mozilla {
     15 
     16 namespace dom {
     17 class DOMSVGTransform;
     18 class DOMSVGTransformList;
     19 }  // namespace dom
     20 
     21 /**
     22 * ATTENTION! WARNING! WATCH OUT!!
     23 *
     24 * Consumers that modify objects of this type absolutely MUST keep the DOM
     25 * wrappers for those lists (if any) in sync!! That's why this class is so
     26 * locked down.
     27 *
     28 * The DOM wrapper class for this class is DOMSVGTransformList.
     29 */
     30 class SVGTransformList {
     31  friend class SVGAnimatedTransformList;
     32  friend class dom::DOMSVGTransform;
     33  friend class dom::DOMSVGTransformList;
     34 
     35 public:
     36  SVGTransformList() = default;
     37  ~SVGTransformList() = default;
     38 
     39  SVGTransformList& operator=(const SVGTransformList& aOther) {
     40    mItems.ClearAndRetainStorage();
     41    // Best-effort, really.
     42    (void)mItems.AppendElements(aOther.mItems, fallible);
     43    return *this;
     44  }
     45 
     46  SVGTransformList(const SVGTransformList& aOther) { *this = aOther; }
     47 
     48  // Only methods that don't make/permit modification to this list are public.
     49  // Only our friend classes can access methods that may change us.
     50 
     51  /// This may return an incomplete string on OOM, but that's acceptable.
     52  void GetValueAsString(nsAString& aValue) const;
     53 
     54  bool IsEmpty() const { return mItems.IsEmpty(); }
     55 
     56  uint32_t Length() const { return mItems.Length(); }
     57 
     58  const SVGTransform& operator[](uint32_t aIndex) const {
     59    return mItems[aIndex];
     60  }
     61 
     62  bool operator==(const SVGTransformList& rhs) const {
     63    return mItems == rhs.mItems;
     64  }
     65 
     66  bool SetCapacity(uint32_t size) { return mItems.SetCapacity(size, fallible); }
     67 
     68  void Compact() { mItems.Compact(); }
     69 
     70  gfxMatrix GetConsolidationMatrix() const;
     71 
     72  // Access to methods that can modify objects of this type is deliberately
     73  // limited. This is to reduce the chances of someone modifying objects of
     74  // this type without taking the necessary steps to keep DOM wrappers in sync.
     75  // If you need wider access to these methods, consider adding a method to
     76  // DOMSVGAnimatedTransformList and having that class act as an intermediary so
     77  // it can take care of keeping DOM wrappers in sync.
     78 
     79 protected:
     80  /**
     81   * These may fail on OOM if the internal capacity needs to be increased, in
     82   * which case the list will be left unmodified.
     83   */
     84  nsresult CopyFrom(const SVGTransformList& rhs);
     85  nsresult CopyFrom(const nsTArray<SVGTransform>& aTransformArray);
     86 
     87  SVGTransform& operator[](uint32_t aIndex) { return mItems[aIndex]; }
     88 
     89  /**
     90   * This may fail (return false) on OOM if the internal capacity is being
     91   * increased, in which case the list will be left unmodified.
     92   */
     93  bool SetLength(uint32_t aNumberOfItems) {
     94    return mItems.SetLength(aNumberOfItems, fallible);
     95  }
     96 
     97 private:
     98  // Marking the following private only serves to show which methods are only
     99  // used by our friend classes (as opposed to our subclasses) - it doesn't
    100  // really provide additional safety.
    101 
    102  nsresult SetValueFromString(const nsAString& aValue);
    103 
    104  void Clear() { mItems.Clear(); }
    105 
    106  bool InsertItem(uint32_t aIndex, const SVGTransform& aTransform) {
    107    if (aIndex >= mItems.Length()) {
    108      aIndex = mItems.Length();
    109    }
    110    return !!mItems.InsertElementAt(aIndex, aTransform, fallible);
    111  }
    112 
    113  void ReplaceItem(uint32_t aIndex, const SVGTransform& aTransform) {
    114    MOZ_ASSERT(aIndex < mItems.Length(),
    115               "DOM wrapper caller should have raised INDEX_SIZE_ERR");
    116    mItems[aIndex] = aTransform;
    117  }
    118 
    119  void RemoveItem(uint32_t aIndex) {
    120    MOZ_ASSERT(aIndex < mItems.Length(),
    121               "DOM wrapper caller should have raised INDEX_SIZE_ERR");
    122    mItems.RemoveElementAt(aIndex);
    123  }
    124 
    125  bool AppendItem(const SVGTransform& aTransform) {
    126    return !!mItems.AppendElement(aTransform, fallible);
    127  }
    128 
    129 protected:
    130  /*
    131   * See SVGLengthList for the rationale for using
    132   * FallibleTArray<SVGTransform> instead of FallibleTArray<SVGTransform,
    133   * 1>.
    134   */
    135  FallibleTArray<SVGTransform> mItems;
    136 };
    137 
    138 }  // namespace mozilla
    139 
    140 #endif  // DOM_SVG_SVGTRANSFORMLIST_H_