GnuCash c935c2f+
Loading...
Searching...
No Matches
Files | Macros | Typedefs

This file defines an API that adds types to the GncGUID's. More...

Files

file  qofid.h
 QOF entity type identification system.
 

Macros

#define QOF_ID_NONE   NULL
 
#define QOF_ID_NULL   "null"
 
#define QOF_ID_BOOK   "Book"
 
#define QOF_ID_SESSION   "Session"
 
#define QSTRCMP   g_strcmp0
 
#define QOF_CHECK_TYPE(obj, type)
 return TRUE if object is of the given type
 
#define QOF_CHECK_CAST(obj, e_type, c_type)
 cast object to the indicated type, print error message if its bad

 

Typedefs

typedef const gchar * QofIdType
 QofIdType declaration.
 
typedef const gchar * QofIdTypeConst
 QofIdTypeConst declaration.
 

Collections of Entities

QofCollection declaration

Parameters
e_typeQofIdType
is_dirtygboolean
hash_of_entitiesGHashTable
datagpointer, place where object class can hang arbitrary data
typedef void(* QofInstanceForeachCB) (QofInstance *, gpointer user_data)
 Callback type for qof_collection_foreach.
 
QofCollection * qof_collection_new (QofIdType type)
 create a new collection of entities of type
 
guint qof_collection_count (const QofCollection *col)
 return the number of entities in the collection.
 
void qof_collection_destroy (QofCollection *col)
 destroy the collection
 
QofIdType qof_collection_get_type (const QofCollection *)
 return the type that the collection stores
 
QofInstanceqof_collection_lookup_entity (const QofCollection *, const GncGUID *)
 Find the entity going only from its guid.
 
void qof_collection_foreach_sorted (const QofCollection *col, QofInstanceForeachCB cb_func, gpointer user_data, GCompareFunc sort_fn)
 Call the callback for each entity in the collection.
 
void qof_collection_foreach (const QofCollection *, QofInstanceForeachCB, gpointer user_data)
 
gpointer qof_collection_get_data (const QofCollection *col)
 Store and retrieve arbitrary object-defined data.
 
void qof_collection_set_data (QofCollection *col, gpointer user_data)
 
gboolean qof_collection_is_dirty (const QofCollection *col)
 Return value of 'dirty' flag on collection.
 

QOF_TYPE_COLLECT: Linking one entity to many of one type

Note
These are NOT the same as the main collections in the book.

QOF_TYPE_COLLECT is a secondary collection, used to select entities of one object type as references of another entity.

See also
QOF_TYPE_CHOICE.
gboolean qof_collection_add_entity (QofCollection *coll, QofInstance *ent)
 Add an entity to a QOF_TYPE_COLLECT.
 
void qof_collection_remove_entity (QofInstance *ent)
 
gint qof_collection_compare (QofCollection *target, QofCollection *merge)
 Compare two secondary collections.
 

Detailed Description

This file defines an API that adds types to the GncGUID's.

GncGUID's with types can be used to identify and reference typed entities.

The idea here is that a GncGUID can be used to uniquely identify some thing. By adding a type, one can then talk about the type of thing identified. By adding a collection, one can then work with a handle to a collection of things of a given type, each uniquely identified by a given ID. QOF Entities can be used independently of any other part of the system. In particular, Entities can be useful even if one is not using the Query and Object parts of the QOF system.

Identifiers are globally-unique and permanent, i.e., once an entity has been assigned an identifier, it retains that same identifier for its lifetime. Identifiers can be encoded as hex strings.

GncGUID Identifiers are 'typed' with strings. The native ids used by QOF are defined below.

  1. An id with type QOF_ID_NONE does not refer to any entity.
  2. An id with type QOF_ID_NULL does not refer to any entity, and will never refer to any entity. =# An identifier with any other type may refer to an actual entity, but that is not guaranteed as that entity does not have to exist within the current book. (See ::PARTIAL_QOFBOOK). Also, creating a new entity from a data source involves creating a temporary GncGUID and then setting the value from the data source. If an id does refer to an entity, the type of the entity will match the type of the identifier.

If you have a type name, and you want to have a way of finding a collection that is associated with that type, then you must use Books.

Entities can refer to other entities as well as to the basic QOF types, using the qofclass parameters.

Macro Definition Documentation

◆ QOF_CHECK_CAST

#define QOF_CHECK_CAST (   obj,
  e_type,
  c_type 
)
Value:
( \
QOF_CHECK_TYPE((obj),(e_type)) ? \
(c_type *) (obj) : \
(c_type *) ({ \
g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, \
"Error: Bad QofInstance at %s:%d", __FILE__, __LINE__); \
(obj); \
}))

cast object to the indicated type, print error message if its bad

Definition at line 107 of file qofid.h.

