-
Notifications
You must be signed in to change notification settings - Fork 760
/
kvp-value.cpp
386 lines (341 loc) · 10.5 KB
/
kvp-value.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
/********************************************************************\
* kvp-value.cpp -- Implements a key-value frame system *
* Copyright (C) 2014 Aaron Laws *
* *
* 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 *
* *
\********************************************************************/
#include "kvp-value.hpp"
#include "kvp-frame.hpp"
#include <cmath>
#include <sstream>
#include <iomanip>
#include <stdexcept>
using boost::typeindex::type_id;
KvpValueImpl::KvpValueImpl(KvpValueImpl const & other) noexcept
{
duplicate(other);
}
KvpValueImpl&
KvpValueImpl::operator=(KvpValueImpl const & other) noexcept
{
duplicate(other);
return *this;
}
KvpValueImpl::KvpValueImpl(KvpValueImpl && b) noexcept
{
datastore = b.datastore;
b.datastore = INT64_C(0);
}
KvpValueImpl&
KvpValueImpl::operator=(KvpValueImpl && b) noexcept
{
std::swap (datastore, b.datastore);
return *this;
}
KvpValueImpl *
KvpValueImpl::add(KvpValueImpl * val) noexcept
{
/* If already a glist here, just append */
if (this->datastore.type() == type_id<GList*>())
{
GList * list = boost::get<GList*>(datastore);
datastore = g_list_append (list, val);
return this;
}
/* If some other value, convert it to a glist */
GList *list = nullptr;
list = g_list_append (list, this);
list = g_list_append (list, val);
return new KvpValueImpl(list);
}
KvpValue::Type
KvpValueImpl::get_type() const noexcept
{
if (datastore.type() == type_id<int64_t>())
return KvpValue::Type::INT64;
else if (datastore.type() == type_id<double>())
return KvpValue::Type::DOUBLE;
else if (datastore.type() == type_id<gnc_numeric>())
return KvpValue::Type::NUMERIC;
else if (datastore.type() == type_id<const gchar *>())
return KvpValue::Type::STRING;
else if (datastore.type() == type_id<GncGUID *>())
return KvpValue::Type::GUID;
else if (datastore.type() == type_id<Time64>())
return KvpValue::Type::TIME64;
else if (datastore.type() == type_id<GList *>())
return KvpValue::Type::GLIST;
else if (datastore.type() == type_id<KvpFrameImpl *>())
return KvpValue::Type::FRAME;
else if (datastore.type() == type_id<GDate>())
return KvpValue::Type::GDATE;
return KvpValue::Type::INVALID;
}
struct to_string_visitor : boost::static_visitor<void>
{
std::ostringstream & output;
to_string_visitor(std::ostringstream & val) : output(val){}
void operator()(int64_t val)
{
output << val << " (64-bit int)";
}
void operator()(KvpFrame* val)
{
output << val->to_string();
}
void operator()(GDate val)
{
output << std::setw(4) << g_date_get_year(&val) << '-';
output << std::setw(2) << g_date_get_month(&val) << '-';
output << std::setw(2) << g_date_get_day(&val);
output << " (gdate)";
}
void operator()(GList * val)
{
output << "KVP_VALUE_GLIST(";
output << "[ ";
/*Since val is passed by value, we can modify it*/
for (;val; val = val->next)
{
auto realvalue = static_cast<const KvpValue *>(val->data);
output << ' ' << realvalue->to_string() << ',';
}
output << " ]";
output << ")";
}
void operator()(Time64 val)
{
char tmp1[MAX_DATE_LENGTH + 1] {};
gnc_time64_to_iso8601_buff (val.t, tmp1);
output << tmp1 << " (time64)";
}
void operator()(gnc_numeric val)
{
auto tmp1 = gnc_numeric_to_string(val);
if (tmp1)
{
output << tmp1;
g_free(tmp1);
}
else
{
output << "(null)";
}
output << " (gnc_numeric)";
}
void operator()(GncGUID * val)
{
char guidstr[GUID_ENCODING_LENGTH+1];
if (val)
{
guid_to_string_buff(val,guidstr);
output << guidstr;
}
else
{
output << "(null)";
}
output << " (guid)";
}
void operator()(const char * val)
{
output << val << " (char *)";
}
void operator()(double val)
{
output << val << " (double)";
}
};
std::string
KvpValueImpl::to_string(std::string const & prefix) const noexcept
{
if (this->datastore.type() == type_id<KvpFrame*>())
return this->get<KvpFrame*>()->to_string(prefix);
std::ostringstream ret;
to_string_visitor visitor {ret};
boost::apply_visitor(visitor, datastore);
/*We still use g_strdup since the return value will be freed by g_free*/
return prefix + ret.str();
}
std::string
KvpValueImpl::to_string() const noexcept
{
return to_string("");
}
static int
kvp_glist_compare(const GList * list1, const GList * list2)
{
const GList *lp1;
const GList *lp2;
if (list1 == list2) return 0;
/* Nothing is always less than something */
if (!list1 && list2) return -1;
if (list1 && !list2) return 1;
lp1 = list1;
lp2 = list2;
while (lp1 && lp2)
{
KvpValue *v1 = (KvpValue *) lp1->data;
KvpValue *v2 = (KvpValue *) lp2->data;
gint vcmp = compare(v1, v2);
if (vcmp != 0) return vcmp;
lp1 = lp1->next;
lp2 = lp2->next;
}
if (!lp1 && lp2) return -1;
if (!lp2 && lp1) return 1;
return 0;
}
static GList *
kvp_glist_copy(const GList * list)
{
GList * retval = NULL;
GList * lptr;
if (!list) return retval;
/* Duplicate the backbone of the list (this duplicates the POINTERS
* to the values; we need to deep-copy the values separately) */
retval = g_list_copy((GList *) list);
/* This step deep-copies the values */
for (lptr = retval; lptr; lptr = lptr->next)
{
lptr->data = new KvpValue(*static_cast<KvpValue *>(lptr->data));
}
return retval;
}
struct compare_visitor : boost::static_visitor<int>
{
template <typename T, typename U>
int operator()( T& one, U& two) const
{
throw std::invalid_argument{"You may not compare objects of different type."};
}
template <typename T>
int operator()( T & one, T & two) const
{
/*This will work for any type that implements < and ==.*/
if (one < two)
return -1;
if (two < one)
return 1;
return 0;
}
};
template <> int compare_visitor::operator()(const char * const & one, const char * const & two) const
{
return strcmp(one, two);
}
template <> int compare_visitor::operator()(gnc_numeric const & one, gnc_numeric const & two) const
{
return gnc_numeric_compare(one, two);
}
template <> int compare_visitor::operator()(GncGUID * const & one, GncGUID * const & two) const
{
return guid_compare(one, two);
}
template <> int compare_visitor::operator()(Time64 const & one, Time64 const & two) const
{
return one.t < two.t ? -1 : one.t > two.t ? 1 : 0;
}
template <> int compare_visitor::operator()(GDate const & one, GDate const & two) const
{
return g_date_compare(&one,&two);
}
template <> int compare_visitor::operator()(GList * const & one, GList * const & two) const
{
return kvp_glist_compare(one, two);
}
template <> int compare_visitor::operator()(KvpFrame * const & one, KvpFrame * const & two) const
{
return compare(one, two);
}
template <> int compare_visitor::operator()(double const & one, double const & two) const
{
if (std::isnan(one) && std::isnan(two))
return 0;
if (one < two) return -1;
if (two < one) return 1;
return 0;
}
int compare(const KvpValueImpl & one, const KvpValueImpl & two) noexcept
{
auto type1 = one.get_type();
auto type2 = two.get_type();
if (type1 != type2)
return type1 < type2 ? -1 : 1;
compare_visitor comparer;
return boost::apply_visitor(comparer, one.datastore, two.datastore);
}
int
compare(const KvpValueImpl * one, const KvpValueImpl * two) noexcept
{
if (one == two) return 0;
if (one && !two) return 1;
if (!one && two) return -1;
assert (one && two); /* Silence a static analysis warning. */
return compare(*one, *two);
}
struct delete_visitor : boost::static_visitor<void>
{
template <typename T> void
operator()(T &) { /*do nothing*/ }
};
static void
destroy_value(void* item)
{
delete static_cast<KvpValue*>(item);
}
template <> void
delete_visitor::operator()(GList * & value)
{
g_list_free_full(value, destroy_value);
}
template <> void
delete_visitor::operator()(const gchar * & value)
{
g_free(const_cast<gchar *> (value));
}
template <> void
delete_visitor::operator()(GncGUID * & value)
{
guid_free(value);
}
template <> void
delete_visitor::operator()(KvpFrame * & value)
{
delete value;
}
KvpValueImpl::~KvpValueImpl() noexcept
{
delete_visitor d;
boost::apply_visitor(d, datastore);
}
void
KvpValueImpl::duplicate(const KvpValueImpl& other) noexcept
{
if (other.datastore.type() == type_id<const gchar *>())
this->datastore = const_cast<const gchar *>(g_strdup(other.get<const gchar *>()));
else if (other.datastore.type() == type_id<GncGUID*>())
this->datastore = guid_copy(other.get<GncGUID *>());
else if (other.datastore.type() == type_id<GList*>())
this->datastore = kvp_glist_copy(other.get<GList *>());
else if (other.datastore.type() == type_id<KvpFrame*>())
this->datastore = new KvpFrame(*other.get<KvpFrame *>());
else
this->datastore = other.datastore;
}