tor-browser

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

txExpandedNameMap.h (4263B)


      1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* This Source Code Form is subject to the terms of the Mozilla Public
      3 * License, v. 2.0. If a copy of the MPL was not distributed with this
      4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      5 
      6 #ifndef TRANSFRMX_EXPANDEDNAMEMAP_H
      7 #define TRANSFRMX_EXPANDEDNAMEMAP_H
      8 
      9 #include "mozilla/UniquePtr.h"
     10 #include "mozilla/UniquePtrExtensions.h"
     11 #include "nsError.h"
     12 #include "nsTArray.h"
     13 #include "txExpandedName.h"
     14 
     15 class txExpandedNameMap_base {
     16 protected:
     17  /**
     18   * Adds an item, if an item with this key already exists an error is
     19   * returned
     20   * @param  aKey   key for item to add
     21   * @param  aValue value of item to add
     22   * @return errorcode
     23   */
     24  nsresult addItem(const txExpandedName& aKey, void* aValue);
     25 
     26  /**
     27   * Sets an item, if an item with this key already exists it is overwritten
     28   * with the new value
     29   * @param  aKey   key for item to set
     30   * @param  aValue value of item to set
     31   * @return errorcode
     32   */
     33  nsresult setItem(const txExpandedName& aKey, void* aValue, void** aOldValue);
     34 
     35  /**
     36   * Gets an item
     37   * @param  aKey  key for item to get
     38   * @return item with specified key, or null if no such item exists
     39   */
     40  void* getItem(const txExpandedName& aKey) const;
     41 
     42  /**
     43   * Removes an item, deleting it if the map owns the values
     44   * @param  aKey  key for item to remove
     45   * @return item with specified key, or null if it has been deleted
     46   *         or no such item exists
     47   */
     48  void* removeItem(const txExpandedName& aKey);
     49 
     50  /**
     51   * Clears the items
     52   */
     53  void clearItems() { mItems.Clear(); }
     54 
     55  class iterator_base {
     56   public:
     57    explicit iterator_base(txExpandedNameMap_base& aMap)
     58        : mMap(aMap), mCurrentPos(uint32_t(-1)) {}
     59 
     60    bool next() { return ++mCurrentPos < mMap.mItems.Length(); }
     61 
     62    const txExpandedName key() {
     63      NS_ASSERTION(mCurrentPos < mMap.mItems.Length(),
     64                   "invalid position in txExpandedNameMap::iterator");
     65      return txExpandedName(mMap.mItems[mCurrentPos].mNamespaceID,
     66                            mMap.mItems[mCurrentPos].mLocalName);
     67    }
     68 
     69   protected:
     70    void* itemValue() {
     71      NS_ASSERTION(mCurrentPos < mMap.mItems.Length(),
     72                   "invalid position in txExpandedNameMap::iterator");
     73      return mMap.mItems[mCurrentPos].mValue;
     74    }
     75 
     76   private:
     77    txExpandedNameMap_base& mMap;
     78    uint32_t mCurrentPos;
     79  };
     80 
     81  friend class iterator_base;
     82 
     83  friend class txMapItemComparator;
     84  struct MapItem {
     85    int32_t mNamespaceID;
     86    RefPtr<nsAtom> mLocalName;
     87    void* mValue;
     88  };
     89 
     90  nsTArray<MapItem> mItems;
     91 };
     92 
     93 template <class E>
     94 class txExpandedNameMap : public txExpandedNameMap_base {
     95 public:
     96  nsresult add(const txExpandedName& aKey, E* aValue) {
     97    return addItem(aKey, (void*)aValue);
     98  }
     99 
    100  nsresult set(const txExpandedName& aKey, E* aValue) {
    101    void* oldValue;
    102    return setItem(aKey, (void*)aValue, &oldValue);
    103  }
    104 
    105  E* get(const txExpandedName& aKey) const { return (E*)getItem(aKey); }
    106 
    107  E* remove(const txExpandedName& aKey) { return (E*)removeItem(aKey); }
    108 
    109  void clear() { clearItems(); }
    110 
    111  class iterator : public iterator_base {
    112   public:
    113    explicit iterator(txExpandedNameMap& aMap) : iterator_base(aMap) {}
    114 
    115    E* value() { return (E*)itemValue(); }
    116  };
    117 };
    118 
    119 template <class E>
    120 class txOwningExpandedNameMap : public txExpandedNameMap_base {
    121 public:
    122  ~txOwningExpandedNameMap() { clear(); }
    123 
    124  nsresult add(const txExpandedName& aKey, E* aValue) {
    125    return addItem(aKey, (void*)aValue);
    126  }
    127 
    128  nsresult set(const txExpandedName& aKey, E* aValue) {
    129    mozilla::UniquePtr<E> oldValue;
    130    return setItem(aKey, (void*)aValue, mozilla::getter_Transfers(oldValue));
    131  }
    132 
    133  E* get(const txExpandedName& aKey) const { return (E*)getItem(aKey); }
    134 
    135  void remove(const txExpandedName& aKey) { delete (E*)removeItem(aKey); }
    136 
    137  void clear() {
    138    uint32_t i, len = mItems.Length();
    139    for (i = 0; i < len; ++i) {
    140      delete (E*)mItems[i].mValue;
    141    }
    142    clearItems();
    143  }
    144 
    145  class iterator : public iterator_base {
    146   public:
    147    explicit iterator(txOwningExpandedNameMap& aMap) : iterator_base(aMap) {}
    148 
    149    E* value() { return (E*)itemValue(); }
    150  };
    151 };
    152 
    153 #endif  // TRANSFRMX_EXPANDEDNAMEMAP_H