Bug 1852754: part 9) Add tests for dynamically loading <link rel="prefetch"> elements...
[gecko.git] / xpcom / docs / collections.rst
blob8c9356e499f73a92791e453da9509ff60eb3e04b
1 XPCOM Collections
2 =================
4 ``nsTArray`` and ``AutoTArray``
5 -------------------------------
7 ``nsTArray<T>`` is a typesafe array for holding various objects, similar to ``std::vector<T>``. (note that
8 ``nsTArray<T>`` is dynamically-sized, unlike ``std::array<T>``) Here's an
9 incomplete list of mappings between the two:
11 ================== ==================================================
12 std::vector<T>     nsTArray<T>
13 ================== ==================================================
14 ``size()``         ``Length()``
15 ``empty()``        ``IsEmpty()``
16 ``resize()``       ``SetLength()`` or ``SetLengthAndRetainStorage()``
17 ``capacity()``     ``Capacity()``
18 ``reserve()``      ``SetCapacity()``
19 ``push_back()``    ``AppendElement()``
20 ``insert()``       ``AppendElements()``
21 ``emplace_back()`` ``EmplaceBack()``
22 ``clear()``        ``Clear()`` or ``ClearAndRetainStorage()``
23 ``data()``         ``Elements()``
24 ``at()``           ``ElementAt()``
25 ``back()``         ``LastElement()``
26 ================== ==================================================
28 Rust Bindings
29 ~~~~~~~~~~~~~
31 When the ``thin_vec`` crate is built in Gecko, ``thin_vec::ThinVec<T>`` is
32 guaranteed to have the same memory layout and allocation strategy as
33 ``nsTArray``, meaning that the two types may be used interchangeably across
34 FFI boundaries. The type is **not** safe to pass by-value over FFI
35 boundaries, due to Rust and C++ differing in when they run destructors.
37 The element type ``T`` must be memory-compatible with both Rust and C++ code
38 to use over FFI.
40 ``nsTHashMap`` and ``nsTHashSet``
41 ---------------------------------
43 These types are the recommended interface for writing new XPCOM hashmaps and
44 hashsets in XPCOM code.
46 Supported Hash Keys
47 ~~~~~~~~~~~~~~~~~~~
49 The following types are supported as the key parameter to ``nsTHashMap`` and
50 ``nsTHashSet``.
52 ========================== ======================
53 Type                       Hash Key
54 ========================== ======================
55 ``T*``                     ``nsPtrHashKey<T>``
56 ``T*``                     ``nsPtrHashKey<T>``
57 ``nsCString``              ``nsCStringHashKey``
58 ``nsString``               ``nsStringHashKey``
59 ``uint32_t``               ``nsUint32HashKey``
60 ``uint64_t``               ``nsUint64HashKey``
61 ``intptr_t``               ``IntPtrHashKey``
62 ``nsCOMPtr<nsISupports>``  ``nsISupportsHashKey``
63 ``RefPtr<T>``              ``nsRefPtrHashKey<T>``
64 ``nsID``                   ``nsIDHashKey``
65 ========================== ======================
67 Any key not in this list must inherit from the ``PLDHashEntryHdr`` class to
68 implement manual hashing behaviour.
70 Class Reference
71 ~~~~~~~~~~~~~~~
73 .. note::
75     The ``nsTHashMap`` and ``nsTHashSet`` types are not declared exactly like
76     this in code. This is intended largely as a practical reference.
78 .. cpp:class:: template<K, V> nsTHashMap<K, V>
80     The ``nsTHashMap<K, V>`` class is currently defined as a thin type alias
81     around ``nsBaseHashtable``. See the methods defined on that class for
82     more detailed documentation.
84     https://searchfox.org/mozilla-central/source/xpcom/ds/nsBaseHashtable.h
86     .. cpp:function:: uint32_t Count() const
88     .. cpp:function:: bool IsEmpty() const
90     .. cpp:function:: bool Get(KeyType aKey, V* aData) const
92         Get the value, returning a flag indicating the presence of the entry
93         in the table.
95     .. cpp:function:: V Get(KeyType aKey) const
97         Get the value, returning a default-initialized object if the entry is
98         not present in the table.
100     .. cpp:function:: Maybe<V> MaybeGet(KeyType aKey) const
102         Get the value, returning Nothing if the entry is not present in the table.
104     .. cpp:function:: V& LookupOrInsert(KeyType aKey, Args&&... aArgs) const
106     .. cpp:function:: V& LookupOrInsertWith(KeyType aKey, F&& aFunc) const
108 .. cpp:class:: template<K> nsTHashSet<K>
110     The ``nsTHashSet<K>`` class is currently defined as a thin type alias
111     around ``nsTBaseHashSet``. See the methods defined on that class for
112     more detailed documentation.
114     https://searchfox.org/mozilla-central/source/xpcom/ds/nsTHashSet.h