1b5fc5ecd3fe5315fc2756c0c25adc458cc8c8d91Elliott Hughes/*
2b5fc5ecd3fe5315fc2756c0c25adc458cc8c8d91Elliott Hughes * Copyright (C) 2009 The Android Open Source Project
3b5fc5ecd3fe5315fc2756c0c25adc458cc8c8d91Elliott Hughes *
4b5fc5ecd3fe5315fc2756c0c25adc458cc8c8d91Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
5b5fc5ecd3fe5315fc2756c0c25adc458cc8c8d91Elliott Hughes * you may not use this file except in compliance with the License.
6b5fc5ecd3fe5315fc2756c0c25adc458cc8c8d91Elliott Hughes * You may obtain a copy of the License at
7b5fc5ecd3fe5315fc2756c0c25adc458cc8c8d91Elliott Hughes *
8b5fc5ecd3fe5315fc2756c0c25adc458cc8c8d91Elliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
9b5fc5ecd3fe5315fc2756c0c25adc458cc8c8d91Elliott Hughes *
10b5fc5ecd3fe5315fc2756c0c25adc458cc8c8d91Elliott Hughes * Unless required by applicable law or agreed to in writing, software
11b5fc5ecd3fe5315fc2756c0c25adc458cc8c8d91Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
12b5fc5ecd3fe5315fc2756c0c25adc458cc8c8d91Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13b5fc5ecd3fe5315fc2756c0c25adc458cc8c8d91Elliott Hughes * See the License for the specific language governing permissions and
14b5fc5ecd3fe5315fc2756c0c25adc458cc8c8d91Elliott Hughes * limitations under the License.
15b5fc5ecd3fe5315fc2756c0c25adc458cc8c8d91Elliott Hughes */
16b5fc5ecd3fe5315fc2756c0c25adc458cc8c8d91Elliott Hughes
17845ce3cbfd6972542b275c95eddfbb6e94469737Elliott Hughes#ifndef LOCAL_ARRAY_H_included
18845ce3cbfd6972542b275c95eddfbb6e94469737Elliott Hughes#define LOCAL_ARRAY_H_included
19845ce3cbfd6972542b275c95eddfbb6e94469737Elliott Hughes
20845ce3cbfd6972542b275c95eddfbb6e94469737Elliott Hughes#include <cstddef>
21845ce3cbfd6972542b275c95eddfbb6e94469737Elliott Hughes#include <new>
22845ce3cbfd6972542b275c95eddfbb6e94469737Elliott Hughes
23845ce3cbfd6972542b275c95eddfbb6e94469737Elliott Hughes/**
24845ce3cbfd6972542b275c95eddfbb6e94469737Elliott Hughes * A fixed-size array with a size hint. That number of bytes will be allocated
25845ce3cbfd6972542b275c95eddfbb6e94469737Elliott Hughes * on the stack, and used if possible, but if more bytes are requested at
26845ce3cbfd6972542b275c95eddfbb6e94469737Elliott Hughes * construction time, a buffer will be allocated on the heap (and deallocated
27845ce3cbfd6972542b275c95eddfbb6e94469737Elliott Hughes * by the destructor).
28845ce3cbfd6972542b275c95eddfbb6e94469737Elliott Hughes *
29845ce3cbfd6972542b275c95eddfbb6e94469737Elliott Hughes * The API is intended to be a compatible subset of C++0x's std::array.
30845ce3cbfd6972542b275c95eddfbb6e94469737Elliott Hughes */
31845ce3cbfd6972542b275c95eddfbb6e94469737Elliott Hughestemplate <size_t STACK_BYTE_COUNT>
32845ce3cbfd6972542b275c95eddfbb6e94469737Elliott Hughesclass LocalArray {
33845ce3cbfd6972542b275c95eddfbb6e94469737Elliott Hughespublic:
34845ce3cbfd6972542b275c95eddfbb6e94469737Elliott Hughes    /**
35845ce3cbfd6972542b275c95eddfbb6e94469737Elliott Hughes     * Allocates a new fixed-size array of the given size. If this size is
36845ce3cbfd6972542b275c95eddfbb6e94469737Elliott Hughes     * less than or equal to the template parameter STACK_BYTE_COUNT, an
37845ce3cbfd6972542b275c95eddfbb6e94469737Elliott Hughes     * internal on-stack buffer will be used. Otherwise a heap buffer will
38845ce3cbfd6972542b275c95eddfbb6e94469737Elliott Hughes     * be allocated.
39845ce3cbfd6972542b275c95eddfbb6e94469737Elliott Hughes     */
40845ce3cbfd6972542b275c95eddfbb6e94469737Elliott Hughes    LocalArray(size_t desiredByteCount) : mSize(desiredByteCount) {
41845ce3cbfd6972542b275c95eddfbb6e94469737Elliott Hughes        if (desiredByteCount > STACK_BYTE_COUNT) {
42845ce3cbfd6972542b275c95eddfbb6e94469737Elliott Hughes            mPtr = new char[mSize];
43845ce3cbfd6972542b275c95eddfbb6e94469737Elliott Hughes        } else {
44845ce3cbfd6972542b275c95eddfbb6e94469737Elliott Hughes            mPtr = &mOnStackBuffer[0];
45845ce3cbfd6972542b275c95eddfbb6e94469737Elliott Hughes        }
46845ce3cbfd6972542b275c95eddfbb6e94469737Elliott Hughes    }
47845ce3cbfd6972542b275c95eddfbb6e94469737Elliott Hughes
48845ce3cbfd6972542b275c95eddfbb6e94469737Elliott Hughes    /**
49845ce3cbfd6972542b275c95eddfbb6e94469737Elliott Hughes     * Frees the heap-allocated buffer, if there was one.
50845ce3cbfd6972542b275c95eddfbb6e94469737Elliott Hughes     */
51845ce3cbfd6972542b275c95eddfbb6e94469737Elliott Hughes    ~LocalArray() {
52845ce3cbfd6972542b275c95eddfbb6e94469737Elliott Hughes        if (mPtr != &mOnStackBuffer[0]) {
53845ce3cbfd6972542b275c95eddfbb6e94469737Elliott Hughes            delete[] mPtr;
54845ce3cbfd6972542b275c95eddfbb6e94469737Elliott Hughes        }
55845ce3cbfd6972542b275c95eddfbb6e94469737Elliott Hughes    }
56845ce3cbfd6972542b275c95eddfbb6e94469737Elliott Hughes
57845ce3cbfd6972542b275c95eddfbb6e94469737Elliott Hughes    // Capacity.
58845ce3cbfd6972542b275c95eddfbb6e94469737Elliott Hughes    size_t size() { return mSize; }
59845ce3cbfd6972542b275c95eddfbb6e94469737Elliott Hughes    bool empty() { return mSize == 0; }
60845ce3cbfd6972542b275c95eddfbb6e94469737Elliott Hughes
61845ce3cbfd6972542b275c95eddfbb6e94469737Elliott Hughes    // Element access.
62845ce3cbfd6972542b275c95eddfbb6e94469737Elliott Hughes    char& operator[](size_t n) { return mPtr[n]; }
63845ce3cbfd6972542b275c95eddfbb6e94469737Elliott Hughes    const char& operator[](size_t n) const { return mPtr[n]; }
64845ce3cbfd6972542b275c95eddfbb6e94469737Elliott Hughes
65845ce3cbfd6972542b275c95eddfbb6e94469737Elliott Hughesprivate:
66845ce3cbfd6972542b275c95eddfbb6e94469737Elliott Hughes    char mOnStackBuffer[STACK_BYTE_COUNT];
67845ce3cbfd6972542b275c95eddfbb6e94469737Elliott Hughes    char* mPtr;
68845ce3cbfd6972542b275c95eddfbb6e94469737Elliott Hughes    size_t mSize;
697ca6fd0dca02f7abdd8808db78357743bbdd23a5Elliott Hughes
707ca6fd0dca02f7abdd8808db78357743bbdd23a5Elliott Hughes    // Disallow copy and assignment.
717ca6fd0dca02f7abdd8808db78357743bbdd23a5Elliott Hughes    LocalArray(const LocalArray&);
727ca6fd0dca02f7abdd8808db78357743bbdd23a5Elliott Hughes    void operator=(const LocalArray&);
73845ce3cbfd6972542b275c95eddfbb6e94469737Elliott Hughes};
74845ce3cbfd6972542b275c95eddfbb6e94469737Elliott Hughes
75845ce3cbfd6972542b275c95eddfbb6e94469737Elliott Hughes#endif // LOCAL_ARRAY_H_included
76