PageRenderTime 23ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/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. #ifndef nsTPtrArray_h__
  39. #define nsTPtrArray_h__
  40. #include "nsTArray.h"
  41. //
  42. // The templatized array class for storing pointers. The class is based on
  43. // nsTArray and has all the features of that class, in addition to an
  44. // implementation of SafeElementAt that returns null for out of bounds access
  45. //
  46. template<class E>
  47. class nsTPtrArray : public nsTArray<E*> {
  48. public:
  49. typedef nsTPtrArray<E> self_type;
  50. typedef nsTArray<E*> base_type;
  51. typedef typename base_type::size_type size_type;
  52. typedef typename base_type::elem_type elem_type;
  53. typedef typename base_type::index_type index_type;
  54. //
  55. // Initialization methods
  56. //
  57. nsTPtrArray() {}
  58. // Initialize this array and pre-allocate some number of elements.
  59. explicit nsTPtrArray(size_type capacity) {
  60. SetCapacity(capacity);
  61. }
  62. // The array's copy-constructor performs a 'deep' copy of the given array.
  63. // @param other The array object to copy.
  64. nsTPtrArray(const self_type& other) {
  65. AppendElements(other);
  66. }
  67. //
  68. // Accessor methods
  69. //
  70. // Forward SafeElementAt to avoid shadowing (and warnings thereof)
  71. elem_type& SafeElementAt(index_type i, elem_type& def) {
  72. return base_type::SafeElementAt(i, def);
  73. }
  74. const elem_type& SafeElementAt(index_type i, const elem_type& def) const {
  75. return base_type::SafeElementAt(i, def);
  76. }
  77. // This method provides direct access to the i'th element of the array in
  78. // a bounds safe manner. If the requested index is out of bounds null is
  79. // returned.
  80. // @param i The index of an element in the array.
  81. elem_type SafeElementAt(index_type i) const {
  82. return SafeElementAt(i, nsnull);
  83. }
  84. };
  85. template<class E, PRUint32 N>
  86. class nsAutoTPtrArray : public nsTPtrArray<E> {
  87. public:
  88. typedef nsTPtrArray<E> base_type;
  89. typedef typename base_type::Header Header;
  90. typedef typename base_type::elem_type elem_type;
  91. nsAutoTPtrArray() {
  92. base_type::mHdr = reinterpret_cast<Header*>(&mAutoBuf);
  93. base_type::mHdr->mLength = 0;
  94. base_type::mHdr->mCapacity = N;
  95. base_type::mHdr->mIsAutoArray = 1;
  96. NS_ASSERTION(base_type::GetAutoArrayBuffer() ==
  97. reinterpret_cast<Header*>(&mAutoBuf),
  98. "GetAutoArrayBuffer needs to be fixed");
  99. }
  100. protected:
  101. char mAutoBuf[sizeof(Header) + N * sizeof(elem_type)];
  102. };
  103. #endif // nsTPtrArray_h__