GnuCash c935c2f+
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Friends
KvpValue Struct Reference

Implements KvpValue using boost::variant. More...

#include <kvp-value.hpp>

Public Types

enum  Type {
  INVALID = -1 , INT64 = 1 , DOUBLE , NUMERIC ,
  STRING , GUID , TIME64 , PLACEHOLDER_DONT_USE ,
  GLIST , FRAME , GDATE
}
 

Public Member Functions

 KvpValueImpl (KvpValueImpl const &) noexcept
 Performs a deep copy.
 
KvpValueImploperator= (const KvpValueImpl &) noexcept
 
 KvpValueImpl (KvpValueImpl &&b) noexcept
 Move.
 
KvpValueImploperator= (KvpValueImpl &&b) noexcept
 
template<typename T >
 KvpValueImpl (T) noexcept
 Create a KvpValue containing the passed in item.
 
 ~KvpValueImpl () noexcept
 Performs a deep delete.
 
KvpValueImpladd (KvpValueImpl *) noexcept
 Adds another value to this KvpValueImpl.
 
KvpValueImpl::Type get_type () const noexcept
 
std::string to_string () const noexcept
 
std::string to_string (std::string const &prefix) const noexcept
 
template<typename T >
get () const noexcept
 
template<typename T >
const T * get_ptr () const noexcept
 
template<typename T >
void set (T) noexcept
 

Friends

int compare (const KvpValueImpl &, const KvpValueImpl &) noexcept
 

Detailed Description

Implements KvpValue using boost::variant.

Capable of holding the following types:

Definition at line 52 of file kvp-value.hpp.

Member Enumeration Documentation

◆ Type

Enumerator
INT64 

QOF_TYPE_INT64 gint64.

DOUBLE 

QOF_TYPE_DOUBLE gdouble.

NUMERIC 

QOF_TYPE_NUMERIC.

STRING 

QOF_TYPE_STRING gchar*.

GUID 

QOF_TYPE_GUID.

TIME64 

QOF_TYPE_DATE.

GLIST 

no QOF equivalent.

FRAME 

no QOF equivalent.

GDATE 

no QOF equivalent.

Definition at line 55 of file kvp-value.hpp.

56 {
57 INVALID = -1,
58 INT64 = 1,
59 DOUBLE,
60 NUMERIC,
61 STRING,
62 GUID,
63 TIME64,
64 PLACEHOLDER_DONT_USE, /* Replaces KVP_TYPE_BINARY */
65 GLIST,
66 FRAME,
67 GDATE,
68 };
@ 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

Constructor & Destructor Documentation

◆ KvpValueImpl() [1/3]

KvpValue::KvpValueImpl ( KvpValueImpl const &  other)
noexcept

Performs a deep copy.

Definition at line 34 of file kvp-value.cpp.

35{
36 duplicate(other);
37}

◆ KvpValueImpl() [2/3]

KvpValue::KvpValueImpl ( KvpValueImpl &&  b)
noexcept

Move.

The old object's datastore is set to int64_t 0.

Definition at line 46 of file kvp-value.cpp.

47{
48 datastore = b.datastore;
49 b.datastore = INT64_C(0);
50}

◆ KvpValueImpl() [3/3]

template<typename T >
KvpValue::KvpValueImpl ( newvalue)
noexcept

Create a KvpValue containing the passed in item.

Note that for pointer types const char*, KvpFrame*, GncGUID*, and GList* the KvpValue takes ownership of the object and will delete/free it when the KvpValue is destroyed. That means these objects must be allocated in the free store or heap as follows:

  • const char*: GLib string allocation, e.g. g_strdup()/
  • KvpFrame*: operator new
  • GncGUID*: guid_new() or guid_copy()
  • GList*: Uses g_list_free(), so it's up to classes using this to empty the list before destroying the KvpValue.

Definition at line 150 of file kvp-value.hpp.

150 :
151 datastore(newvalue)
152{
153}

◆ ~KvpValueImpl()

KvpValue::~KvpValueImpl ( )
noexcept

Performs a deep delete.

The contents of this KvpValueImpl are also deleted.

Definition at line 366 of file kvp-value.cpp.

367{
369 boost::apply_visitor(d, datastore);
370}

Member Function Documentation

◆ add()

KvpValueImpl * KvpValue::add ( KvpValueImpl val)
noexcept

Adds another value to this KvpValueImpl.

