aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/monlib/encode/encoder_state.h
blob: fcfff53ede95b783122b0a0c80058da7894463ef (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
#pragma once

#include "encoder_state_enum.h" 
 
#include <util/generic/serialized_enum.h> 
#include <util/generic/yexception.h>

 
namespace NMonitoring {
 
    template <typename EEncoderState> 
    class TEncoderStateImpl { 
    public:
        using EState = EEncoderState; 

        explicit TEncoderStateImpl(EEncoderState state = EEncoderState::ROOT) 
            : State_(state)
        {
        }

        TEncoderStateImpl& operator=(EEncoderState rhs) noexcept { 
            State_ = rhs;
            return *this;
        }

        inline bool operator==(EEncoderState rhs) const noexcept {
            return State_ == rhs;
        }

        inline bool operator!=(EEncoderState rhs) const noexcept {
            return !operator==(rhs);
        }

        [[noreturn]] inline void ThrowInvalid(TStringBuf message) const {
            ythrow yexception() << "invalid encoder state: "
                                << ToStr() << ", " << message;
        }

        inline void Expect(EEncoderState expected) const {
            if (Y_UNLIKELY(State_ != expected)) {
                ythrow yexception()
                    << "invalid encoder state: " << ToStr()
                    << ", expected: " << TEncoderStateImpl(expected).ToStr(); 
            }
        }

        inline void Switch(EEncoderState from, EEncoderState to) {
            Expect(from);
            State_ = to;
        }

        TStringBuf ToStr() const noexcept { 
            return NEnumSerializationRuntime::GetEnumNamesImpl<EEncoderState>().at(State_); 
        } 

    private:
        EEncoderState State_;
    };

    using TEncoderState = TEncoderStateImpl<EEncoderState>; 
 
} // namespace NMonitoring