sax.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #pragma once
  2. #include "config.h"
  3. #include <util/string/cast.h>
  4. #include <util/generic/maybe.h>
  5. #include <util/generic/string.h>
  6. #include <util/generic/vector.h>
  7. #include <util/generic/yexception.h>
  8. class IInputStream;
  9. namespace NConfig {
  10. class IConfig {
  11. public:
  12. class IValue {
  13. public:
  14. virtual TString AsString() = 0;
  15. virtual bool AsBool() = 0;
  16. virtual IConfig* AsSubConfig() = 0;
  17. virtual bool IsContainer() const = 0;
  18. };
  19. class IFunc {
  20. public:
  21. inline void Consume(const TString& key, IValue* value) {
  22. DoConsume(key, value);
  23. }
  24. virtual ~IFunc() = default;
  25. private:
  26. virtual void DoConsume(const TString& key, IValue* value) {
  27. (void)key;
  28. (void)value;
  29. }
  30. };
  31. virtual ~IConfig() = default;
  32. inline void ForEach(IFunc* func) {
  33. DoForEach(func);
  34. }
  35. virtual void DumpJson(IOutputStream& stream) const = 0;
  36. virtual void DumpLua(IOutputStream& stream) const = 0;
  37. private:
  38. virtual void DoForEach(IFunc* func) = 0;
  39. };
  40. template <class T>
  41. static inline bool ParseFromString(const TString& s, TMaybe<T>& t) {
  42. t.ConstructInPlace(FromString<T>(s));
  43. return true;
  44. }
  45. template <class T>
  46. static inline bool ParseFromString(const TString& s, THolder<T>& t) {
  47. t = MakeHolder<T>(FromString<T>(s));
  48. return true;
  49. }
  50. template <class T>
  51. static inline bool ParseFromString(const TString& s, T& t) {
  52. t = FromString<T>(s);
  53. return true;
  54. }
  55. THolder<IConfig> ConfigParser(IInputStream& in, const TGlobals& globals = TGlobals());
  56. }
  57. #define START_PARSE \
  58. void DoConsume(const TString& key, NConfig::IConfig::IValue* value) override { \
  59. (void)key; \
  60. (void)value;
  61. #define END_PARSE \
  62. ythrow NConfig::TConfigParseError() << "unsupported key(" << key.Quote() << ")"; \
  63. }
  64. #define ON_KEY(k, v) if (key == k && NConfig::ParseFromString(value->AsString(), v))