PageRenderTime 223ms CodeModel.GetById 141ms app.highlight 6ms RepoModel.GetById 74ms app.codeStats 1ms

/gecko_api/include/nsTPtrArray.h

http://firefox-mac-pdf.googlecode.com/
C++ Header | 119 lines | 47 code | 16 blank | 56 comment | 0 complexity | fa0949941935b387b2984a5fc28d865f MD5 | raw file
  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
  5 *
  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/
 10 *
 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
 14 * License.
 15 *
 16 * The Original Code is C++ pointer array template.
 17 *
 18 * The Initial Developer of the Original Code is Mozilla Corporation.
 19 * Portions created by the Initial Developer are Copyright (C) 2006
 20 * the Initial Developer. All Rights Reserved.
 21 *
 22 * Contributor(s):
 23 *  Jonas Sicking <jonas@sicking.cc>
 24 *
 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.
 36 *
 37 * ***** END LICENSE BLOCK ***** */
 38
 39
 40#ifndef nsTPtrArray_h__
 41#define nsTPtrArray_h__
 42
 43#include "nsTArray.h"
 44
 45//
 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
 49//
 50template<class E>
 51class nsTPtrArray : public nsTArray<E*> {
 52  public:
 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;
 58
 59    //
 60    // Initialization methods
 61    //
 62
 63    nsTPtrArray() {}
 64
 65    // Initialize this array and pre-allocate some number of elements.
 66    explicit nsTPtrArray(size_type capacity) {
 67      SetCapacity(capacity);
 68    }
 69    
 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      AppendElements(other);
 74    }
 75
 76    //
 77    // Accessor methods
 78    //
 79
 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);
 83    }
 84    const elem_type& SafeElementAt(index_type i, const elem_type& def) const {
 85      return base_type::SafeElementAt(i, def);
 86    }
 87
 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
 90    // returned.
 91    // @param i  The index of an element in the array.
 92    elem_type SafeElementAt(index_type i) const {
 93      return SafeElementAt(i, nsnull);
 94    }
 95};
 96
 97template<class E, PRUint32 N>
 98class nsAutoTPtrArray : public nsTPtrArray<E> {
 99  public:
100    typedef nsTPtrArray<E> base_type;
101    typedef typename base_type::Header Header;
102    typedef typename base_type::elem_type elem_type;
103
104    nsAutoTPtrArray() {
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;
109
110      NS_ASSERTION(base_type::GetAutoArrayBuffer() ==
111                   reinterpret_cast<Header*>(&mAutoBuf),
112                   "GetAutoArrayBuffer needs to be fixed");
113    }
114
115  protected:
116    char mAutoBuf[sizeof(Header) + N * sizeof(elem_type)];
117};
118
119#endif  // nsTPtrArray_h__