Add BStackOrHeapArray.
This commit is contained in:
committed by
Stephan Aßmus
parent
6abec6b93a
commit
4bda0212ce
@@ -0,0 +1,42 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2012, Jonathan Schleifer <[email protected]>. All Rights Reserved.
|
||||||
|
* Distributed under the terms of the MIT License.
|
||||||
|
*/
|
||||||
|
#ifndef _SUPPORT_STACKORHEAPARRAY_H
|
||||||
|
#define _SUPPORT_STACKORHEAPARRAY_H
|
||||||
|
|
||||||
|
#include <new>
|
||||||
|
|
||||||
|
template <typename Type, int StackSize>
|
||||||
|
class BStackOrHeapArray {
|
||||||
|
public:
|
||||||
|
BStackOrHeapArray(size_t count)
|
||||||
|
{
|
||||||
|
if (count > StackSize)
|
||||||
|
fData = new(std::nothrow) Type[count];
|
||||||
|
else
|
||||||
|
fData = fStackData;
|
||||||
|
}
|
||||||
|
|
||||||
|
~BStackOrHeapArray()
|
||||||
|
{
|
||||||
|
if (fData != fStackData)
|
||||||
|
delete[] fData;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool IsValid() const
|
||||||
|
{
|
||||||
|
return fData != NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
operator Type*()
|
||||||
|
{
|
||||||
|
return fData;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
Type fStackData[StackSize];
|
||||||
|
Type* fData;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
Reference in New Issue
Block a user