/* -*-c++-*- */
/* osgEarth - Dynamic map generation toolkit for OpenSceneGraph
 * Copyright 2008-2009 Pelican Ventures, Inc.
 * http://osgearth.org
 *
 * osgEarth is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 */

#ifndef SEAMLESS_AUTOBUFFER
#define SEAMLESS_AUTOBUFFER 1

namespace seamless
{
/**
 * Implements an array that is locally allocated if less than a given
 * size; otherwise, it is allocated on the heap. This is useful if the
 * common case can, for example, easily be allocated on the stack.
 */
template<typename T, int N>
class AutoBuffer
{
public:
    AutoBuffer(int size)
        : _heapStore(size > N ? new T[size] : 0),
          _ptr(size > N ? _heapStore : &_store[0])
    {
    }

    ~AutoBuffer()
    {
        delete [] _heapStore;
    }

    T& operator[](int i) const { return _ptr[i]; }
    
private:
    AutoBuffer(const AutoBuffer&);
    T _store[N];
    T* _heapStore;
    T* const _ptr;
};
}
#endif
