aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/containers/stack_array/stack_array.h
blob: 9f097da72c12fd6b916b7896d3704dad748703c5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#pragma once

#include "range_ops.h"

#include <util/generic/array_ref.h> 
#include <util/system/defaults.h> /* For alloca. */

namespace NStackArray {
    /**
     * A stack-allocated array. Should be used instead of � variable length 
     * arrays that are not part of C++ standard.
     *
     * Example usage:
     * @code
     * void f(int size) {
     *    // T array[size]; // Wrong!
     *    TStackArray<T> array(ALLOC_ON_STACK(T, size)); // Right!
     *    // ...
     * }
     * @endcode
     *
     * Note that it is generally a *VERY BAD* idea to use this in inline methods
     * as those might be called from a loop, and then stack overflow is in the cards.
     */
    template <class T>
    class TStackArray: public TArrayRef<T> {
    public:
        inline TStackArray(void* data, size_t len)
            : TArrayRef<T>((T*)data, len)
        {
            NRangeOps::InitializeRange(this->begin(), this->end());
        }

        inline ~TStackArray() { 
            NRangeOps::DestroyRange(this->begin(), this->end());
        }
    };
}

#define ALLOC_ON_STACK(type, n) alloca(sizeof(type) * (n)), (n)