If this KvpValueImpl represents a collection (GList), the new value is added to the collection and this is returned.

Otherwise, a new KvpValueImpl representing a collection is created, this and the new value are added to it, and it is returned.

Definition at line 60 of file kvp-value.cpp.

61{
62 /* If already a glist here, just append */
63 if (this->datastore.type() == type_id<GList*>())
64 {
65 GList * list = boost::get<GList*>(datastore);
66 datastore = g_list_append (list, val);
67 return this;
68 }
69 /* If some other value, convert it to a glist */
70 GList *list = nullptr;
71
72 list = g_list_append (list, this);
73 list = g_list_append (list, val);
74 return new KvpValueImpl(list);
75}
Implements KvpValue using boost::variant.
Definition kvp-value.hpp:53

◆ get()

template<typename T >
T KvpValue::get ( ) const
noexcept

Definition at line 156 of file kvp-value.hpp.

157{
158 if (this->datastore.type() != boost::typeindex::type_id<T>()) return {};
159 return boost::get<T>(datastore);
160}

◆ get_ptr()

template<typename T >
const T * KvpValue::get_ptr ( ) const
noexcept

Definition at line 163 of file kvp-value.hpp.

164{
165 if (this->datastore.type() != typeid(T)) return nullptr;
166 return boost::get<T>(&datastore);
167}

◆ get_type()

KvpValue::Type KvpValue::get_type ( ) const
noexcept

Definition at line 78 of file kvp-value.cpp.

79{
80 if (datastore.type() == type_id<int64_t>())
81 return KvpValue::Type::INT64;
82 else if (datastore.type() == type_id<double>())
83 return KvpValue::Type::DOUBLE;
84 else if (datastore.type() == type_id<gnc_numeric>())
85 return KvpValue::Type::NUMERIC;
86 else if (datastore.type() == type_id<const gchar *>())
87 return KvpValue::Type::STRING;
88 else if (datastore.type() == type_id<GncGUID *>())
89 return KvpValue::Type::GUID;
90 else if (datastore.type() == type_id<Time64>())
91 return KvpValue::Type::TIME64;
92 else if (datastore.type() == type_id<GList *>())
93 return KvpValue::Type::GLIST;
94 else if (datastore.type() == type_id<KvpFrameImpl *>())
95 return KvpValue::Type::FRAME;
96 else if (datastore.type() == type_id<GDate>())
97 return KvpValue::Type::GDATE;
98
99 return KvpValue::Type::INVALID;
100}

◆ operator=() [1/2]

KvpValueImpl & KvpValue::operator= ( const KvpValueImpl other)
noexcept

Definition at line 40 of file kvp-value.cpp.

41{
42 duplicate(other);
43 return *this;
44}

◆ operator=() [2/2]

KvpValueImpl & KvpValue::operator= ( KvpValueImpl &&  b)
noexcept

Definition at line 53 of file kvp-value.cpp.

54{
55 std::swap (datastore, b.datastore);
56 return *this;
57}

◆ set()

template<typename T >
void KvpValue::set ( val)
noexcept

Definition at line 170 of file kvp-value.hpp.

171{
172 this->datastore = val;
173}

◆ to_string() [1/2]

std::string KvpValue::to_string ( ) const
noexcept

Definition at line 201 of file kvp-value.cpp.

202{
203 return to_string("");
204}

◆ to_string() [2/2]

std::string KvpValue::to_string ( std::string const &  prefix) const
noexcept

Definition at line 189 of file kvp-value.cpp.

190{
191 if (this->datastore.type() == type_id<KvpFrame*>())
192 return this->get<KvpFrame*>()->to_string(prefix);
193 std::ostringstream ret;
194 to_string_visitor visitor {ret};
195 boost::apply_visitor(visitor, datastore);
196 /*We still use g_strdup since the return value will be freed by g_free*/
197 return prefix + ret.str();
198}

Friends And Related Symbol Documentation

◆ compare

int compare ( const KvpValueImpl one,
const KvpValueImpl two 
)
friend

Definition at line 311 of file kvp-value.cpp.

312{
313 auto type1 = one.get_type();
314 auto type2 = two.get_type();
315
316 if (type1 != type2)
317 return type1 < type2 ? -1 : 1;
318
319 compare_visitor comparer;
320 return boost::apply_visitor(comparer, one.datastore, two.datastore);
321}

The documentation for this struct was generated from the following files: