gfx/2d/StackArray.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/2d/StackArray.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,30 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +/* A handy class that will allocate data for size*T objects on the stack and
     1.9 + * otherwise allocate them on the heap. It is similar in purpose to nsAutoTArray */
    1.10 +
    1.11 +template <class T, size_t size>
    1.12 +class StackArray
    1.13 +{
    1.14 +public:
    1.15 +  StackArray(size_t count) {
    1.16 +    if (count > size) {
    1.17 +      mData = new T[count];
    1.18 +    } else {
    1.19 +      mData = mStackData;
    1.20 +    }
    1.21 +  }
    1.22 +  ~StackArray() {
    1.23 +    if (mData != mStackData) {
    1.24 +      delete[] mData;
    1.25 +    }
    1.26 +  }
    1.27 +  T& operator[](size_t n) { return mData[n]; }
    1.28 +  const T& operator[](size_t n) const { return mData[n]; }
    1.29 +  T* data() { return mData; };
    1.30 +private:
    1.31 +  T mStackData[size];
    1.32 +  T* mData;
    1.33 +};

mercurial