7#ifndef OPENSUBDIV3_VTR_STACK_BUFFER_H
8#define OPENSUBDIV3_VTR_STACK_BUFFER_H
10#include "../version.h"
32template <
typename TYPE,
unsigned int SIZE,
bool POD_TYPE = false>
36 typedef unsigned int size_type;
41 StackBuffer(size_type size);
50 operator TYPE
const * ()
const {
return _data; }
51 operator TYPE * () {
return _data; }
53 size_type GetSize()
const {
return _size; }
55 void SetSize(size_type size);
56 void Reserve(size_type capacity);
60 StackBuffer(
const StackBuffer<TYPE,SIZE,POD_TYPE> &) { }
61 StackBuffer& operator=(
const StackBuffer<TYPE,SIZE,POD_TYPE> &) {
return *
this; }
63 void allocate(size_type capacity);
75 char _staticData[SIZE *
sizeof(TYPE)];
83template <
typename TYPE,
unsigned int SIZE,
bool POD_TYPE>
85StackBuffer<TYPE,SIZE,POD_TYPE>::allocate(size_type capacity) {
90 _dynamicData =
static_cast<char*
>(::operator
new(capacity *
sizeof(TYPE)));
92 _data =
reinterpret_cast<TYPE*
>(_dynamicData);
96template <
typename TYPE,
unsigned int SIZE,
bool POD_TYPE>
98StackBuffer<TYPE,SIZE,POD_TYPE>::deallocate() {
100 ::operator
delete(_dynamicData);
101 _dynamicData =
nullptr;
103 _data =
reinterpret_cast<TYPE*
>(_staticData);
113template <
typename TYPE,
unsigned int SIZE,
bool POD_TYPE>
115StackBuffer<TYPE,SIZE,POD_TYPE>::construct() {
117 for (size_type i = 0; i < _size; ++i) {
118 (void)
new (&_data[i]) TYPE;
121template <
typename TYPE,
unsigned int SIZE,
bool POD_TYPE>
123StackBuffer<TYPE,SIZE,POD_TYPE>::destruct() {
125 for (size_type i = 0; i < _size; ++i) {
133template <
typename TYPE,
unsigned int SIZE,
bool POD_TYPE>
135StackBuffer<TYPE,SIZE,POD_TYPE>::StackBuffer() :
136 _data(reinterpret_cast<TYPE*>(_staticData)),
139 _dynamicData(nullptr) {
143template <
typename TYPE,
unsigned int SIZE,
bool POD_TYPE>
145StackBuffer<TYPE,SIZE,POD_TYPE>::StackBuffer(size_type size) :
146 _data(reinterpret_cast<TYPE*>(_staticData)),
149 _dynamicData(nullptr) {
159template <
typename TYPE,
unsigned int SIZE,
bool POD_TYPE>
161StackBuffer<TYPE,SIZE,POD_TYPE>::~StackBuffer() {
172template <
typename TYPE,
unsigned int SIZE,
bool POD_TYPE>
174StackBuffer<TYPE,SIZE,POD_TYPE>::Reserve(size_type capacity) {
176 if (capacity > _capacity) {
185template <
typename TYPE,
unsigned int SIZE,
bool POD_TYPE>
187StackBuffer<TYPE,SIZE,POD_TYPE>::SetSize(size_type size)
194 }
else if (size > _capacity) {
208using namespace OPENSUBDIV_VERSION;