forked from RefPerSys/RefPerSys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrbufdict_rps.cc
387 lines (353 loc) · 12.2 KB
/
strbufdict_rps.cc
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/****************************************************************
* file strbufdict_rps.cc
* SPDX-License-Identifier: GPL-3.0-or-later
*
* Description:
* This file is part of the Reflective Persistent System.
*
* It has the string buffer code, and code for dictionnaries
* associating strings to values.
*
* Author(s):
* Basile Starynkevitch <basile@starynkevitch.net>
* Abhishek Chakravarti <abhishek@taranjali.org>
* Nimesh Neema <nimeshneema@gmail.com>
*
* © Copyright 2019 - 2021 The Reflective Persistent System Team
* team@refpersys.org & http://refpersys.org/
*
* License:
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
#include "refpersys.hh"
Rps_PayloadStrBuf::Rps_PayloadStrBuf(Rps_ObjectZone*obz)
: Rps_Payload(Rps_Type::PaylStrBuf, obz),
strbuf_out(),
strbuf_indent(0),
strbuf_transient(false)
{
} // end PayloadStrBuf::Rps_PayloadStrBuf
Rps_PayloadStrBuf::~Rps_PayloadStrBuf()
{
} // end Rps_PayloadStrBuf::~Rps_PayloadStrBuf
void
Rps_PayloadStrBuf::gc_mark([[maybe_unused]] Rps_GarbageCollector& gc) const
{
} // end Rps_PayloadStrBuf::gc_mark
void
Rps_PayloadStrBuf::dump_scan(Rps_Dumper*du) const
{
RPS_ASSERT(du != nullptr);
return;
} // end Rps_PayloadStrBuf::dump_scan
Rps_ObjectRef
Rps_PayloadStrBuf::make_string_buffer_object(Rps_CallFrame*callframe, Rps_ObjectRef obclassarg, Rps_ObjectRef obspacearg)
{
RPS_LOCALFRAME(/*descr:*/ RPS_ROOT_OB(_1rfASGBBbFz02VUsMw), //"rps_serve_onion_expanded_stream"∈rps_routine
/*prev:*/callframe,
Rps_ObjectRef obsbuf;
Rps_ObjectRef obclass;
Rps_ObjectRef obspace;
);
_f.obclass = obclassarg;
_f.obspace = obspacearg;
if (!_f.obclass)
_f.obclass = Rps_PayloadStrBuf::the_string_buffer_class();
if (_f.obclass != Rps_PayloadStrBuf::the_string_buffer_class()
&& !_f.obclass->is_subclass_of(Rps_PayloadStrBuf::the_string_buffer_class()))
{
std::ostringstream outmsg;
outmsg << "make_string_buffer_object:" << _f.obclass->oid() << " not a subclass of string_buffer" << std::endl;
throw std::runtime_error(outmsg.str());
}
_f.obsbuf = Rps_ObjectRef::make_object(&_, _f.obclass, _f.obspace);
[[maybe_unused]] auto paylsbuf = _f.obsbuf->put_new_plain_payload<Rps_PayloadStrBuf>();
return _f.obsbuf;
} // end of Rps_PayloadStrBuf::make_string_buffer_object
void
Rps_PayloadStrBuf::dump_json_content(Rps_Dumper*du, Json::Value&jv) const
{
/// see function rpsldpy_string_buffer
RPS_ASSERT(du != nullptr);
RPS_ASSERT(jv.type() == Json::objectValue);
if (strbuf_transient)
return;
const std::string&str = strbuf_out.str();
auto eol = str.find('\n');
if (eol > 0)
{
auto begit = str.begin();
Json::Value jarr(Json::arrayValue);
while (eol > 0)
{
auto eolit = begit+eol;
std::string linstr = str.substr(begit-str.begin(),eolit-str.begin());
jarr.append(Json::Value(linstr));
begit = eolit;
eol = str.find(begit+1-str.begin(), '\n');
}
jv["strbuf_lines"] = jarr;
}
else
{
jv["strbuf_string"] = Json::Value(str);
};
jv["strbuf_indent"] = Json::Value(strbuf_indent);
return;
} // end Rps_PayloadStrBuf::dump_json_content
//// loading of Rps_PayloadStrBuf; see above Rps_PayloadStrBuf::dump_json_content
void
rpsldpy_string_buffer(Rps_ObjectZone*obz, Rps_Loader*ld, const Json::Value& jv, Rps_Id spacid, unsigned lineno)
{
RPS_ASSERT(obz != nullptr);
RPS_ASSERT(ld != nullptr);
RPS_ASSERT(obz->get_payload() == nullptr);
RPS_ASSERT(jv.type() == Json::objectValue);
RPS_DEBUG_LOG(LOAD,"start rpsldpy_string_buffer obz=" << obz
<< " jv=" << jv
<< " spacid=" << spacid
<< " lineno=" << lineno);
auto paylsbuf = obz->put_new_plain_payload<Rps_PayloadStrBuf>();
RPS_ASSERT(paylsbuf);
if (jv.isMember("strbuf_lines"))
{
auto jarr = jv["strbuf_lines"];
if (jarr.isArray())
{
int arrlen = jarr.size();
for (int ix=0; ix<arrlen; ix++)
{
auto& jcomp = jarr[ix];
if (jcomp.isString())
paylsbuf->strbuf_out << jcomp.asString();
}
}
}
else if (jv.isMember("strbuf_string"))
{
std::string str = jv["strbuf_string"].asString();
paylsbuf->strbuf_out << str;
}
else
RPS_WARNOUT("rpsldpy_string_buffer: incorrect jv=" << jv
<< "in spacid=" << spacid
<< " lineno=" << lineno);
paylsbuf->strbuf_indent = jv["strbuf_indent"].asInt();
} // end of rpsldpy_string_buffer
void
Rps_PayloadStrBuf::clear_buffer()
{
/// clear the buffer used by strbuf_out, which is a std::ostringstream
strbuf_out.str("");
} // end Rps_PayloadStrBuf::clear_buffer
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
Rps_PayloadStringDict::Rps_PayloadStringDict(Rps_ObjectZone*obz)
: Rps_Payload(Rps_Type::PaylStringDict, obz),
dict_map(),
dict_is_transient(false)
{
} // end PayloadStringDict::Rps_PayloadStringDict
Rps_PayloadStringDict::~Rps_PayloadStringDict()
{
dict_map.clear();
dict_is_transient = false;
} // end Rps_PayloadStringDict::~Rps_PayloadStringDict
void
Rps_PayloadStringDict::gc_mark(Rps_GarbageCollector&gc) const
{
for (auto it : dict_map)
{
it.second.gc_mark(gc);
}
} // end Rps_PayloadStringDict::gc_mark
void
Rps_PayloadStringDict::dump_scan(Rps_Dumper*du) const
{
RPS_ASSERT(du != nullptr);
if (dict_is_transient)
return;
for (auto it : dict_map)
{
rps_dump_scan_value(du,it.second,0);
}
} // end Rps_PayloadStringDict::dump_scan
void
Rps_PayloadStringDict::dump_json_content(Rps_Dumper*du, Json::Value&jv) const
{
/// see function rpsldpy_string_dictionary below
RPS_ASSERT(du != nullptr);
RPS_ASSERT(jv.type() == Json::objectValue);
if (dict_is_transient)
return;
Json::Value jarr(Json::arrayValue);
for (auto it : dict_map)
{
if (!rps_is_dumpable_value(du,it.second))
continue;
Json::Value jcur = rps_dump_json_value(du, it.second);
Json::Value jent(Json::objectValue);
jent["str"] = it.first;
jent["val"] = jcur;
jarr.append(jent);
}
jv["payload"] = "string_dictionary";
jv["dictionary"] = jarr;
} // end Rps_PayloadStringDict::dump_json_content
//// loading of Rps_PayloadStringDict; see above Rps_PayloadStringDict::dump_json_content
void
rpsldpy_string_dictionary(Rps_ObjectZone*obz, Rps_Loader*ld, const Json::Value& jv, Rps_Id spacid, unsigned lineno)
{
RPS_ASSERT(obz != nullptr);
RPS_ASSERT(ld != nullptr);
RPS_ASSERT(obz->get_payload() == nullptr);
RPS_ASSERT(jv.type() == Json::objectValue);
if (!jv.isMember("dictionary"))
{
RPS_FATALOUT("rpsldpy_string_dictionary: object " << obz->oid()
<< " in space " << spacid << " lineno#" << lineno
<< " has incomplete payload"
<< std::endl
<< " jv " << (jv));
}
auto payldict = obz->put_new_plain_payload<Rps_PayloadStringDict>();
Json::Value jarr = jv["dictionary"];
if (!jarr.isArray())
RPS_FATALOUT("rpsldpy_string_dictionary: object " << obz->oid()
<< " in space " << spacid << " lineno#" << lineno
<< " has bad dictionary "
<< std::endl
<< jarr);
unsigned nbent = jarr.size();
for (int entix=0; entix<(int)nbent; entix++)
{
Json::Value jcurent = jarr[entix];
if (!jcurent.isObject()
|| !jcurent.isMember("str")
|| !jcurent.isMember("val"))
continue;
std::string curstr = jcurent["str"].asString();
if (curstr.empty())
continue;
payldict->add(curstr, Rps_Value(jcurent["val"],ld));
}
} // end rpsldpy_string_dictionary
void
Rps_PayloadStringDict::add(const std::string&str, Rps_Value val)
{
if (!str.empty() && !val.is_empty())
dict_map.insert({str,val});
else if (!str.empty() && !val)
dict_map.erase(str);
} // end Rps_PayloadStringDict::add
Rps_Value
Rps_PayloadStringDict::find(const std::string&str) const
{
if (!str.empty())
{
auto it = dict_map.find(str);
if (it != dict_map.end())
return it->second;
}
return nullptr;
} // end Rps_PayloadStringDict::find
void
Rps_PayloadStringDict::remove(const std::string&str)
{
dict_map.erase(str);
} // end Rps_PayloadStringDict::remove
void
Rps_PayloadStringDict::set_transient(bool transient)
{
dict_is_transient = transient;
} // end PayloadStringDict::set_transient
void
Rps_PayloadStringDict::iterate_with_callframe(Rps_CallFrame*callerframe, const std::function <bool(Rps_CallFrame*,const std::string&,const Rps_Value)>& stopfun)
{
RPS_ASSERT(callerframe == nullptr || callerframe->is_good_call_frame());
RPS_ASSERT(stopfun);
for (auto it : dict_map)
{
if (stopfun(callerframe, it.first, it.second))
return;
}
} // end Rps_PayloadStringDict::iterate_with_callframe
void
Rps_PayloadStringDict::iterate_with_data(void*data, const std::function <bool(void*,const std::string&,const Rps_Value)>& stopfun)
{
RPS_ASSERT(stopfun);
for (auto it : dict_map)
{
if (stopfun(data, it.first, it.second))
return;
}
} // end Rps_PayloadStringDict::iterate_with_callframe
void
Rps_PayloadStringDict::iterate_apply(Rps_CallFrame*callerframe, Rps_Value closarg)
{
RPS_LOCALFRAME(/*descr:*/nullptr,
/*prev:*/callerframe,
Rps_ObjectRef obown;
Rps_Value closv;
Rps_Value curstrv;
Rps_Value curval;
);
RPS_ASSERT(callerframe == nullptr || callerframe->is_good_call_frame());
if (!closarg.is_closure())
return;
_f.obown = owner();
if (!_f.obown)
return;
_f.closv = closarg;
for (auto it : dict_map)
{
_f.curstrv = Rps_StringValue(it.first);
_f.curval = it.second;
Rps_TwoValues pair = Rps_ClosureValue(_f.closv).apply3(&_, _f.obown, _f.curstrv, _f.curval);
if (!pair)
return;
_f.curstrv = nullptr;
_f.curval = nullptr;
}
} // end Rps_PayloadStringDict::iterate_apply
Rps_ObjectRef
Rps_PayloadStringDict::the_string_dictionary_class(void)
{
return RPS_ROOT_OB(_3FztYBKABxZ02DUPRm);
} // end Rps_PayloadStringDict::the_string_dictionary_class
Rps_ObjectRef
Rps_PayloadStringDict::make_string_dictionary_object(Rps_CallFrame*callframe, Rps_ObjectRef obclassarg, Rps_ObjectRef obspacearg)
{
RPS_ASSERT(!callframe || callframe->is_good_call_frame());
RPS_LOCALFRAME(the_string_dictionary_class(),
callframe,
Rps_ObjectRef obstrdict;
Rps_ObjectRef obclass;
Rps_ObjectRef obspace;
);
_f.obclass = obclassarg;
_f.obspace = obspacearg;
if (!_f.obclass)
_f.obclass = the_string_dictionary_class();
if (_f.obclass != the_string_dictionary_class()
&& !Rps_Value(_f.obclass).is_subclass_of(&_,
the_string_dictionary_class()))
throw std::runtime_error("invalid class for make_string_dictionary_object");
_f.obstrdict = Rps_ObjectRef::make_object(&_, _f.obclass, _f.obspace);
auto payldict = _f.obstrdict->put_new_plain_payload<Rps_PayloadStringDict>();
RPS_ASSERT(payldict);
return _f.obstrdict;
} // end of Rps_PayloadStringDict::make_string_dictionary_object
//// end of file strbufdict_rps.cc