resetable_setting.h 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #pragma once
  2. #include "yql_panic.h"
  3. #include <util/generic/maybe.h>
  4. #include <util/generic/variant.h>
  5. namespace NYql {
  6. template <typename S, typename R>
  7. class TResetableSettingBase {
  8. protected:
  9. using TSet = S;
  10. using TReset = R;
  11. public:
  12. void Set(const TSet& value) {
  13. Value.ConstructInPlace(value);
  14. }
  15. void Reset(const TReset& value) {
  16. Value.ConstructInPlace(value);
  17. }
  18. bool Defined() const {
  19. return Value.Defined();
  20. }
  21. explicit operator bool() const {
  22. return Defined();
  23. }
  24. bool IsSet() const {
  25. YQL_ENSURE(Defined());
  26. return Value->index() == 0;
  27. }
  28. const TSet& GetValueSet() const {
  29. YQL_ENSURE(IsSet());
  30. return std::get<TSet>(*Value);
  31. }
  32. const TReset& GetValueReset() const {
  33. YQL_ENSURE(!IsSet());
  34. return std::get<TReset>(*Value);
  35. }
  36. private:
  37. TMaybe<std::variant<TSet, TReset>> Value;
  38. };
  39. template <typename S, typename R>
  40. class TResetableSetting: public TResetableSettingBase<S, R> {
  41. };
  42. template <typename S>
  43. class TResetableSetting<S, void>: public TResetableSettingBase<S, TNothing> {
  44. private:
  45. const TNothing& GetValueReset() const;
  46. public:
  47. void Reset() {
  48. TResetableSettingBase<S, TNothing>::Reset(Nothing());
  49. }
  50. };
  51. }