-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathdatafmt.cpp
328 lines (286 loc) · 9.28 KB
/
datafmt.cpp
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/**
* Copyright - See the COPYRIGHT that is included with this distribution.
* pvxs is distributed subject to a Software License Agreement found
* in file LICENSE that is included with this distribution.
*/
#include "dataimpl.h"
namespace pvxs {
namespace {
struct FmtDelta {
std::ostream& strm;
const Value::Fmt& fmt;
void field(const std::string& prefix, const Value& val, bool verytop)
{
if(verytop && !val.isMarked(false))
return;
strm<<indent{}<<prefix;
if(!verytop)
strm<<" ";
strm<<val.type().name();
if(val.type()==TypeCode::Struct && !val.id().empty()) {
strm<<" \""<<escape(val.id())<<'"';
}
if(fmt._showValue) {
auto store = Value::Helper::store_ptr(val);
switch(val.storageType()) {
case StoreType::Real: strm<<" = "<<store->as<double>(); break;
case StoreType::Integer: strm<<" = "<<store->as<int64_t>(); break;
case StoreType::UInteger: strm<<" = "<<store->as<uint64_t>(); break;
case StoreType::Bool: strm<<" = "<<(store->as<bool>() ? "true" : "false"); break;
case StoreType::String: strm<<" = \""<<escape(store->as<std::string>())<<"\""; break;
case StoreType::Array: {
auto& varr = store->as<shared_array<const void>>();
if(varr.original_type()!=ArrayType::Value) {
strm<<" = "<<varr.format().limit(fmt._limit);
}
}
break;
default:
break;
}
}
strm<<"\n";
switch(val.type().code) {
case TypeCode::Union:
case TypeCode::Any: {
auto uval = val.as<Value>();
std::string cprefix(prefix);
cprefix+="->";
if(val.type()==TypeCode::Union) {
auto desc = Value::Helper::desc(val);
auto udesc = Value::Helper::desc(uval);
for(auto idx : range(desc->members.size())) {
if(udesc == &desc->members[idx]) {
cprefix+=desc->miter[idx].first;
break;
}
}
}
top(cprefix, uval, false);
}
break;
case TypeCode::StructA:
case TypeCode::UnionA:
case TypeCode::AnyA: {
auto rawval = val.as<const shared_array<const void>>();
if(rawval.original_type()==ArrayType::Null) {
} else if(rawval.original_type()==ArrayType::Value) {
auto aval = rawval.castTo<const Value>();
for(auto idx : range(aval.size())) {
std::ostringstream strm;
strm<<indent{}<<prefix<<'['<<idx<<']';
top(strm.str(), aval[idx], false);
}
} else {
throw std::logic_error("Value[] is not");
}
}
break;
default:
break;
}
}
void top(const std::string& prefix, const Value& val, bool verytop)
{
if(!val) {
strm<<indent{}<<prefix;
if(!verytop)
strm<<' ';
strm<<"null\n";
return;
}
field(prefix, val, verytop);
if(val.type()==TypeCode::Struct) {
for(const auto& fld : val.imarked()) {
std::string cprefix(prefix);
if(!verytop)
cprefix += '.';
cprefix += val.nameOf(fld);
field(cprefix, fld, false);
}
}
}
};
struct FmtTree {
std::ostream& strm;
const Value::Fmt& fmt;
void show_value(const Value& fld) {
const auto type(fld.type());
assert(type.kind()!=Kind::Compound);
switch(type.code) {
case TypeCode::Bool:
strm<<(fld.as<bool>() ? "true" : "false");
return;
case TypeCode::Int8:
case TypeCode::Int16:
case TypeCode::Int32:
case TypeCode::Int64:
strm<<fld.as<int64_t>();
return;
case TypeCode::UInt8:
case TypeCode::UInt16:
case TypeCode::UInt32:
case TypeCode::UInt64:
strm<<fld.as<uint64_t>();
return;
case TypeCode::Float32:
case TypeCode::Float64:
strm<<fld.as<double>();
return;
case TypeCode::String:
strm<<"\""<<escape(fld.as<std::string>())<<"\"";
return;
case TypeCode::BoolA:
case TypeCode::Int8A:
case TypeCode::Int16A:
case TypeCode::Int32A:
case TypeCode::Int64A:
case TypeCode::UInt8A:
case TypeCode::UInt16A:
case TypeCode::UInt32A:
case TypeCode::UInt64A:
case TypeCode::Float32A:
case TypeCode::Float64A:
case TypeCode::StringA:
{
auto varr = fld.as<shared_array<const void>>();
strm<<varr.format().limit(fmt._limit);
}
return;
case TypeCode::Any:
case TypeCode::Struct:
case TypeCode::Union:
case TypeCode::StructA:
case TypeCode::UnionA:
case TypeCode::AnyA:
assert(false);
break;
default:
strm<<"!!Invalid TypeCode!! "<<int(type.code)<<"\n";
return;
}
}
// each invocation emits at least one complete line
void show(const Value& fld, const std::string& member) {
// caller should indent{}
if(!fld) {
strm<<"null\n";
return;
}
const auto type(fld.type());
strm<<type;
{
auto id(fld.id());
if(!id.empty())
strm<<" \""<<id<<"\"";
}
if(type.kind()!=Kind::Compound) {
if(!member.empty())
strm<<' '<<member;
if(fmt._showValue) {
strm<<" = ";
show_value(fld);
}
strm<<"\n";
return;
}
if(type==TypeCode::Any
|| (!fmt._showValue && type==TypeCode::AnyA)
|| (fmt._showValue && type==TypeCode::Union)) {
// any NAME = VAL
// union NAME.MEM TYPE = VAL
Value val;
if(fmt._showValue)
val = fld.as<Value>();
if(!member.empty())
strm<<' '<<member;
if(type==TypeCode::Union && val) { // implied _showValue
auto mem(fld.nameOf(val));
strm<<'.'<<mem;
}
if(fmt._showValue) {
strm<<" ";
show(val, std::string());
} else {
strm<<"\n";
}
return;
} else if(type==TypeCode::Struct
|| type==TypeCode::Union // && !_showValue
|| (!fmt._showValue && type==TypeCode::StructA)
|| (!fmt._showValue && type==TypeCode::UnionA))
{
// struct "id" { ... } NAME
Value def;
if(!type.isarray()) {
def = fld;
} else { // StructA, UnionA
//def = fld.allocMember(); // can't call directly due to const
auto desc(Value::Helper::desc(fld));
auto store(Value::Helper::store(fld));
decltype (store->top->desc) fld(store->top->desc, desc->members.data());
def = Value::Helper::build(fld); // not connection to fld (not parent)
}
strm<<" {";
bool first = true;
{
Indented I(strm);
for(auto mem : def.ichildren()) {
auto mname(def.nameOf(mem));
if(first)
strm<<'\n';
strm<<indent{};
show(mem, mname);
first = false;
}
}
if(!first)
strm<<indent{};
strm<<'}';
if(!member.empty())
strm<<' '<<member;
strm<<"\n";
} else {
// struct[] NAME = [ ... ]
if(!member.empty())
strm<<' '<<member;
auto arr(fld.as<shared_array<const Value>>());
strm<<" = {"<<arr.size()<<"}[";
size_t shown = 0u;
{
Indented I(strm);
for(auto& elem : arr) {
if(!shown)
strm<<'\n';
strm<<indent{};
if(fmt._limit && shown>=fmt._limit) {
strm<<"...\n";
break;
}
show(elem, std::string());
shown++;
}
}
if(shown)
strm<<indent{};
strm<<"]\n";
}
}
};
} // namespace
std::ostream& operator<<(std::ostream& strm, const Value::Fmt& fmt)
{
switch (fmt._format) {
case Value::Fmt::Tree:
strm<<indent{};
FmtTree{strm, fmt}.show(*fmt.top, std::string());
break;
case Value::Fmt::Delta:
FmtDelta{strm, fmt}.top("", *fmt.top, true);
break;
default:
strm<<"<Unknown Value format()>\n";
}
return strm;
}
}