mfbt/EnumeratedArray.h

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 /* EnumeratedArray is like Array, but indexed by a typed enum. */
michael@0 8
michael@0 9 #ifndef mozilla_EnumeratedArray_h
michael@0 10 #define mozilla_EnumeratedArray_h
michael@0 11
michael@0 12 #include "mozilla/Array.h"
michael@0 13 #include "mozilla/TypedEnum.h"
michael@0 14
michael@0 15 namespace mozilla {
michael@0 16
michael@0 17 /**
michael@0 18 * EnumeratedArray is a fixed-size array container for use when an
michael@0 19 * array is indexed by a specific enum class, as currently implemented
michael@0 20 * by MOZ_BEGIN_ENUM_CLASS.
michael@0 21 *
michael@0 22 * This provides type safety by guarding at compile time against accidentally
michael@0 23 * indexing such arrays with unrelated values. This also removes the need
michael@0 24 * for manual casting when using a typed enum value to index arrays.
michael@0 25 *
michael@0 26 * Aside from the typing of indices, EnumeratedArray is similar to Array.
michael@0 27 *
michael@0 28 * Example:
michael@0 29 *
michael@0 30 * MOZ_BEGIN_ENUM_CLASS(AnimalSpecies)
michael@0 31 * Cow,
michael@0 32 * Sheep,
michael@0 33 * Count
michael@0 34 * MOZ_END_ENUM_CLASS(AnimalSpecies)
michael@0 35 *
michael@0 36 * EnumeratedArray<AnimalSpecies, AnimalSpecies::Count, int> headCount;
michael@0 37 *
michael@0 38 * headCount[AnimalSpecies::Cow] = 17;
michael@0 39 * headCount[AnimalSpecies::Sheep] = 30;
michael@0 40 *
michael@0 41 */
michael@0 42 template<typename IndexType,
michael@0 43 MOZ_TEMPLATE_ENUM_CLASS_ENUM_TYPE(IndexType) SizeAsEnumValue,
michael@0 44 typename ValueType>
michael@0 45 class EnumeratedArray
michael@0 46 {
michael@0 47 public:
michael@0 48 static const size_t Size = size_t(SizeAsEnumValue);
michael@0 49
michael@0 50 private:
michael@0 51 Array<ValueType, Size> mArray;
michael@0 52
michael@0 53 public:
michael@0 54 EnumeratedArray() {}
michael@0 55
michael@0 56 explicit EnumeratedArray(const EnumeratedArray& aOther)
michael@0 57 {
michael@0 58 for (size_t i = 0; i < Size; i++)
michael@0 59 mArray[i] = aOther.mArray[i];
michael@0 60 }
michael@0 61
michael@0 62 explicit EnumeratedArray(const ValueType (&aOther)[Size])
michael@0 63 {
michael@0 64 for (size_t i = 0; i < Size; i++)
michael@0 65 mArray[i] = aOther[i];
michael@0 66 }
michael@0 67
michael@0 68 ValueType& operator[](IndexType aIndex)
michael@0 69 {
michael@0 70 return mArray[size_t(aIndex)];
michael@0 71 }
michael@0 72
michael@0 73 const ValueType& operator[](IndexType aIndex) const
michael@0 74 {
michael@0 75 return mArray[size_t(aIndex)];
michael@0 76 }
michael@0 77 };
michael@0 78
michael@0 79 } // namespace mozilla
michael@0 80
michael@0 81 #endif // mozilla_EnumeratedArray_h

mercurial