GnuCash c935c2f+
Loading...
Searching...
No Matches
kvp-value.hpp
1/********************************************************************\
2 * kvp-value.hpp -- Implements a key-value frame system *
3 * Copyright (C) 2014 Aaron Laws *
4 * *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License as *
7 * published by the Free Software Foundation; either version 2 of *
8 * the License, or (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License*
16 * along with this program; if not, contact: *
17 * *
18 * Free Software Foundation Voice: +1-617-542-5942 *
19 * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
20 * Boston, MA 02110-1301, USA gnu@gnu.org *
21 * *
22\********************************************************************/
23
24#ifndef GNC_KVP_VALUE_TYPE
25#define GNC_KVP_VALUE_TYPE
26
27#include <config.h>
28#include "qof.h"
29
30#include <cstdint>
31#include <boost/variant.hpp>
32
33//Must be a struct because it's exposed to C so that it can in turn be
34//translated to/from Scheme.
53{
54 public:
55 enum Type
56 {
57 INVALID = -1,
58 INT64 = 1,
64 PLACEHOLDER_DONT_USE, /* Replaces KVP_TYPE_BINARY */
68 };
69
73 KvpValueImpl(KvpValueImpl const &) noexcept;
74 KvpValueImpl& operator=(const KvpValueImpl&) noexcept;
75
79 KvpValueImpl(KvpValueImpl && b) noexcept;
80 KvpValueImpl& operator=(KvpValueImpl && b) noexcept;
81
94 template <typename T>
95 KvpValueImpl(T) noexcept;
96
102 ~KvpValueImpl() noexcept;
103
115 KvpValueImpl * add (KvpValueImpl *) noexcept;
116
117 KvpValueImpl::Type get_type() const noexcept;
118
119 std::string to_string() const noexcept;
120 std::string to_string(std::string const & prefix) const noexcept;
121
122 template <typename T>
123 T get() const noexcept;
124 template <typename T>
125 const T* get_ptr() const noexcept;
126
127 template <typename T>
128 void set(T) noexcept;
129
130 friend int compare(const KvpValueImpl &, const KvpValueImpl &) noexcept;
131
132 private:
133 void duplicate(const KvpValueImpl&) noexcept;
134 boost::variant<
135 int64_t,
136 double,
137 gnc_numeric,
138 const char*,
139 GncGUID *,
140 Time64,
141 GList *,
142 KvpFrame *,
143 GDate> datastore;
144};
145
146int
147compare(const KvpValueImpl *, const KvpValue *) noexcept;
149template <typename T>
150KvpValueImpl::KvpValueImpl(T newvalue) noexcept:
151 datastore(newvalue)
152{
153}
154
155template <typename T> T
156KvpValueImpl::get() const noexcept
157{
158 if (this->datastore.type() != boost::typeindex::type_id<T>()) return {};
159 return boost::get<T>(datastore);
160}
161
162template <typename T> const T*
163KvpValueImpl::get_ptr() const noexcept
164{
165 if (this->datastore.type() != typeid(T)) return nullptr;
166 return boost::get<T>(&datastore);
167}
168
169template <typename T> void
170KvpValueImpl::set(T val) noexcept
171{
172 this->datastore = val;
173}
181void gvalue_from_kvp_value (const KvpValue *kval, GValue* val);
182
187KvpValue* kvp_value_from_gvalue (const GValue *gval);
188
192#endif
KvpValue * kvp_value_from_gvalue(const GValue *gval)
Convert a gvalue into a kvpvalue.
void gvalue_from_kvp_value(const KvpValue *kval, GValue *val)
Convert a kvp_value into a GValue.
The type used to store guids in C.
Definition guid.h:75
Implements KvpValue using boost::variant.
Definition kvp-value.hpp:53
KvpValueImpl * add(KvpValueImpl *) noexcept
Adds another value to this KvpValueImpl.
Definition kvp-value.cpp:60
~KvpValueImpl() noexcept
Performs a deep delete.
@ INT64
QOF_TYPE_INT64 gint64.
Definition kvp-value.hpp:58
@ TIME64
QOF_TYPE_DATE.
Definition kvp-value.hpp:63
@ GUID
QOF_TYPE_GUID.
Definition kvp-value.hpp:62
@ STRING
QOF_TYPE_STRING gchar*.
Definition kvp-value.hpp:61
@ GDATE
no QOF equivalent.
Definition kvp-value.hpp:67
@ GLIST
no QOF equivalent.
Definition kvp-value.hpp:65
@ NUMERIC
QOF_TYPE_NUMERIC.
Definition kvp-value.hpp:60
@ FRAME
no QOF equivalent.
Definition kvp-value.hpp:66
@ DOUBLE
QOF_TYPE_DOUBLE gdouble.
Definition kvp-value.hpp:59