|
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 * This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 #ifndef mozStorageBindingParamsArray_h |
|
8 #define mozStorageBindingParamsArray_h |
|
9 |
|
10 #include "nsAutoPtr.h" |
|
11 #include "nsTArray.h" |
|
12 #include "mozilla/Attributes.h" |
|
13 |
|
14 #include "mozIStorageBindingParamsArray.h" |
|
15 |
|
16 namespace mozilla { |
|
17 namespace storage { |
|
18 |
|
19 class StorageBaseStatementInternal; |
|
20 |
|
21 class BindingParamsArray MOZ_FINAL : public mozIStorageBindingParamsArray |
|
22 { |
|
23 typedef nsTArray< nsCOMPtr<mozIStorageBindingParams> > array_type; |
|
24 |
|
25 public: |
|
26 NS_DECL_THREADSAFE_ISUPPORTS |
|
27 NS_DECL_MOZISTORAGEBINDINGPARAMSARRAY |
|
28 |
|
29 BindingParamsArray(StorageBaseStatementInternal *aOwningStatement); |
|
30 |
|
31 typedef array_type::size_type size_type; |
|
32 |
|
33 /** |
|
34 * Locks the array and prevents further modification to it (such as adding |
|
35 * more elements to it). |
|
36 */ |
|
37 void lock(); |
|
38 |
|
39 /** |
|
40 * @return the pointer to the owning BindingParamsArray. |
|
41 */ |
|
42 const StorageBaseStatementInternal *getOwner() const; |
|
43 |
|
44 /** |
|
45 * @return the number of elemets the array contains. |
|
46 */ |
|
47 const size_type length() const { return mArray.Length(); } |
|
48 |
|
49 class iterator { |
|
50 public: |
|
51 iterator(BindingParamsArray *aArray, |
|
52 uint32_t aIndex) |
|
53 : mArray(aArray) |
|
54 , mIndex(aIndex) |
|
55 { |
|
56 } |
|
57 |
|
58 iterator &operator++(int) |
|
59 { |
|
60 mIndex++; |
|
61 return *this; |
|
62 } |
|
63 |
|
64 bool operator==(const iterator &aOther) const |
|
65 { |
|
66 return mIndex == aOther.mIndex; |
|
67 } |
|
68 bool operator!=(const iterator &aOther) const |
|
69 { |
|
70 return !(*this == aOther); |
|
71 } |
|
72 mozIStorageBindingParams *operator*() |
|
73 { |
|
74 NS_ASSERTION(mIndex < mArray->length(), |
|
75 "Dereferenceing an invalid value!"); |
|
76 return mArray->mArray[mIndex].get(); |
|
77 } |
|
78 private: |
|
79 void operator--() { } |
|
80 BindingParamsArray *mArray; |
|
81 uint32_t mIndex; |
|
82 }; |
|
83 |
|
84 /** |
|
85 * Obtains an iterator pointing to the beginning of the array. |
|
86 */ |
|
87 inline iterator begin() |
|
88 { |
|
89 NS_ASSERTION(length() != 0, |
|
90 "Obtaining an iterator to the beginning with no elements!"); |
|
91 return iterator(this, 0); |
|
92 } |
|
93 |
|
94 /** |
|
95 * Obtains an iterator pointing to the end of the array. |
|
96 */ |
|
97 inline iterator end() |
|
98 { |
|
99 NS_ASSERTION(mLocked, |
|
100 "Obtaining an iterator to the end when we are not locked!"); |
|
101 return iterator(this, length()); |
|
102 } |
|
103 private: |
|
104 nsCOMPtr<StorageBaseStatementInternal> mOwningStatement; |
|
105 array_type mArray; |
|
106 bool mLocked; |
|
107 |
|
108 friend class iterator; |
|
109 }; |
|
110 |
|
111 } // namespace storage |
|
112 } // namespace mozilla |
|
113 |
|
114 #endif // mozStorageBindingParamsArray_h |