summaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/containers/non_empty.h
blob: f0cec6ce884cc9c03ac9e79f9db46fee125c53a2 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#pragma once

#include <library/cpp/yt/string/string_builder.h>

#include <util/system/compiler.h>

#include <initializer_list>

namespace NYT {

////////////////////////////////////////////////////////////////////////////////

template <class TContainer>
class TNonEmpty
{
public:
    using TUnderlying = TContainer;

    using size_type = typename TUnderlying::size_type;
    using difference_type = typename TUnderlying::difference_type;

    using value_type = typename TUnderlying::value_type;

    using iterator = typename TUnderlying::iterator;
    using const_iterator = typename TUnderlying::const_iterator;

    using const_reverse_iterator = typename TUnderlying::const_reverse_iterator;
    using reverse_iterator = typename TUnderlying::reverse_iterator;

    using reference = typename TUnderlying::reference;
    using const_reference = typename TUnderlying::const_reference;

    using pointer = typename TUnderlying::pointer;
    using const_pointer = typename TUnderlying::const_pointer;

    explicit TNonEmpty(TContainer underlying);
    template <class TIterator>
    TNonEmpty(TIterator first, TIterator last);
    TNonEmpty(std::initializer_list<value_type> values);

    size_type size() const;

    void resize(size_type size);
    void resize(size_type size, const value_type& value);

    iterator begin();
    const_iterator begin() const;
    iterator end();
    const_iterator end() const;

    reverse_iterator rbegin();
    const_reverse_iterator rbegin() const;
    reverse_iterator rend();
    const_reverse_iterator rend() const;

    pointer data();
    const_pointer data() const;

    reference operator[](size_type index);
    const_reference operator[](size_type index) const;

    reference front();
    const_reference front() const;
    reference back();
    const_reference back() const;

    void push_back(const value_type& value);
    void push_back(value_type&& value);

    template <class... TArgs>
    reference emplace_back(TArgs&&... args);

    void pop_back();

    const TContainer& Get() const Y_LIFETIME_BOUND;
    operator const TContainer&() const Y_LIFETIME_BOUND;

private:
    TContainer Underlying_;
};

template <class TContainer>
void FormatValue(TStringBuilderBase* builder, const TNonEmpty<TContainer>& nonempty, TStringBuf spec);

////////////////////////////////////////////////////////////////////////////////

} // namespace NYT

#define NON_EMPTY_INL_H_
#include "non_empty-inl.h"
#undef NON_EMPTY_INL_H_