tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

StackArray.h (1066B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=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
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 /* A handy class that will allocate data for size*T objects on the stack and
      8 * otherwise allocate them on the heap. It is similar in purpose to AutoTArray
      9 */
     10 
     11 #ifndef MOZILLA_GFX_STACKARRAY_H_
     12 #define MOZILLA_GFX_STACKARRAY_H_
     13 
     14 template <class T, size_t size>
     15 class StackArray final {
     16 public:
     17  explicit StackArray(size_t count) {
     18    if (count > size) {
     19      mData = new T[count];
     20    } else {
     21      mData = mStackData;
     22    }
     23  }
     24  ~StackArray() {
     25    if (mData != mStackData) {
     26      delete[] mData;
     27    }
     28  }
     29  T& operator[](size_t n) { return mData[n]; }
     30  const T& operator[](size_t n) const { return mData[n]; }
     31  T* data() { return mData; };
     32 
     33 private:
     34  T mStackData[size];
     35  T* mData;
     36 };
     37 
     38 #endif  // MOZILLA_GFX_STACKARRAY_H_