dom/quota/ArrayCluster.h

branch
TOR_BUG_3246
changeset 7
129ffea94266
equal deleted inserted replaced
-1:000000000000 0:2f8b7ce993bd
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=2 et sw=2 tw=80: */
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 file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #ifndef mozilla_dom_quota_arraycluster_h__
8 #define mozilla_dom_quota_arraycluster_h__
9
10 #include "mozilla/dom/quota/QuotaCommon.h"
11
12 #include "Client.h"
13
14 BEGIN_QUOTA_NAMESPACE
15
16 template <class ValueType, uint32_t Length = Client::TYPE_MAX>
17 class ArrayCluster
18 {
19 public:
20 ArrayCluster()
21 {
22 mArrays.AppendElements(Length);
23 }
24
25 nsTArray<ValueType>&
26 ArrayAt(uint32_t aIndex)
27 {
28 MOZ_ASSERT(aIndex < Length, "Bad index!");
29 return mArrays[aIndex];
30 }
31
32 nsTArray<ValueType>&
33 operator[](uint32_t aIndex)
34 {
35 return ArrayAt(aIndex);
36 }
37
38 bool
39 IsEmpty()
40 {
41 for (uint32_t index = 0; index < Length; index++) {
42 if (!mArrays[index].IsEmpty()) {
43 return false;
44 }
45 }
46 return true;
47 }
48
49 template <class T>
50 void
51 AppendElementsTo(uint32_t aIndex, nsTArray<T>& aArray)
52 {
53 NS_ASSERTION(aIndex < Length, "Bad index!");
54 aArray.AppendElements(mArrays[aIndex]);
55 }
56
57 template <class T>
58 void
59 AppendElementsTo(uint32_t aIndex, ArrayCluster<T, Length>& aArrayCluster)
60 {
61 NS_ASSERTION(aIndex < Length, "Bad index!");
62 aArrayCluster[aIndex].AppendElements(mArrays[aIndex]);
63 }
64
65 template <class T>
66 void
67 AppendElementsTo(nsTArray<T>& aArray)
68 {
69 for (uint32_t index = 0; index < Length; index++) {
70 aArray.AppendElements(mArrays[index]);
71 }
72 }
73
74 template<class T>
75 void
76 AppendElementsTo(ArrayCluster<T, Length>& aArrayCluster)
77 {
78 for (uint32_t index = 0; index < Length; index++) {
79 aArrayCluster[index].AppendElements(mArrays[index]);
80 }
81 }
82
83 template<class T>
84 void
85 SwapElements(ArrayCluster<T, Length>& aArrayCluster)
86 {
87 for (uint32_t index = 0; index < Length; index++) {
88 mArrays[index].SwapElements(aArrayCluster.mArrays[index]);
89 }
90 }
91
92 void
93 Clear()
94 {
95 for (uint32_t index = 0; index < Length; index++) {
96 mArrays[index].Clear();
97 }
98 }
99
100 private:
101 nsAutoTArray<nsTArray<ValueType>, Length> mArrays;
102 };
103
104 END_QUOTA_NAMESPACE
105
106 #endif // mozilla_dom_quota_arraycluster_h__

mercurial