StackOrHeapArray.h revision 4c5e5d8b
1/*
2 * Copyright 2012, Jonathan Schleifer <js@webkeks.org>. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
4 */
5#ifndef	_SUPPORT_STACKORHEAPARRAY_H
6#define	_SUPPORT_STACKORHEAPARRAY_H
7
8#include <cstddef>
9#include <new>
10
11template <typename Type, int StackSize>
12class BStackOrHeapArray {
13public:
14	BStackOrHeapArray(size_t count)
15	{
16		if (count > StackSize)
17			fData = new(std::nothrow) Type[count];
18		else
19			fData = fStackData;
20	}
21
22	~BStackOrHeapArray()
23	{
24		if (fData != fStackData)
25			delete[] fData;
26	}
27
28	bool IsValid() const
29	{
30		return fData != NULL;
31	}
32
33	operator Type*()
34	{
35		return fData;
36	}
37
38private:
39	Type	fStackData[StackSize];
40	Type*	fData;
41};
42
43#endif
44