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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
#pragma once
#include "fwd.h"
#include "value.h"
#include <library/cpp/json/json_value.h>
#include <util/generic/hash.h>
#include <util/generic/ptr.h>
#include <util/generic/deque.h>
#include <util/system/type_name.h>
#include <util/generic/vector.h>
#include <util/generic/yexception.h>
#include <util/generic/bt_exception.h>
#include <util/ysaveload.h>
class IInputStream;
class IOutputStream;
namespace NConfig {
typedef THashMap<TString, NJson::TJsonValue> TGlobals;
class TConfigError: public TWithBackTrace<yexception> {
};
class TConfigParseError: public TConfigError {
};
class TTypeMismatch: public TConfigError {
};
struct TArray;
struct TDict;
class TConfig {
public:
inline TConfig()
: V_(Null())
{
}
inline TConfig(IValue* v)
: V_(v)
{
}
TConfig(const TConfig& config) = default;
TConfig& operator=(const TConfig& config) = default;
template <class T>
inline bool IsA() const {
return V_->IsA(typeid(T));
}
inline bool IsNumeric() const {
return IsA<double>() || IsA<i64>() || IsA<ui64>();
}
template <class T>
inline const T& Get() const {
return GetNonConstant<T>();
}
template <class T>
inline T& GetNonConstant() const {
if (this->IsA<T>()) {
return *(T*)V_->Ptr();
}
if constexpr (std::is_same_v<T, ::NConfig::TArray>) {
NCfgPrivate::ReportTypeMismatch(V_->TypeName(), "array");
} else if constexpr (std::is_same_v<T, ::NConfig::TDict>) {
NCfgPrivate::ReportTypeMismatch(V_->TypeName(), "dict");
} else if constexpr (std::is_same_v<T, TString>) {
NCfgPrivate::ReportTypeMismatch(V_->TypeName(), "string");
} else {
NCfgPrivate::ReportTypeMismatch(V_->TypeName(), ::TypeName<T>());
}
}
template <class T>
inline T As() const {
return ValueAs<T>(V_.Get());
}
template <class T>
inline T As(T def) const {
return IsNull() ? def : As<T>();
}
inline bool IsNull() const noexcept {
return V_.Get() == Null();
}
const TConfig& Or(const TConfig& r) const {
return IsNull() ? r : *this;
}
//assume value is dict
bool Has(const TStringBuf& key) const;
const TConfig& operator[](const TStringBuf& key) const;
const TConfig& At(const TStringBuf& key) const;
//assume value is array
const TConfig& operator[](size_t index) const;
size_t GetArraySize() const;
static TConfig FromIni(IInputStream& in, const TGlobals& g = TGlobals());
static TConfig FromJson(IInputStream& in, const TGlobals& g = TGlobals());
static TConfig FromLua(IInputStream& in, const TGlobals& g = TGlobals());
//load yconf format. unsafe, but natural mapping
static TConfig FromMarkup(IInputStream& in, const TGlobals& g = TGlobals());
static TConfig FromStream(IInputStream& in, const TGlobals& g = TGlobals());
inline void ToJson(IOutputStream& out) const {
V_->ToJson(out);
}
void DumpJson(IOutputStream& out) const;
void DumpLua(IOutputStream& out) const;
static TConfig ReadJson(TStringBuf in, const TGlobals& g = TGlobals());
static TConfig ReadLua(TStringBuf in, const TGlobals& g = TGlobals());
static TConfig ReadMarkup(TStringBuf in, const TGlobals& g = TGlobals());
static TConfig ReadIni(TStringBuf in, const TGlobals& g = TGlobals());
void Load(IInputStream* stream);
void Save(IOutputStream* stream) const;
private:
TIntrusivePtr<IValue> V_;
};
struct TArray: public TDeque<TConfig> {
const TConfig& Index(size_t index) const;
const TConfig& At(size_t index) const;
};
struct TDict: public THashMap<TString, TConfig> {
const TConfig& Find(const TStringBuf& key) const;
const TConfig& At(const TStringBuf& key) const;
};
THolder<IInputStream> CreatePreprocessor(const TGlobals& g, IInputStream& in);
}
|