1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 : * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
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 mozilla.org code.
17 : *
18 : * The Initial Developer of the Original Code is
19 : * Mozilla Corporation.
20 : * Portions created by the Initial Developer are Copyright (C) 2009
21 : * the Initial Developer. All Rights Reserved.
22 : *
23 : * Contributor(s):
24 : * Shawn Wilsher <me@shawnwilsher.com> (Original Author)
25 : *
26 : * Alternatively, the contents of this file may be used under the terms of
27 : * either the GNU General Public License Version 2 or later (the "GPL"), or
28 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 : * in which case the provisions of the GPL or the LGPL are applicable instead
30 : * of those above. If you wish to allow use of your version of this file only
31 : * under the terms of either the GPL or the LGPL, and not to allow others to
32 : * use your version of this file under the terms of the MPL, indicate your
33 : * decision by deleting the provisions above and replace them with the notice
34 : * and other provisions required by the GPL or the LGPL. If you do not delete
35 : * the provisions above, a recipient may use your version of this file under
36 : * the terms of any one of the MPL, the GPL or the LGPL.
37 : *
38 : * ***** END LICENSE BLOCK ***** */
39 :
40 : #ifndef mozStorageBindingParamsArray_h
41 : #define mozStorageBindingParamsArray_h
42 :
43 : #include "nsAutoPtr.h"
44 : #include "nsTArray.h"
45 :
46 : #include "mozIStorageBindingParamsArray.h"
47 :
48 : namespace mozilla {
49 : namespace storage {
50 :
51 : class StorageBaseStatementInternal;
52 :
53 : class BindingParamsArray : public mozIStorageBindingParamsArray
54 121768 : {
55 : typedef nsTArray< nsCOMPtr<mozIStorageBindingParams> > array_type;
56 :
57 : public:
58 : NS_DECL_ISUPPORTS
59 : NS_DECL_MOZISTORAGEBINDINGPARAMSARRAY
60 :
61 : BindingParamsArray(StorageBaseStatementInternal *aOwningStatement);
62 :
63 : typedef array_type::size_type size_type;
64 :
65 : /**
66 : * Locks the array and prevents further modification to it (such as adding
67 : * more elements to it).
68 : */
69 : void lock();
70 :
71 : /**
72 : * @return the pointer to the owning BindingParamsArray.
73 : */
74 : const StorageBaseStatementInternal *getOwner() const;
75 :
76 : /**
77 : * @return the number of elemets the array contains.
78 : */
79 1199360 : const size_type length() const { return mArray.Length(); }
80 :
81 : class iterator {
82 : public:
83 434899 : iterator(BindingParamsArray *aArray,
84 : PRUint32 aIndex)
85 : : mArray(aArray)
86 434899 : , mIndex(aIndex)
87 : {
88 434899 : }
89 :
90 49270 : iterator &operator++(int)
91 : {
92 49270 : mIndex++;
93 49270 : return *this;
94 : }
95 :
96 138584 : bool operator==(const iterator &aOther) const
97 : {
98 138584 : return mIndex == aOther.mIndex;
99 : }
100 98392 : bool operator!=(const iterator &aOther) const
101 : {
102 98392 : return !(*this == aOther);
103 : }
104 385931 : mozIStorageBindingParams *operator*()
105 : {
106 385931 : NS_ASSERTION(mIndex < mArray->length(),
107 : "Dereferenceing an invalid value!");
108 385931 : return mArray->mArray[mIndex].get();
109 : }
110 : private:
111 : void operator--() { }
112 : BindingParamsArray *mArray;
113 : PRUint32 mIndex;
114 : };
115 :
116 : /**
117 : * Obtains an iterator pointing to the beginning of the array.
118 : */
119 385777 : inline iterator begin()
120 : {
121 385777 : NS_ASSERTION(length() != 0,
122 : "Obtaining an iterator to the beginning with no elements!");
123 385777 : return iterator(this, 0);
124 : }
125 :
126 : /**
127 : * Obtains an iterator pointing to the end of the array.
128 : */
129 49122 : inline iterator end()
130 : {
131 49122 : NS_ASSERTION(mLocked,
132 : "Obtaining an iterator to the end when we are not locked!");
133 49122 : return iterator(this, length());
134 : }
135 : private:
136 : nsCOMPtr<StorageBaseStatementInternal> mOwningStatement;
137 : array_type mArray;
138 : bool mLocked;
139 :
140 : friend class iterator;
141 : };
142 :
143 : } // namespace storage
144 : } // namespace mozilla
145 :
146 : #endif // mozStorageBindingParamsArray_h
|