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