michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ : michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef mozStorageBindingParamsArray_h michael@0: #define mozStorageBindingParamsArray_h michael@0: michael@0: #include "nsAutoPtr.h" michael@0: #include "nsTArray.h" michael@0: #include "mozilla/Attributes.h" michael@0: michael@0: #include "mozIStorageBindingParamsArray.h" michael@0: michael@0: namespace mozilla { michael@0: namespace storage { michael@0: michael@0: class StorageBaseStatementInternal; michael@0: michael@0: class BindingParamsArray MOZ_FINAL : public mozIStorageBindingParamsArray michael@0: { michael@0: typedef nsTArray< nsCOMPtr > array_type; michael@0: michael@0: public: michael@0: NS_DECL_THREADSAFE_ISUPPORTS michael@0: NS_DECL_MOZISTORAGEBINDINGPARAMSARRAY michael@0: michael@0: BindingParamsArray(StorageBaseStatementInternal *aOwningStatement); michael@0: michael@0: typedef array_type::size_type size_type; michael@0: michael@0: /** michael@0: * Locks the array and prevents further modification to it (such as adding michael@0: * more elements to it). michael@0: */ michael@0: void lock(); michael@0: michael@0: /** michael@0: * @return the pointer to the owning BindingParamsArray. michael@0: */ michael@0: const StorageBaseStatementInternal *getOwner() const; michael@0: michael@0: /** michael@0: * @return the number of elemets the array contains. michael@0: */ michael@0: const size_type length() const { return mArray.Length(); } michael@0: michael@0: class iterator { michael@0: public: michael@0: iterator(BindingParamsArray *aArray, michael@0: uint32_t aIndex) michael@0: : mArray(aArray) michael@0: , mIndex(aIndex) michael@0: { michael@0: } michael@0: michael@0: iterator &operator++(int) michael@0: { michael@0: mIndex++; michael@0: return *this; michael@0: } michael@0: michael@0: bool operator==(const iterator &aOther) const michael@0: { michael@0: return mIndex == aOther.mIndex; michael@0: } michael@0: bool operator!=(const iterator &aOther) const michael@0: { michael@0: return !(*this == aOther); michael@0: } michael@0: mozIStorageBindingParams *operator*() michael@0: { michael@0: NS_ASSERTION(mIndex < mArray->length(), michael@0: "Dereferenceing an invalid value!"); michael@0: return mArray->mArray[mIndex].get(); michael@0: } michael@0: private: michael@0: void operator--() { } michael@0: BindingParamsArray *mArray; michael@0: uint32_t mIndex; michael@0: }; michael@0: michael@0: /** michael@0: * Obtains an iterator pointing to the beginning of the array. michael@0: */ michael@0: inline iterator begin() michael@0: { michael@0: NS_ASSERTION(length() != 0, michael@0: "Obtaining an iterator to the beginning with no elements!"); michael@0: return iterator(this, 0); michael@0: } michael@0: michael@0: /** michael@0: * Obtains an iterator pointing to the end of the array. michael@0: */ michael@0: inline iterator end() michael@0: { michael@0: NS_ASSERTION(mLocked, michael@0: "Obtaining an iterator to the end when we are not locked!"); michael@0: return iterator(this, length()); michael@0: } michael@0: private: michael@0: nsCOMPtr mOwningStatement; michael@0: array_type mArray; michael@0: bool mLocked; michael@0: michael@0: friend class iterator; michael@0: }; michael@0: michael@0: } // namespace storage michael@0: } // namespace mozilla michael@0: michael@0: #endif // mozStorageBindingParamsArray_h