GnuCash  5.6-150-g038405b370+
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,
62  GUID,
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 
146 int
147 compare(const KvpValueImpl *, const KvpValue *) noexcept;
149 template <typename T>
150 KvpValueImpl::KvpValueImpl(T newvalue) noexcept:
151  datastore(newvalue)
152 {
153 }
154 
155 template <typename T> T
156 KvpValueImpl::get() const noexcept
157 {
158  if (this->datastore.type() != boost::typeindex::type_id<T>()) return {};
159  return boost::get<T>(datastore);
160 }
161 
162 template <typename T> const T*
163 KvpValueImpl::get_ptr() const noexcept
164 {
165  if (this->datastore.type() != typeid(T)) return nullptr;
166  return boost::get<T>(&datastore);
167 }
168 
169 template <typename T> void
170 KvpValueImpl::set(T val) noexcept
171 {
172  this->datastore = val;
173 }
181 void gvalue_from_kvp_value (const KvpValue *kval, GValue* val);
182 
187 KvpValue* kvp_value_from_gvalue (const GValue *gval);
188 
192 #endif
no QOF equivalent.
Definition: kvp-value.hpp:66
void gvalue_from_kvp_value(const KvpValue *kval, GValue *val)
Convert a kvp_value into a GValue.
Definition: kvp-frame.cpp:237
STL namespace.
KvpValue * kvp_value_from_gvalue(const GValue *gval)
Convert a gvalue into a kvpvalue.
Definition: kvp-frame.cpp:280
QOF_TYPE_STRING gchar*.
Definition: kvp-value.hpp:61
QOF_TYPE_DATE.
Definition: kvp-value.hpp:63
QOF_TYPE_NUMERIC.
Definition: kvp-value.hpp:60
QOF_TYPE_DOUBLE gdouble.
Definition: kvp-value.hpp:59
KvpValueImpl * add(KvpValueImpl *) noexcept
Adds another value to this KvpValueImpl.
Definition: kvp-value.cpp:60
Implements KvpValue using boost::variant.
Definition: kvp-value.hpp:52
KvpValueImpl(KvpValueImpl const &) noexcept
Performs a deep copy.
Definition: kvp-value.cpp:34
no QOF equivalent.
Definition: kvp-value.hpp:67
no QOF equivalent.
Definition: kvp-value.hpp:65
~KvpValueImpl() noexcept
Performs a deep delete.
Definition: kvp-value.cpp:366
QOF_TYPE_GUID.
Definition: kvp-value.hpp:62
The type used to store guids in C.
Definition: guid.h:75
QOF_TYPE_INT64 gint64.
Definition: kvp-value.hpp:58