aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/config/sax.h
blob: 8ce252c9a714abf2d23856b2def9235782a2c9e6 (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
#pragma once

#include "config.h"

#include <util/string/cast.h>
#include <util/generic/maybe.h>
#include <util/generic/string.h>
#include <util/generic/vector.h>
#include <util/generic/yexception.h>

class IInputStream;

namespace NConfig {
    class IConfig {
    public:
        class IValue {
        public:
            virtual TString AsString() = 0;
            virtual bool AsBool() = 0;
            virtual IConfig* AsSubConfig() = 0;

            virtual bool IsContainer() const = 0;
        };

        class IFunc {
        public:
            inline void Consume(const TString& key, IValue* value) {
                DoConsume(key, value);
            }

            virtual ~IFunc() = default;

        private:
            virtual void DoConsume(const TString& key, IValue* value) {
                (void)key;
                (void)value;
            }
        };

        virtual ~IConfig() = default;

        inline void ForEach(IFunc* func) {
            DoForEach(func);
        }
        virtual void DumpJson(IOutputStream& stream) const = 0;
        virtual void DumpLua(IOutputStream& stream) const = 0;

    private:
        virtual void DoForEach(IFunc* func) = 0;
    };

    template <class T>
    static inline bool ParseFromString(const TString& s, TMaybe<T>& t) {
        t.ConstructInPlace(FromString<T>(s));

        return true;
    }

    template <class T>
    static inline bool ParseFromString(const TString& s, THolder<T>& t) {
        t = MakeHolder<T>(FromString<T>(s));

        return true;
    }

    template <class T>
    static inline bool ParseFromString(const TString& s, T& t) {
        t = FromString<T>(s);

        return true;
    }

    THolder<IConfig> ConfigParser(IInputStream& in, const TGlobals& globals = TGlobals());
}

#define START_PARSE                                                                \
    void DoConsume(const TString& key, NConfig::IConfig::IValue* value) override { \
        (void)key;                                                                 \
        (void)value;
#define END_PARSE                                                                    \
    ythrow NConfig::TConfigParseError() << "unsupported key(" << key.Quote() << ")"; \
    }
#define ON_KEY(k, v) if (key == k && NConfig::ParseFromString(value->AsString(), v))