xref: /haiku/headers/os/support/StackOrHeapArray.h (revision 4bda0212cedb6225acb3a1b7cc9f61d395787b1f)
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 <new>
9 
10 template <typename Type, int StackSize>
11 class BStackOrHeapArray {
12 public:
13 	BStackOrHeapArray(size_t count)
14 	{
15 		if (count > StackSize)
16 			fData = new(std::nothrow) Type[count];
17 		else
18 			fData = fStackData;
19 	}
20 
21 	~BStackOrHeapArray()
22 	{
23 		if (fData != fStackData)
24 			delete[] fData;
25 	}
26 
27 	bool IsValid() const
28 	{
29 		return fData != NULL;
30 	}
31 
32 	operator Type*()
33 	{
34 		return fData;
35 	}
36 
37 private:
38 	Type	fStackData[StackSize];
39 	Type*	fData;
40 };
41 
42 #endif
43