Line data Source code
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 final : public mozIStorageBindingParamsArray
22 : {
23 : typedef nsTArray< nsCOMPtr<mozIStorageBindingParams> > array_type;
24 :
25 19 : ~BindingParamsArray() {}
26 :
27 : public:
28 : NS_DECL_THREADSAFE_ISUPPORTS
29 : NS_DECL_MOZISTORAGEBINDINGPARAMSARRAY
30 :
31 : explicit BindingParamsArray(StorageBaseStatementInternal *aOwningStatement);
32 :
33 : typedef array_type::size_type size_type;
34 :
35 : /**
36 : * Locks the array and prevents further modification to it (such as adding
37 : * more elements to it).
38 : */
39 : void lock();
40 :
41 : /**
42 : * @return the pointer to the owning BindingParamsArray.
43 : */
44 : const StorageBaseStatementInternal *getOwner() const;
45 :
46 : /**
47 : * @return the number of elemets the array contains.
48 : */
49 174 : size_type length() const { return mArray.Length(); }
50 :
51 : class iterator {
52 : public:
53 64 : iterator(BindingParamsArray *aArray,
54 : uint32_t aIndex)
55 64 : : mArray(aArray)
56 64 : , mIndex(aIndex)
57 : {
58 64 : }
59 :
60 6 : iterator &operator++(int)
61 : {
62 6 : mIndex++;
63 6 : return *this;
64 : }
65 :
66 18 : bool operator==(const iterator &aOther) const
67 : {
68 18 : return mIndex == aOther.mIndex;
69 : }
70 12 : bool operator!=(const iterator &aOther) const
71 : {
72 12 : return !(*this == aOther);
73 : }
74 58 : mozIStorageBindingParams *operator*()
75 : {
76 58 : NS_ASSERTION(mIndex < mArray->length(),
77 : "Dereferenceing an invalid value!");
78 58 : return mArray->mArray[mIndex].get();
79 : }
80 : private:
81 : void operator--() { }
82 : BindingParamsArray *mArray;
83 : uint32_t mIndex;
84 : };
85 :
86 : /**
87 : * Obtains an iterator pointing to the beginning of the array.
88 : */
89 58 : inline iterator begin()
90 : {
91 58 : NS_ASSERTION(length() != 0,
92 : "Obtaining an iterator to the beginning with no elements!");
93 58 : return iterator(this, 0);
94 : }
95 :
96 : /**
97 : * Obtains an iterator pointing to the end of the array.
98 : */
99 6 : inline iterator end()
100 : {
101 6 : NS_ASSERTION(mLocked,
102 : "Obtaining an iterator to the end when we are not locked!");
103 6 : return iterator(this, length());
104 : }
105 : private:
106 : nsCOMPtr<StorageBaseStatementInternal> mOwningStatement;
107 : array_type mArray;
108 : bool mLocked;
109 :
110 : friend class iterator;
111 : };
112 :
113 : } // namespace storage
114 : } // namespace mozilla
115 :
116 : #endif // mozStorageBindingParamsArray_h
|