109 : \
110 (c_type *) ({ \
111 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, \
112 "Error: Bad QofInstance at %s:%d", __FILE__, __LINE__); \
113 (obj); \
114 }))

◆ QOF_CHECK_TYPE

#define QOF_CHECK_TYPE (   obj,
  type 
)
Value:
(((obj) != NULL) && \
(0 == QSTRCMP((type),(((QofInstance *)(obj))->e_type))))

return TRUE if object is of the given type

Definition at line 102 of file qofid.h.

◆ QOF_ID_BOOK

#define QOF_ID_BOOK   "Book"

Definition at line 96 of file qofid.h.

◆ QOF_ID_NONE

#define QOF_ID_NONE   NULL

Definition at line 93 of file qofid.h.

◆ QOF_ID_NULL

#define QOF_ID_NULL   "null"

Definition at line 94 of file qofid.h.

◆ QOF_ID_SESSION

#define QOF_ID_SESSION   "Session"

Definition at line 97 of file qofid.h.

◆ QSTRCMP

#define QSTRCMP   g_strcmp0

Definition at line 99 of file qofid.h.

Typedef Documentation

◆ QofIdType

typedef const gchar* QofIdType

QofIdType declaration.

Definition at line 80 of file qofid.h.

◆ QofIdTypeConst

typedef const gchar* QofIdTypeConst

QofIdTypeConst declaration.

Definition at line 82 of file qofid.h.

◆ QofInstanceForeachCB

typedef void(* QofInstanceForeachCB) (QofInstance *, gpointer user_data)

Callback type for qof_collection_foreach.

Definition at line 146 of file qofid.h.

Function Documentation

◆ qof_collection_add_entity()

gboolean qof_collection_add_entity ( QofCollection *  coll,
QofInstance ent 
)

Add an entity to a QOF_TYPE_COLLECT.

Note
These are NOT the same as the main collections in the book.

Entities can be freely added and merged across these secondary collections, they will not be removed from the original collection as they would by using ::qof_instance_insert_entity or ::qof_instance_remove_entity.

Definition at line 109 of file qofid.cpp.

110{
111 QofInstance *e;
112 const GncGUID *guid;
113
114 e = NULL;
115 if (!coll || !ent)
116 {
117 return FALSE;
118 }
119 guid = qof_instance_get_guid(ent);
120 if (guid_equal(guid, guid_null()))
121 {
122 return FALSE;
123 }
124 g_return_val_if_fail (coll->e_type == ent->e_type, FALSE);
125 e = qof_collection_lookup_entity(coll, guid);
126 if ( e != NULL )
127 {
128 return FALSE;
129 }
130 g_hash_table_insert (coll->hash_of_entities, (gpointer)guid, ent);
131 return TRUE;
132}
QofInstance * qof_collection_lookup_entity(const QofCollection *col, const GncGUID *guid)
Find the entity going only from its guid.
Definition qofid.cpp:209
const GncGUID * guid_null(void)
Returns a GncGUID which is guaranteed to never reference any entity.
Definition guid.cpp:165
gboolean guid_equal(const GncGUID *guid_1, const GncGUID *guid_2)
Given two GUIDs, return TRUE if they are non-NULL and equal.
Definition guid.cpp:237
const GncGUID * qof_instance_get_guid(gconstpointer inst)
Return the GncGUID of this instance.
QofIdType e_type
Entity type.
Definition qofinstance.h:75
The type used to store guids in C.
Definition guid.h:75

◆ qof_collection_compare()

gint qof_collection_compare ( QofCollection *  target,
QofCollection *  merge 
)

Compare two secondary collections.

Performs a deep comparison of the collections. Each QofInstance in each collection is looked up in the other collection, via the GncGUID.

Returns
0 if the collections are identical or both are NULL otherwise -1 if target is NULL or either collection contains an entity with an invalid GncGUID or if the types of the two collections do not match, or +1 if merge is NULL or if any entity exists in one collection but not in the other.

Definition at line 171 of file qofid.cpp.

172{
173 gint value;
174
175 value = 0;
176 if (!target && !merge)
177 {
178 return 0;
179 }
180 if (target == merge)
181 {
182 return 0;
183 }
184 if (!target && merge)
185 {
186 return -1;
187 }
188 if (target && !merge)
189 {
190 return 1;
191 }
192 if (target->e_type != merge->e_type)
193 {
194 return -1;
195 }
196 qof_collection_set_data(target, &value);
197 qof_collection_foreach(merge, collection_compare_cb, target);
198 value = *(gint*)qof_collection_get_data(target);
199 if (value == 0)
200 {
201 qof_collection_set_data(merge, &value);
202 qof_collection_foreach(target, collection_compare_cb, merge);
203 value = *(gint*)qof_collection_get_data(merge);
204 }
205 return value;
206}
gpointer qof_collection_get_data(const QofCollection *col)
Store and retrieve arbitrary object-defined data.
Definition qofid.cpp:266

