mfbt/EnumeratedArray.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mfbt/EnumeratedArray.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,81 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim: set ts=8 sts=2 et sw=2 tw=80: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +/* EnumeratedArray is like Array, but indexed by a typed enum. */
    1.11 +
    1.12 +#ifndef mozilla_EnumeratedArray_h
    1.13 +#define mozilla_EnumeratedArray_h
    1.14 +
    1.15 +#include "mozilla/Array.h"
    1.16 +#include "mozilla/TypedEnum.h"
    1.17 +
    1.18 +namespace mozilla {
    1.19 +
    1.20 +/**
    1.21 + * EnumeratedArray is a fixed-size array container for use when an
    1.22 + * array is indexed by a specific enum class, as currently implemented
    1.23 + * by MOZ_BEGIN_ENUM_CLASS.
    1.24 + *
    1.25 + * This provides type safety by guarding at compile time against accidentally
    1.26 + * indexing such arrays with unrelated values. This also removes the need
    1.27 + * for manual casting when using a typed enum value to index arrays.
    1.28 + *
    1.29 + * Aside from the typing of indices, EnumeratedArray is similar to Array.
    1.30 + *
    1.31 + * Example:
    1.32 + *
    1.33 + *   MOZ_BEGIN_ENUM_CLASS(AnimalSpecies)
    1.34 + *     Cow,
    1.35 + *     Sheep,
    1.36 + *     Count
    1.37 + *   MOZ_END_ENUM_CLASS(AnimalSpecies)
    1.38 + *
    1.39 + *   EnumeratedArray<AnimalSpecies, AnimalSpecies::Count, int> headCount;
    1.40 + *
    1.41 + *   headCount[AnimalSpecies::Cow] = 17;
    1.42 + *   headCount[AnimalSpecies::Sheep] = 30;
    1.43 + *
    1.44 + */
    1.45 +template<typename IndexType,
    1.46 +         MOZ_TEMPLATE_ENUM_CLASS_ENUM_TYPE(IndexType) SizeAsEnumValue,
    1.47 +         typename ValueType>
    1.48 +class EnumeratedArray
    1.49 +{
    1.50 +  public:
    1.51 +    static const size_t Size = size_t(SizeAsEnumValue);
    1.52 +
    1.53 +  private:
    1.54 +    Array<ValueType, Size> mArray;
    1.55 +
    1.56 +  public:
    1.57 +    EnumeratedArray() {}
    1.58 +
    1.59 +    explicit EnumeratedArray(const EnumeratedArray& aOther)
    1.60 +    {
    1.61 +      for (size_t i = 0; i < Size; i++)
    1.62 +        mArray[i] = aOther.mArray[i];
    1.63 +    }
    1.64 +
    1.65 +    explicit EnumeratedArray(const ValueType (&aOther)[Size])
    1.66 +    {
    1.67 +      for (size_t i = 0; i < Size; i++)
    1.68 +        mArray[i] = aOther[i];
    1.69 +    }
    1.70 +
    1.71 +    ValueType& operator[](IndexType aIndex)
    1.72 +    {
    1.73 +      return mArray[size_t(aIndex)];
    1.74 +    }
    1.75 +
    1.76 +    const ValueType& operator[](IndexType aIndex) const
    1.77 +    {
    1.78 +      return mArray[size_t(aIndex)];
    1.79 +    }
    1.80 +};
    1.81 +
    1.82 +} // namespace mozilla
    1.83 +
    1.84 +#endif // mozilla_EnumeratedArray_h

mercurial