Sese Framework
x.y.z
A cross-platform framework
载入中...
搜索中...
未找到
Value.h
浏览该文件的文档.
1
// Copyright 2024 libsese
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
19
20
#pragma once
21
22
#include <map>
23
#include <variant>
24
#include <vector>
25
#include <optional>
26
27
#include <
sese/Config.h
>
28
#include <
sese/text/StringBuilder.h
>
29
#include <
sese/text/Format.h
>
30
31
namespace
sese
{
32
34
class
Value
{
35
public
:
36
enum class
Type
{
37
NONE
= 0,
38
BOOL
,
39
INT
,
40
DOUBLE
,
41
STRING
,
42
BLOB
,
43
LIST
,
44
DICT
45
};
46
47
using
Integer
= int64_t;
48
using
String
= std::string;
49
using
Blob
= std::vector<uint8_t>;
50
51
class
List
;
52
class
Dict
;
53
55
class
Null
{};
56
58
class
List
{
59
public
:
60
using
Raw
= std::vector<Value>;
61
using
Iterator
= Raw::iterator;
62
using
ConstIterator
= Raw::const_iterator;
63
using
ReverseIterator
= Raw::reverse_iterator;
64
using
ConstReverseIterator
= Raw::const_reverse_iterator;
65
66
[[nodiscard]]
size_t
empty
()
const
;
67
[[nodiscard]]
size_t
size
()
const
;
68
void
reserve
(
size_t
size
);
69
void
resize
(
size_t
size
);
70
void
clear
();
71
72
const
Value
&
operator[]
(
size_t
index)
const
;
73
Value
&
operator[]
(
size_t
index);
74
75
size_t
erase
(
const
Value
&value);
76
Iterator
erase
(
Iterator
it);
77
ConstIterator
erase
(
ConstIterator
it);
78
Iterator
erase
(
const
Iterator
&first,
const
Iterator
&last);
79
ConstIterator
erase
(
const
ConstIterator
&first,
const
ConstIterator
&last);
80
81
Iterator
begin
();
82
[[nodiscard]]
ConstIterator
begin
()
const
;
83
Iterator
end
();
84
[[nodiscard]]
ConstIterator
end
()
const
;
85
86
[[nodiscard]]
ConstIterator
cbegin
()
const
;
87
[[nodiscard]]
ConstIterator
cend
()
const
;
88
89
ReverseIterator
rbegin
();
90
[[nodiscard]]
ConstReverseIterator
rbegin
()
const
;
91
ReverseIterator
rend
();
92
[[nodiscard]]
ConstReverseIterator
rend
()
const
;
93
94
[[nodiscard]]
const
Value
&
front
()
const
;
95
Value
&
front
();
96
[[nodiscard]]
const
Value
&
back
()
const
;
97
Value
&
back
();
98
99
void
append
(
Value
&&value) &;
100
void
append
(
bool
value) &;
101
void
append
(
Integer
value) &;
102
void
append
(
double
value) &;
103
void
append
(
const
char
*value) &;
104
void
append
(
String
&&value) &;
105
void
append
(
Blob
&&value) &;
106
void
append
(
const
char
*bytes,
size_t
length) &;
107
void
append
(
List
&&value) &;
108
void
append
(
Dict
&&value) &;
109
110
List
&&
append
(
Value
&&value) &&;
111
List
&&
append
(
bool
value) &&;
112
List
&&
append
(
Integer
value) &&;
113
List
&&
append
(
double
value) &&;
114
List
&&
append
(
const
char
*value) &&;
115
List
&&
append
(
String
&&value) &&;
116
List
&&
append
(
const
char
*bytes,
size_t
length) &&;
117
List
&&
append
(
Blob
&&value) &&;
118
List
&&
append
(
List
&&value) &&;
119
List
&&
append
(
Dict
&&value) &&;
120
121
Iterator
insert
(
Iterator
it,
Value
&&value);
122
123
private
:
124
Raw
vector
;
125
};
126
128
class
Dict
{
129
public
:
130
using
Raw
= std::map<String, Value *>;
131
using
Iterator
= Raw::iterator;
132
using
ConstIterator
= Raw::const_iterator;
133
using
ReverseIterator
= Raw::reverse_iterator;
134
using
ConstReverseIterator
= Raw::const_reverse_iterator;
135
136
Dict
() =
default
;
137
~Dict
();
138
// move constructor
139
Dict
(
Dict
&&other)
noexcept
;
140
Dict
&
operator=
(
Dict
&&other)
noexcept
;
141
// copy constructor
142
Dict
(
const
Dict
&other) =
delete
;
143
Dict
&
operator=
(
const
Dict
&other) =
delete
;
144
145
[[nodiscard]]
bool
empty
()
const
;
146
[[nodiscard]]
size_t
size
()
const
;
147
void
clear
();
148
[[nodiscard]]
bool
contains
(
const
String
&key)
const
;
149
150
Iterator
begin
();
151
[[nodiscard]]
ConstIterator
begin
()
const
;
152
Iterator
end
();
153
[[nodiscard]]
ConstIterator
end
()
const
;
154
155
[[nodiscard]]
ConstIterator
cbegin
()
const
;
156
[[nodiscard]]
ConstIterator
cend
()
const
;
157
158
ReverseIterator
rbegin
();
159
[[nodiscard]]
ConstReverseIterator
rbegin
()
const
;
160
ReverseIterator
rend
();
161
[[nodiscard]]
ConstReverseIterator
rend
()
const
;
162
163
Iterator
erase
(
const
Iterator
&it);
164
Iterator
erase
(
const
ConstIterator
&it);
165
166
[[nodiscard]]
const
Value
*
find
(
const
String
&key)
const
;
167
Value
*
find
(
const
String
&key);
168
169
void
set
(
const
String
&key,
Value
&&value) &;
170
void
set
(
const
String
&key,
bool
value) &;
171
void
set
(
const
String
&key,
Integer
value) &;
172
void
set
(
const
String
&key,
double
value) &;
173
void
set
(
const
String
&key,
const
char
*value) &;
174
void
set
(
const
String
&key,
const
char
*value,
size_t
length) &;
175
void
set
(
const
String
&key,
String
&&value) &;
176
void
set
(
const
String
&key,
Blob
&&value) &;
177
void
set
(
const
String
&key,
List
&&value) &;
178
void
set
(
const
String
&key,
Dict
&&value) &;
179
180
Dict
&&
set
(
const
String
&key,
Value
&&value) &&;
181
Dict
&&
set
(
const
String
&key,
bool
value) &&;
182
Dict
&&
set
(
const
String
&key,
Integer
value) &&;
183
Dict
&&
set
(
const
String
&key,
double
value) &&;
184
Dict
&&
set
(
const
String
&key,
const
char
*value) &&;
185
Dict
&&
set
(
const
String
&key,
const
char
*value,
size_t
length) &&;
186
Dict
&&
set
(
const
String
&key,
String
&&value) &&;
187
Dict
&&
set
(
const
String
&key,
Blob
&&value) &&;
188
Dict
&&
set
(
const
String
&key,
List
&&value) &&;
189
Dict
&&
set
(
const
String
&key,
Dict
&&value) &&;
190
191
bool
remove
(
const
String
&key);
192
193
private
:
194
Raw
map
;
195
};
196
197
Value
() =
default
;
198
explicit
Value
(
Type
type);
200
explicit
Value
(
bool
value);
202
explicit
Value
(
Integer
value);
204
explicit
Value
(
double
value);
206
explicit
Value
(
const
char
*value);
208
explicit
Value
(
const
char
*bytes,
size_t
length);
210
explicit
Value
(
String
&&value)
noexcept
;
212
explicit
Value
(
Blob
&&value);
214
explicit
Value
(
List
&&value);
216
explicit
Value
(
Dict
&&value);
217
218
static
Value
list
();
219
static
Value
dict
();
220
221
[[nodiscard]]
Type
getType
()
const
;
222
223
[[nodiscard]]
bool
isNull
()
const
;
224
[[nodiscard]]
bool
isBool
()
const
;
225
[[nodiscard]]
bool
isInt
()
const
;
226
[[nodiscard]]
bool
isDouble
()
const
;
227
[[nodiscard]]
bool
isString
()
const
;
228
[[nodiscard]]
bool
isBlob
()
const
;
229
[[nodiscard]]
bool
isList
()
const
;
230
[[nodiscard]]
bool
isDict
()
const
;
231
234
[[nodiscard]] std::optional<bool>
getIfBool
()
const
;
237
[[nodiscard]] std::optional<Integer>
getIfInt
()
const
;
240
[[nodiscard]] std::optional<double>
getIfDouble
()
const
;
241
[[nodiscard]]
String
*
getIfString
();
242
[[nodiscard]]
Blob
*
getIfBlob
();
243
[[nodiscard]]
Dict
*
getIfDict
();
244
[[nodiscard]]
List
*
getIfList
();
245
246
[[nodiscard]]
bool
getBool
()
const
;
247
[[nodiscard]]
Integer
getInt
()
const
;
248
[[nodiscard]]
double
getDouble
()
const
;
249
[[nodiscard]]
const
String
&
getString
()
const
;
250
[[nodiscard]]
String
&
getString
();
251
[[nodiscard]]
const
Blob
&
getBlob
()
const
;
252
[[nodiscard]]
Blob
&
getBlob
();
253
[[nodiscard]]
const
List
&
getList
()
const
;
254
[[nodiscard]]
List
&
getList
();
255
[[nodiscard]]
const
Dict
&
getDict
()
const
;
256
[[nodiscard]]
Dict
&
getDict
();
257
258
[[nodiscard]] std::string
toString
(
size_t
level = 4) const noexcept;
259
260
bool
operator==(const
Value
&rhs) const;
261
bool
operator!=(const
Value
&rhs) const;
262
263
void
toString
(text::StringBuilder &string_builder,
size_t
level) const noexcept;
264
265
private:
266
std::variant<
Null
,
bool
,
Integer
,
double
,
String
,
Blob
,
List
,
Dict
>
data
;
267
};
268
269
template<>
270
struct text::overload::
Formatter
<
Value
> {
271
size_t
level = 4;
272
273
bool
parse
(
const
std::string &args) {
274
char
*end;
275
level =
static_cast<
size_t
>
(std::strtol(args.c_str(), &end, 10));
276
if
(level == 0) {
277
return
false
;
278
}
279
return
true
;
280
}
281
282
void
format
(
FmtCtx
&ctx,
Value
&value)
const
{
283
value.
toString
(ctx.
builder
, level);
284
}
285
};
286
287
}
// namespace sese
sese
util
Value.h
生成于 2024年 十一月 4日 星期一 09:58:03 , 为 Sese Framework使用
1.11.0