GnuCash  5.6-38-g01bffa485a+
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 <boost/variant.hpp>
31 
32 //Must be a struct because it's exposed to C so that it can in turn be
33 //translated to/from Scheme.
52 {
53  public:
54  enum Type
55  {
56  INVALID = -1,
57  INT64 = 1,
61  GUID,
63  PLACEHOLDER_DONT_USE, /* Replaces KVP_TYPE_BINARY */
67  };
68 
72  KvpValueImpl(KvpValueImpl const &) noexcept;
73  KvpValueImpl& operator=(const KvpValueImpl&) noexcept;
74 
78  KvpValueImpl(KvpValueImpl && b) noexcept;
79  KvpValueImpl& operator=(KvpValueImpl && b) noexcept;
80 
93  template <typename T>
94  KvpValueImpl(T) noexcept;
95 
101  ~KvpValueImpl() noexcept;
102 
114  KvpValueImpl * add (KvpValueImpl *) noexcept;
115 
116  KvpValueImpl::Type get_type() const noexcept;
117 
118  std::string to_string() const noexcept;
119  std::string to_string(std::string const & prefix) const noexcept;
120 
121  template <typename T>
122  T get() const noexcept;
123  template <typename T>
124  const T* get_ptr() const noexcept;
125 
126  template <typename T>
127  void set(T) noexcept;
128 
129  friend int compare(const KvpValueImpl &, const KvpValueImpl &) noexcept;
130 
131  private:
132  void duplicate(const KvpValueImpl&) noexcept;
133  boost::variant<
134  int64_t,
135  double,
136  gnc_numeric,
137  const char*,
138  GncGUID *,
139  Time64,
140  GList *,
141  KvpFrame *,
142  GDate> datastore;
143 };
144 
145 int
146 compare(const KvpValueImpl *, const KvpValue *) noexcept;
148 template <typename T>
149 KvpValueImpl::KvpValueImpl(T newvalue) noexcept:
150  datastore(newvalue)
151 {
152 }
153 
154 template <typename T> T
155 KvpValueImpl::get() const noexcept
156 {
157  if (this->datastore.type() != boost::typeindex::type_id<T>()) return {};
158  return boost::get<T>(datastore);
159 }
160 
161 template <typename T> const T*
162 KvpValueImpl::get_ptr() const noexcept
163 {
164  if (this->datastore.type() != typeid(T)) return nullptr;
165  return boost::get<T>(&datastore);
166 }
167 
168 template <typename T> void
169 KvpValueImpl::set(T val) noexcept
170 {
171  this->datastore = val;
172 }
180 GValue* gvalue_from_kvp_value (const KvpValue *kval, GValue* val = nullptr);
181 
186 KvpValue* kvp_value_from_gvalue (const GValue *gval);
187 
191 #endif
no QOF equivalent.
Definition: kvp-value.hpp:65
STL namespace.
KvpValue * kvp_value_from_gvalue(const GValue *gval)
Convert a gvalue into a kvpvalue.
Definition: kvp-frame.cpp:285
QOF_TYPE_STRING gchar*.
Definition: kvp-value.hpp:60
QOF_TYPE_DATE.
Definition: kvp-value.hpp:62
QOF_TYPE_NUMERIC.
Definition: kvp-value.hpp:59
QOF_TYPE_DOUBLE gdouble.
Definition: kvp-value.hpp:58
KvpValueImpl * add(KvpValueImpl *) noexcept
Adds another value to this KvpValueImpl.
Definition: kvp-value.cpp:60
Implements KvpValue using boost::variant.
Definition: kvp-value.hpp:51
KvpValueImpl(KvpValueImpl const &) noexcept
Performs a deep copy.
Definition: kvp-value.cpp:34
no QOF equivalent.
Definition: kvp-value.hpp:66
no QOF equivalent.
Definition: kvp-value.hpp:64
GValue * gvalue_from_kvp_value(const KvpValue *kval, GValue *val=nullptr)
Convert a kvp_value into a GValue.
Definition: kvp-frame.cpp:236
~KvpValueImpl() noexcept
Performs a deep delete.
Definition: kvp-value.cpp:366
QOF_TYPE_GUID.
Definition: kvp-value.hpp:61
The type used to store guids in C.
Definition: guid.h:75
QOF_TYPE_INT64 gint64.
Definition: kvp-value.hpp:57