/src/mozilla-central/gfx/2d/StackArray.h
Line | Count | Source (jump to first uncovered line) |
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 | | template <class T, size_t size> |
11 | | class StackArray |
12 | | { |
13 | | public: |
14 | 0 | explicit StackArray(size_t count) { |
15 | 0 | if (count > size) { |
16 | 0 | mData = new T[count]; |
17 | 0 | } else { |
18 | 0 | mData = mStackData; |
19 | 0 | } |
20 | 0 | } |
21 | 0 | ~StackArray() { |
22 | 0 | if (mData != mStackData) { |
23 | 0 | delete[] mData; |
24 | 0 | } |
25 | 0 | } |
26 | 0 | T& operator[](size_t n) { return mData[n]; } |
27 | | const T& operator[](size_t n) const { return mData[n]; } |
28 | 0 | T* data() { return mData; }; |
29 | | private: |
30 | | T mStackData[size]; |
31 | | T* mData; |
32 | | }; |