◆ qof_collection_count()

guint qof_collection_count ( const QofCollection *  col)

return the number of entities in the collection.

Definition at line 221 of file qofid.cpp.

222{
223 guint c;
224
225 c = g_hash_table_size(col->hash_of_entities);
226 return c;
227}

◆ qof_collection_destroy()

void qof_collection_destroy ( QofCollection *  col)

destroy the collection

Definition at line 64 of file qofid.cpp.

65{
66 delete col;
67}

◆ qof_collection_foreach()

void qof_collection_foreach ( const QofCollection *  col,
QofInstanceForeachCB  cb_func,
gpointer  user_data 
)

Definition at line 303 of file qofid.cpp.

305{
306 qof_collection_foreach_sorted (col, cb_func, user_data, nullptr);
307}
void qof_collection_foreach_sorted(const QofCollection *col, QofInstanceForeachCB cb_func, gpointer user_data, GCompareFunc sort_fn)
Call the callback for each entity in the collection.
Definition qofid.cpp:283

◆ qof_collection_foreach_sorted()

void qof_collection_foreach_sorted ( const QofCollection *  col,
QofInstanceForeachCB  cb_func,
gpointer  user_data,
GCompareFunc  sort_fn 
)

Call the callback for each entity in the collection.

Definition at line 283 of file qofid.cpp.

285{
286 GList *entries;
287
288 g_return_if_fail (col);
289 g_return_if_fail (cb_func);
290
291 PINFO("Hash Table size of %s before is %d", col->e_type, g_hash_table_size(col->hash_of_entities));
292
293 entries = g_hash_table_get_values (col->hash_of_entities);
294 if (sort_fn)
295 entries = g_list_sort (entries, sort_fn);
296 g_list_foreach (entries, (GFunc)cb_func, user_data);
297 g_list_free (entries);
298
299 PINFO("Hash Table size of %s after is %d", col->e_type, g_hash_table_size(col->hash_of_entities));
300}
#define PINFO(format, args...)
Print an informational note.
Definition qoflog.h:256

◆ qof_collection_get_data()

gpointer qof_collection_get_data ( const QofCollection *  col)

Store and retrieve arbitrary object-defined data.

XXX We need to add a callback for when the collection is being destroyed, so that the user has a chance to clean up anything that was put in the 'data' member here.

Definition at line 266 of file qofid.cpp.

267{
268 return col ? col->data : NULL;
269}

◆ qof_collection_get_type()

QofIdType qof_collection_get_type ( const QofCollection *  col)

return the type that the collection stores

Definition at line 73 of file qofid.cpp.

74{
75 return col->e_type;
76}

◆ qof_collection_is_dirty()

gboolean qof_collection_is_dirty ( const QofCollection *  col)

Return value of 'dirty' flag on collection.

Definition at line 232 of file qofid.cpp.

233{
234 return col ? col->is_dirty : FALSE;
235}

◆ qof_collection_lookup_entity()

QofInstance * qof_collection_lookup_entity ( const QofCollection *  col,
const GncGUID guid 
)

Find the entity going only from its guid.

Definition at line 209 of file qofid.cpp.

210{
211 QofInstance *ent;
212 g_return_val_if_fail (col, NULL);
213 if (guid == NULL) return NULL;
214 ent = static_cast<QofInstance*>(g_hash_table_lookup (col->hash_of_entities,
215 guid));
216 if (ent != NULL && qof_instance_get_destroying(ent)) return NULL;
217 return ent;
218}
gboolean qof_instance_get_destroying(gconstpointer ptr)
Retrieve the flag that indicates whether or not this object is about to be destroyed.

◆ qof_collection_new()

QofCollection * qof_collection_new ( QofIdType  type)

create a new collection of entities of type

Definition at line 58 of file qofid.cpp.

59{
60 return new QofCollection (type);
61}

◆ qof_collection_remove_entity()

void qof_collection_remove_entity ( QofInstance ent)

Definition at line 81 of file qofid.cpp.

82{
83 QofCollection *col;
84 const GncGUID *guid;
85
86 if (!ent) return;
88 if (!col) return;
89 guid = qof_instance_get_guid(ent);
90 g_hash_table_remove (col->hash_of_entities, guid);
91 qof_instance_set_collection(ent, NULL);
92}
QofCollection * qof_instance_get_collection(gconstpointer ptr)
Return the collection this instance belongs to.

◆ qof_collection_set_data()

void qof_collection_set_data ( QofCollection *  col,
gpointer  user_data 
)

Definition at line 272 of file qofid.cpp.

273{
274 if (col)
275 {
276 col->data = user_data;
277 }
278}