/
kvp-frame.cpp
457 lines (422 loc) · 13.5 KB
/
kvp-frame.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
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/********************************************************************
* kvp_frame.cpp -- Implements a key-value frame system *
* Copyright (C) 2000 Bill Gribble *
* Copyright (C) 2001,2003 Linas Vepstas <linas@linas.org> *
* *
* 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 2 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, contact: *
* *
* Free Software Foundation Voice: +1-617-542-5942 *
* 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
* Boston, MA 02110-1301, USA gnu@gnu.org *
* *
********************************************************************/
extern "C"
{
#include <config.h>
#include "qof.h"
#include <glib.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
}
#include "kvp-value.hpp"
#include "kvp-frame.hpp"
#include <typeinfo>
#include <sstream>
#include <algorithm>
#include <vector>
#include <numeric>
/* This static indicates the debugging module that this .o belongs to. */
static QofLogModule log_module = "qof.kvp";
static const char delim = '/';
KvpFrameImpl::KvpFrameImpl(const KvpFrameImpl & rhs) noexcept
{
std::for_each(rhs.m_valuemap.begin(), rhs.m_valuemap.end(),
[this](const map_type::value_type & a)
{
auto key = qof_string_cache_insert(a.first);
auto val = new KvpValueImpl(*a.second);
this->m_valuemap.insert({key,val});
}
);
}
KvpFrameImpl::~KvpFrameImpl() noexcept
{
std::for_each(m_valuemap.begin(), m_valuemap.end(),
[](const map_type::value_type &a){
qof_string_cache_remove(a.first);
delete a.second;
}
);
m_valuemap.clear();
}
KvpFrame *
KvpFrame::get_child_frame_or_nullptr (Path const & path) noexcept
{
if (!path.size ())
return this;
auto key = path.front ();
auto map_iter = m_valuemap.find (key.c_str ());
if (map_iter == m_valuemap.end ())
return nullptr;
auto child = map_iter->second->get <KvpFrame *> ();
if (!child)
return nullptr;
Path send;
std::copy (path.begin () + 1, path.end (), std::back_inserter (send));
return child->get_child_frame_or_nullptr (send);
}
KvpFrame *
KvpFrame::get_child_frame_or_create (Path const & path) noexcept
{
if (!path.size ())
return this;
auto key = path.front ();
auto spot = m_valuemap.find (key.c_str ());
if (spot == m_valuemap.end () || spot->second->get_type () != KvpValue::Type::FRAME)
delete set_impl (key.c_str (), new KvpValue {new KvpFrame});
Path send;
std::copy (path.begin () + 1, path.end (), std::back_inserter (send));
auto child_val = m_valuemap.at (key.c_str ());
auto child = child_val->get <KvpFrame *> ();
return child->get_child_frame_or_create (send);
}
KvpValue *
KvpFrame::set_impl (std::string const & key, KvpValue * value) noexcept
{
KvpValue * ret {};
auto spot = m_valuemap.find (key.c_str ());
if (spot != m_valuemap.end ())
{
qof_string_cache_remove (spot->first);
ret = spot->second;
m_valuemap.erase (spot);
}
if (value)
{
auto cachedkey = static_cast <char const *> (qof_string_cache_insert (key.c_str ()));
m_valuemap.emplace (cachedkey, value);
}
return ret;
}
KvpValue *
KvpFrameImpl::set (Path path, KvpValue* value) noexcept
{
if (path.empty())
return nullptr;
auto key = path.back ();
path.pop_back ();
auto target = get_child_frame_or_nullptr (path);
if (!target)
return nullptr;
return target->set_impl (key, value);
}
KvpValue *
KvpFrameImpl::set_path (Path path, KvpValue* value) noexcept
{
auto key = path.back();
path.pop_back();
auto target = get_child_frame_or_create (path);
if (!target)
return nullptr;
return target->set_impl (key, value);
}
KvpValue *
KvpFrameImpl::get_slot (Path path) noexcept
{
auto key = path.back();
path.pop_back();
auto target = get_child_frame_or_nullptr (path);
if (!target)
return nullptr;
auto spot = target->m_valuemap.find (key.c_str ());
if (spot != target->m_valuemap.end ())
return spot->second;
return nullptr;
}
std::string
KvpFrameImpl::to_string() const noexcept
{
return to_string("");
}
std::string
KvpFrameImpl::to_string(std::string const & prefix) const noexcept
{
if (!m_valuemap.size())
return prefix;
std::ostringstream ret;
std::for_each(m_valuemap.begin(), m_valuemap.end(),
[this,&ret,&prefix](const map_type::value_type &a)
{
std::string new_prefix {prefix};
if (a.first)
{
new_prefix += a.first;
new_prefix += "/";
}
if (a.second)
ret << a.second->to_string(new_prefix) << "\n";
else
ret << new_prefix << "(null)\n";
}
);
return ret.str();
}
std::vector<std::string>
KvpFrameImpl::get_keys() const noexcept
{
std::vector<std::string> ret;
ret.reserve (m_valuemap.size());
std::for_each(m_valuemap.begin(), m_valuemap.end(),
[&ret](const KvpFrameImpl::map_type::value_type &a)
{
ret.push_back(a.first);
}
);
return ret;
}
int compare(const KvpFrameImpl * one, const KvpFrameImpl * two) noexcept
{
if (one && !two) return 1;
if (!one && two) return -1;
if (!one && !two) return 0;
return compare(*one, *two);
}
/**
* If the first KvpFrameImpl has an item that the second does not, 1 is returned.
* The first item within the two KvpFrameImpl that is not similar, that comparison is returned.
* If all the items within the first KvpFrameImpl match items within the second, but the
* second has more elements, -1 is returned.
* Otherwise, 0 is returned.
*/
int compare(const KvpFrameImpl & one, const KvpFrameImpl & two) noexcept
{
for (const auto & a : one.m_valuemap)
{
auto otherspot = two.m_valuemap.find(a.first);
if (otherspot == two.m_valuemap.end())
{
return 1;
}
auto comparison = compare(a.second,otherspot->second);
if (comparison != 0)
return comparison;
}
if (one.m_valuemap.size() < two.m_valuemap.size())
return -1;
return 0;
}
static void
gvalue_list_from_kvp_value (KvpValue *kval, gpointer pList)
{
GList **gvlist = NULL;
GValue *gval = gvalue_from_kvp_value (kval);
gvlist = (GList**)pList;
if (G_VALUE_TYPE (gval))
*gvlist = g_list_prepend (*gvlist, gval);
}
static void
kvp_value_list_from_gvalue (GValue *gval, gpointer pList)
{
GList **kvplist = (GList**)pList;
KvpValue *kvp;
if (!(gval && G_VALUE_TYPE (gval)))
return;
kvp = kvp_value_from_gvalue (gval);
*kvplist = g_list_prepend (*kvplist, kvp);
}
GValue*
gvalue_from_kvp_value (const KvpValue *kval)
{
GValue *val;
gnc_numeric num;
Time64 tm;
GDate gdate;
if (kval == NULL) return NULL;
val = g_slice_new0 (GValue);
switch (kval->get_type())
{
case KvpValue::Type::INT64:
g_value_init (val, G_TYPE_INT64);
g_value_set_int64 (val, kval->get<int64_t>());
break;
case KvpValue::Type::DOUBLE:
g_value_init (val, G_TYPE_DOUBLE);
g_value_set_double (val, kval->get<double>());
break;
case KvpValue::Type::NUMERIC:
g_value_init (val, GNC_TYPE_NUMERIC);
num = kval->get<gnc_numeric>();
g_value_set_boxed (val, &num);
break;
case KvpValue::Type::STRING:
g_value_init (val, G_TYPE_STRING);
g_value_set_string (val, kval->get<const char*>());
break;
case KvpValue::Type::GUID:
g_value_init (val, GNC_TYPE_GUID);
g_value_set_boxed (val, kval->get<GncGUID*>());
break;
case KvpValue::Type::TIME64:
g_value_init (val, GNC_TYPE_TIME64);
tm = kval->get<Time64>();
g_value_set_boxed (val, &tm);
break;
case KvpValue::Type::GDATE:
g_value_init (val, G_TYPE_DATE);
gdate = kval->get<GDate>();
g_value_set_boxed (val, &gdate);
break;
case KvpValue::Type::GLIST:
{
GList *gvalue_list = NULL;
GList *kvp_list = kval->get<GList*>();
g_list_foreach (kvp_list, (GFunc)gvalue_list_from_kvp_value,
&gvalue_list);
g_value_init (val, GNC_TYPE_VALUE_LIST);
gvalue_list = g_list_reverse (gvalue_list);
g_value_set_boxed (val, gvalue_list);
break;
}
/* No transfer of KVP frames outside of QofInstance-derived classes! */
case KvpValue::Type::FRAME:
PWARN ("Error! Attempt to transfer KvpFrame!");
default:
PWARN ("Error! Invalid KVP Transfer Request!");
g_slice_free (GValue, val);
val = NULL;
break;
}
return val;
}
KvpValue*
kvp_value_from_gvalue (const GValue *gval)
{
KvpValue *val = NULL;
GType type;
if (gval == NULL)
return NULL;
type = G_VALUE_TYPE (gval);
g_return_val_if_fail (G_VALUE_TYPE (gval), NULL);
if (type == G_TYPE_INT64)
val = new KvpValue(g_value_get_int64 (gval));
else if (type == G_TYPE_DOUBLE)
val = new KvpValue(g_value_get_double (gval));
else if (type == G_TYPE_BOOLEAN)
{
auto bval = g_value_get_boolean(gval);
if (bval)
val = new KvpValue(g_strdup("true"));
}
else if (type == GNC_TYPE_NUMERIC)
val = new KvpValue(*(gnc_numeric*)g_value_get_boxed (gval));
else if (type == G_TYPE_STRING)
{
auto string = g_value_get_string(gval);
if (string != nullptr)
val = new KvpValue(g_strdup(string));
}
else if (type == GNC_TYPE_GUID)
{
auto boxed = g_value_get_boxed(gval);
if (boxed != nullptr)
val = new KvpValue(guid_copy(static_cast<GncGUID*>(boxed)));
}
else if (type == GNC_TYPE_TIME64)
val = new KvpValue(*(Time64*)g_value_get_boxed (gval));
else if (type == G_TYPE_DATE)
val = new KvpValue(*(GDate*)g_value_get_boxed (gval));
else if (type == GNC_TYPE_VALUE_LIST)
{
GList *gvalue_list = (GList*)g_value_get_boxed (gval);
GList *kvp_list = NULL;
g_list_foreach (gvalue_list, (GFunc)kvp_value_list_from_gvalue,
&kvp_list);
kvp_list = g_list_reverse (kvp_list);
val = new KvpValue(kvp_list);
// g_list_free_full (gvalue_list, (GDestroyNotify)g_value_unset);
// gvalue_list = NULL;
}
else
PWARN ("Error! Don't know how to make a KvpValue from a %s",
G_VALUE_TYPE_NAME (gval));
return val;
}
/* The following are required for using KvpValue GLists as GValues */
static void
gnc_gvalue_copy (GValue *src, gpointer uData)
{
GList **new_list = (GList**)uData;
GValue *dest = g_value_init (g_slice_new0 (GValue), G_VALUE_TYPE (src));
g_value_copy (src, dest);
*new_list = g_list_prepend(*new_list, dest);
}
void
gnc_gvalue_free (GValue *val)
{
if (val == NULL || ! G_IS_VALUE (val)) return;
g_value_unset (val);
g_slice_free (GValue, val);
}
static GList*
gnc_value_list_copy (GList *list)
{
GList *new_list = NULL;
g_list_foreach (list, (GFunc)gnc_gvalue_copy, &new_list);
new_list = g_list_reverse (new_list);
return new_list;
}
static void
gnc_value_list_free (GList *list)
{
g_list_free_full (list, (GDestroyNotify)gnc_gvalue_free);
}
GType
gnc_value_list_get_type (void)
{
static GType type = 0;
if (type == 0)
{
type = g_boxed_type_register_static ("gnc_value_list",
(GBoxedCopyFunc)gnc_value_list_copy,
(GBoxedFreeFunc)gnc_value_list_free);
}
return type;
}
void
KvpFrame::flatten_kvp_impl(std::vector <std::string> path, std::vector <KvpEntry> & entries) const noexcept
{
for (auto const & entry : m_valuemap)
{
std::vector<std::string> new_path {path};
new_path.push_back("/");
if (entry.second->get_type() == KvpValue::Type::FRAME)
{
new_path.push_back(entry.first);
entry.second->get<KvpFrame*>()->flatten_kvp_impl(new_path, entries);
}
else
{
new_path.emplace_back (entry.first);
entries.emplace_back (new_path, entry.second);
}
}
}
std::vector <KvpEntry>
KvpFrame::flatten_kvp(void) const noexcept
{
std::vector <KvpEntry> ret;
flatten_kvp_impl({}, ret);
return ret;
}