encoder_state.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #pragma once
  2. #include "encoder_state_enum.h"
  3. #include <util/generic/serialized_enum.h>
  4. #include <util/generic/yexception.h>
  5. namespace NMonitoring {
  6. template <typename EEncoderState>
  7. class TEncoderStateImpl {
  8. public:
  9. using EState = EEncoderState;
  10. explicit TEncoderStateImpl(EEncoderState state = EEncoderState::ROOT)
  11. : State_(state)
  12. {
  13. }
  14. TEncoderStateImpl& operator=(EEncoderState rhs) noexcept {
  15. State_ = rhs;
  16. return *this;
  17. }
  18. inline bool operator==(EEncoderState rhs) const noexcept {
  19. return State_ == rhs;
  20. }
  21. inline bool operator!=(EEncoderState rhs) const noexcept {
  22. return !operator==(rhs);
  23. }
  24. [[noreturn]] inline void ThrowInvalid(TStringBuf message) const {
  25. ythrow yexception() << "invalid encoder state: "
  26. << ToStr() << ", " << message;
  27. }
  28. inline void Expect(EEncoderState expected) const {
  29. if (Y_UNLIKELY(State_ != expected)) {
  30. ythrow yexception()
  31. << "invalid encoder state: " << ToStr()
  32. << ", expected: " << TEncoderStateImpl(expected).ToStr();
  33. }
  34. }
  35. inline void Switch(EEncoderState from, EEncoderState to) {
  36. Expect(from);
  37. State_ = to;
  38. }
  39. TStringBuf ToStr() const noexcept {
  40. return NEnumSerializationRuntime::GetEnumNamesImpl<EEncoderState>().at(State_);
  41. }
  42. private:
  43. EEncoderState State_;
  44. };
  45. using TEncoderState = TEncoderStateImpl<EEncoderState>;
  46. } // namespace NMonitoring