aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/containers/stack_array/range_ops.h
blob: 1d40341aa190df92b7e3189d375de6a4db474597 (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
41
42
43
44
45
46
47
48
49
50
51
52
#pragma once

#include <util/generic/typetraits.h>

#include <new>

namespace NRangeOps {
    template <class T, bool isTrivial>
    struct TRangeOpsBase {
        static inline void DestroyRange(T* b, T* e) noexcept {
            while (e > b) {
                (--e)->~T();
            }
        }

        static inline void InitializeRange(T* b, T* e) {
            T* c = b;

            try {
                for (; c < e; ++c) {
                    new (c) T();
                }
            } catch (...) {
                DestroyRange(b, c);

                throw;
            }
        }
    };

    template <class T>
    struct TRangeOpsBase<T, true> {
        static inline void DestroyRange(T*, T*) noexcept {
        }

        static inline void InitializeRange(T*, T*) noexcept {
        }
    };

    template <class T>
    using TRangeOps = TRangeOpsBase<T, TTypeTraits<T>::IsPod>;

    template <class T>
    static inline void DestroyRange(T* b, T* e) noexcept {
        TRangeOps<T>::DestroyRange(b, e);
    }

    template <class T>
    static inline void InitializeRange(T* b, T* e) {
        TRangeOps<T>::InitializeRange(b, e);
    }
}