1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 /* ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
16 * The Original Code is C++ pointer array template.
18 * The Initial Developer of the Original Code is Mozilla Foundation.
19 * Portions created by the Initial Developer are Copyright (C) 2006
20 * the Initial Developer. All Rights Reserved.
23 * Jonas Sicking <jonas@sicking.cc>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
40 #ifndef nsTPtrArray_h__
41 #define nsTPtrArray_h__
46 // The templatized array class for storing pointers. The class is based on
47 // nsTArray and has all the features of that class, in addition to an
48 // implementation of SafeElementAt that returns null for out of bounds access
51 class nsTPtrArray
: public nsTArray
<E
*> {
53 typedef nsTPtrArray
<E
> self_type
;
54 typedef nsTArray
<E
*> base_type
;
55 typedef typename
base_type::size_type size_type
;
56 typedef typename
base_type::elem_type elem_type
;
57 typedef typename
base_type::index_type index_type
;
60 // Initialization methods
65 // Initialize this array and pre-allocate some number of elements.
66 explicit nsTPtrArray(size_type capacity
) {
67 this->SetCapacity(capacity
);
70 // The array's copy-constructor performs a 'deep' copy of the given array.
71 // @param other The array object to copy.
72 nsTPtrArray(const self_type
& other
) {
73 this->AppendElements(other
);
80 // Forward SafeElementAt to avoid shadowing (and warnings thereof)
81 elem_type
& SafeElementAt(index_type i
, elem_type
& def
) {
82 return base_type::SafeElementAt(i
, def
);
84 const elem_type
& SafeElementAt(index_type i
, const elem_type
& def
) const {
85 return base_type::SafeElementAt(i
, def
);
88 // This method provides direct access to the i'th element of the array in
89 // a bounds safe manner. If the requested index is out of bounds null is
91 // @param i The index of an element in the array.
92 elem_type
SafeElementAt(index_type i
) const {
93 return SafeElementAt(i
, nsnull
);
97 template<class E
, PRUint32 N
>
98 class nsAutoTPtrArray
: public nsTPtrArray
<E
> {
100 typedef nsTPtrArray
<E
> base_type
;
101 typedef typename
base_type::Header Header
;
102 typedef typename
base_type::elem_type elem_type
;
105 base_type::mHdr
= reinterpret_cast<Header
*>(&mAutoBuf
);
106 base_type::mHdr
->mLength
= 0;
107 base_type::mHdr
->mCapacity
= N
;
108 base_type::mHdr
->mIsAutoArray
= 1;
110 NS_ASSERTION(base_type::GetAutoArrayBuffer() ==
111 reinterpret_cast<Header
*>(&mAutoBuf
),
112 "GetAutoArrayBuffer needs to be fixed");
117 char mAutoBuf
[sizeof(Header
) + N
* sizeof(elem_type
)];
122 #endif // nsTPtrArray_h__