24#ifndef OPENSUBDIV3_VTR_STACK_BUFFER_H
25#define OPENSUBDIV3_VTR_STACK_BUFFER_H
27#include "../version.h"
30namespace OPENSUBDIV_VERSION {
49template <
typename TYPE,
unsigned int SIZE,
bool POD_TYPE = false>
53 typedef unsigned int size_type;
58 StackBuffer(size_type size);
67 operator TYPE
const * ()
const {
return _data; }
68 operator TYPE * () {
return _data; }
70 size_type GetSize()
const {
return _size; }
72 void SetSize(size_type size);
73 void Reserve(size_type capacity);
77 StackBuffer(
const StackBuffer<TYPE,SIZE,POD_TYPE> &) { }
78 StackBuffer& operator=(
const StackBuffer<TYPE,SIZE,POD_TYPE> &) {
return *
this; }
80 void allocate(size_type capacity);
92 char _staticData[SIZE *
sizeof(TYPE)];
100template <
typename TYPE,
unsigned int SIZE,
bool POD_TYPE>
102StackBuffer<TYPE,SIZE,POD_TYPE>::allocate(size_type capacity) {
107 _dynamicData =
static_cast<char*
>(::operator
new(capacity *
sizeof(TYPE)));
109 _data =
reinterpret_cast<TYPE*
>(_dynamicData);
110 _capacity = capacity;
113template <
typename TYPE,
unsigned int SIZE,
bool POD_TYPE>
115StackBuffer<TYPE,SIZE,POD_TYPE>::deallocate() {
117 ::operator
delete(_dynamicData);
119 _data =
reinterpret_cast<TYPE*
>(_staticData);
129template <
typename TYPE,
unsigned int SIZE,
bool POD_TYPE>
131StackBuffer<TYPE,SIZE,POD_TYPE>::construct() {
133 for (size_type i = 0; i < _size; ++i) {
134 (void)
new (&_data[i]) TYPE;
137template <
typename TYPE,
unsigned int SIZE,
bool POD_TYPE>
139StackBuffer<TYPE,SIZE,POD_TYPE>::destruct() {
141 for (size_type i = 0; i < _size; ++i) {
149template <
typename TYPE,
unsigned int SIZE,
bool POD_TYPE>
151StackBuffer<TYPE,SIZE,POD_TYPE>::StackBuffer() :
152 _data(reinterpret_cast<TYPE*>(_staticData)),
159template <
typename TYPE,
unsigned int SIZE,
bool POD_TYPE>
161StackBuffer<TYPE,SIZE,POD_TYPE>::StackBuffer(size_type size) :
162 _data(reinterpret_cast<TYPE*>(_staticData)),
175template <
typename TYPE,
unsigned int SIZE,
bool POD_TYPE>
177StackBuffer<TYPE,SIZE,POD_TYPE>::~StackBuffer() {
188template <
typename TYPE,
unsigned int SIZE,
bool POD_TYPE>
190StackBuffer<TYPE,SIZE,POD_TYPE>::Reserve(size_type capacity) {
192 if (capacity > _capacity) {
201template <
typename TYPE,
unsigned int SIZE,
bool POD_TYPE>
203StackBuffer<TYPE,SIZE,POD_TYPE>::SetSize(size_type size)
210 }
else if (size > _capacity) {
224using namespace OPENSUBDIV_VERSION;