xref: /haiku/headers/os/support/StackOrHeapArray.h (revision 1deede7388b04dbeec5af85cae7164735ea9e70d)
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 
11 template <typename Type, int StackSize>
12 class BStackOrHeapArray {
13 public:
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 
38 private:
39 	Type	fStackData[StackSize];
40 	Type*	fData;
41 };
42 
43 #endif
44