GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Typedefs

Typedefs

typedef struct gnc_price_lookup_s GNCPriceLookup
 
typedef GList PriceList
 

Constructors

GNCPricegnc_price_create (QofBook *book)
 
GNCPricegnc_price_clone (GNCPrice *p, QofBook *book)
 

Memory Management

void gnc_price_ref (GNCPrice *p)
 
void gnc_price_unref (GNCPrice *p)
 

Setters

All of the setters store copies of the data given, with the exception of the commodity field which just stores the pointer given. It is assumed that commodities are a global resource and are pointer unique.

Invocations of the setters should be wrapped with calls to gnc_price_begin_edit() and commit_edit(). The begin/commit calls help ensure that the local price db is synchronized with the backend.

void gnc_price_begin_edit (GNCPrice *p)
 
void gnc_price_commit_edit (GNCPrice *p)
 
void gnc_price_set_commodity (GNCPrice *p, gnc_commodity *c)
 
void gnc_price_set_currency (GNCPrice *p, gnc_commodity *c)
 
void gnc_price_set_time (GNCPrice *p, Timespec t)
 
void gnc_price_set_source (GNCPrice *p, const char *source)
 
void gnc_price_set_typestr (GNCPrice *p, const char *type)
 
void gnc_price_set_value (GNCPrice *p, gnc_numeric value)
 

Getters

All of the getters return data that's internal to the GNCPrice, not copies, so don't free these values.

GNCPricegnc_price_lookup (const GncGUID *guid, QofBook *book)
 
gnc_commoditygnc_price_get_commodity (const GNCPrice *p)
 
gnc_commoditygnc_price_get_currency (const GNCPrice *p)
 
Timespec gnc_price_get_time (const GNCPrice *p)
 
const char * gnc_price_get_source (const GNCPrice *p)
 
const char * gnc_price_get_typestr (const GNCPrice *p)
 
gnc_numeric gnc_price_get_value (const GNCPrice *p)
 
gboolean gnc_price_equal (const GNCPrice *p1, const GNCPrice *p2)
 
#define gnc_price_get_guid(X)   qof_entity_get_guid(QOF_INSTANCE(X))
 
#define gnc_price_return_guid(X)   (*(qof_entity_get_guid(QOF_INSTANCE(X))))
 
#define gnc_price_get_book(X)   qof_instance_get_book(QOF_INSTANCE(X))
 

Internal/Debugging

void gnc_price_print (GNCPrice *db, FILE *f, int indent)
 
void gnc_pricedb_print_contents (GNCPriceDB *db, FILE *f)
 

GNCPrice lists

The database communicates multiple prices in and out via gnc price lists. These are just time sorted GLists of GNCPrice pointers. Functions for manipulating these lists are provided. These functions are helpful in that they handle maintaining proper reference counts on behalf of the price list for every price being held in a given list. I.e. insert "refs" the prices being inserted, remove and destroy "unref" the prices that will no longer be referred to by the list.

gboolean gnc_price_list_insert (PriceList **prices, GNCPrice *p, gboolean check_dupl)
 
gboolean gnc_price_list_remove (PriceList **prices, GNCPrice *p)
 
void gnc_price_list_destroy (PriceList *prices)
 
gboolean gnc_price_list_equal (PriceList *prices1, PriceList *prices2)
 

Detailed Description

Each price in the database represents an "instantaneous" quote for a given commodity with respect to another commodity. For example, a given price might represent the value of LNUX in USD on 2001-02-03.

Fields:
Implementation Details:
Note
For source and type, NULL and the empty string are considered the same, so if one of these is "", then after a file save/restore, it might be NULL. Behave accordingly.

GNCPrices are reference counted. When you gnc_price_create one or clone it, the new price's count is set to 1. When you are finished with a price, call gnc_price_unref. If you hand the price pointer to some other code that needs to keep it, make sure it calls gnc_price_ref to indicate its interest in that price, and calls gnc_price_unref when it's finished with the price. For those unfamiliar with reference counting, basically each price stores an integer count which starts at 1 and is incremented every time someone calls gnc_price_ref. Conversely, the count is decremented every time someone calls gnc_price_unref. If the count ever reaches 0, the price is destroyed.

All of the getters return data that's internal to the GNCPrice, not copies, so don't free these values.

All of the setters store copies of the data given, with the exception of the commodity field which just stores the pointer given. It is assumed that commodities are a global resource and are pointer unique.

Function Documentation

GNCPrice* gnc_price_clone ( GNCPrice p,
QofBook book 
)

gnc_price_clone - returns a newly allocated price that's a content-wise duplicate of the given price, p. The returned clone will have a reference count of 1.

Definition at line 295 of file gnc-pricedb.c.

296 {
297  /* the clone doesn't belong to a PriceDB */
298  GNCPrice *new_p;
299 
300  g_return_val_if_fail (book, NULL);
301 
302  ENTER ("pr=%p", p);
303 
304  if (!p)
305  {
306  LEAVE (" ");
307  return NULL;
308  }
309 
310  new_p = gnc_price_create(book);
311  if (!new_p)
312  {
313  LEAVE (" ");
314  return NULL;
315  }
316 
317  qof_instance_copy_version(new_p, p);
318 
319  gnc_price_begin_edit(new_p);
320  /* never ever clone guid's */
321  gnc_price_set_commodity(new_p, gnc_price_get_commodity(p));
322  gnc_price_set_time(new_p, gnc_price_get_time(p));
323  gnc_price_set_source(new_p, gnc_price_get_source(p));
324  gnc_price_set_typestr(new_p, gnc_price_get_typestr(p));
325  gnc_price_set_value(new_p, gnc_price_get_value(p));
326  gnc_price_set_currency(new_p, gnc_price_get_currency(p));
327  gnc_price_commit_edit(new_p);
328  LEAVE (" ");
329  return(new_p);
330 }
GNCPrice * gnc_price_create(QofBook *book)
Definition: gnc-pricedb.c:236
#define ENTER(format, args...)
Definition: qoflog.h:261
#define LEAVE(format, args...)
Definition: qoflog.h:271
GNCPrice* gnc_price_create ( QofBook book)

gnc_price_create - returns a newly allocated and initialized price with a reference count of 1.

Definition at line 236 of file gnc-pricedb.c.

237 {
238  GNCPrice *p;
239 
240  g_return_val_if_fail (book, NULL);
241 
242  p = g_object_new(GNC_TYPE_PRICE, NULL);
243 
244  qof_instance_init_data (&p->inst, GNC_ID_PRICE, book);
245  qof_event_gen (&p->inst, QOF_EVENT_CREATE, NULL);
246 
247  return p;
248 }
void qof_instance_init_data(QofInstance *, QofIdType, QofBook *)
void qof_event_gen(QofInstance *entity, QofEventId event_type, gpointer event_data)
Invoke all registered event handlers using the given arguments.
void gnc_price_list_destroy ( PriceList *  prices)

gnc_price_list_destroy - destroy the given price list, calling gnc_price_unref on all the prices included in the list.

Definition at line 701 of file gnc-pricedb.c.

702 {
703  g_list_foreach(prices, price_list_destroy_helper, NULL);
704  g_list_free(prices);
705 }
gboolean gnc_price_list_insert ( PriceList **  prices,
GNCPrice p,
gboolean  check_dupl 
)

gnc_price_list_insert - insert a price into the given list, calling gnc_price_ref on it during the process.

Definition at line 645 of file gnc-pricedb.c.

646 {
647  GList *result_list;
648  PriceListIsDuplStruct* pStruct;
649  gboolean isDupl;
650 
651  if (!prices || !p) return FALSE;
652  gnc_price_ref(p);
653 
654  if (check_dupl)
655  {
656  pStruct = g_new0( PriceListIsDuplStruct, 1 );
657  pStruct->pPrice = p;
658  pStruct->isDupl = FALSE;
659  g_list_foreach( *prices, price_list_is_duplicate, pStruct );
660  isDupl = pStruct->isDupl;
661  g_free( pStruct );
662 
663  if ( isDupl )
664  {
665  return TRUE;
666  }
667  }
668 
669  result_list = g_list_insert_sorted(*prices, p, compare_prices_by_date);
670  if (!result_list) return FALSE;
671  *prices = result_list;
672  return TRUE;
673 }
void gnc_price_ref(GNCPrice *p)
Definition: gnc-pricedb.c:265
gboolean gnc_price_list_remove ( PriceList **  prices,
GNCPrice p 
)

gnc_price_list_remove - remove the price, p, from the given list, calling gnc_price_unref on it during the process.

Definition at line 676 of file gnc-pricedb.c.

677 {
678  GList *result_list;
679  GList *found_element;
680 
681  if (!prices || !p) return FALSE;
682 
683  found_element = g_list_find(*prices, p);
684  if (!found_element) return TRUE;
685 
686  result_list = g_list_remove_link(*prices, found_element);
687  gnc_price_unref((GNCPrice *) found_element->data);
688  g_list_free(found_element);
689 
690  *prices = result_list;
691  return TRUE;
692 }
void gnc_price_unref(GNCPrice *p)
Definition: gnc-pricedb.c:272
void gnc_price_print ( GNCPrice db,
FILE *  f,
int  indent 
)

This simple function can be useful for debugging the price code

Definition at line 2424 of file gnc-pricedb.c.

2425 {
2426  gnc_commodity *commodity;
2427  gnc_commodity *currency;
2428  gchar *istr = NULL; /* indent string */
2429  const char *str;
2430 
2431  if (!p) return;
2432  if (!f) return;
2433 
2434  commodity = gnc_price_get_commodity(p);
2435  currency = gnc_price_get_currency(p);
2436 
2437  if (!commodity) return;
2438  if (!currency) return;
2439 
2440  istr = g_strnfill(indent, ' ');
2441 
2442  fprintf(f, "%s<pdb:price>\n", istr);
2443  fprintf(f, "%s <pdb:commodity pointer=%p>\n", istr, commodity);
2444  str = gnc_commodity_get_namespace(commodity);
2445  str = str ? str : "(null)";
2446  fprintf(f, "%s <cmdty:ref-space>%s</gnc:cmdty:ref-space>\n", istr, str);
2447  str = gnc_commodity_get_mnemonic(commodity);
2448  str = str ? str : "(null)";
2449  fprintf(f, "%s <cmdty:ref-id>%s</cmdty:ref-id>\n", istr, str);
2450  fprintf(f, "%s </pdb:commodity>\n", istr);
2451  fprintf(f, "%s <pdb:currency pointer=%p>\n", istr, currency);
2452  str = gnc_commodity_get_namespace(currency);
2453  str = str ? str : "(null)";
2454  fprintf(f, "%s <cmdty:ref-space>%s</gnc:cmdty:ref-space>\n", istr, str);
2455  str = gnc_commodity_get_mnemonic(currency);
2456  str = str ? str : "(null)";
2457  fprintf(f, "%s <cmdty:ref-id>%s</cmdty:ref-id>\n", istr, str);
2458  fprintf(f, "%s </pdb:currency>\n", istr);
2459  str = gnc_price_get_source(p);
2460  str = str ? str : "(null)";
2461  fprintf(f, "%s %s\n", istr, str);
2462  str = gnc_price_get_typestr(p);
2463  str = str ? str : "(null)";
2464  fprintf(f, "%s %s\n", istr, str);
2465  fprintf(f, "%s %g\n", istr, gnc_numeric_to_double(gnc_price_get_value(p)));
2466  fprintf(f, "%s</pdb:price>\n", istr);
2467 
2468  g_free(istr);
2469 }
const char * gnc_commodity_get_mnemonic(const gnc_commodity *cm)
const char * gnc_commodity_get_namespace(const gnc_commodity *cm)
gdouble gnc_numeric_to_double(gnc_numeric n)
void gnc_price_ref ( GNCPrice p)

gnc_price_ref - indicate your need for a given price to stick around (i.e. increase its reference count by 1).

Definition at line 265 of file gnc-pricedb.c.

266 {
267  if (!p) return;
268  p->refcount++;
269 }
void gnc_price_unref ( GNCPrice p)

gnc_price_unref - indicate you're finished with a price (i.e. decrease its reference count by 1).

Definition at line 272 of file gnc-pricedb.c.

273 {
274  if (!p) return;
275  if (p->refcount == 0)
276  {
277  return;
278  }
279 
280  p->refcount--;
281 
282  if (p->refcount <= 0)
283  {
284  if (NULL != p->db)
285  {
286  PERR("last unref while price in database");
287  }
288  gnc_price_destroy (p);
289  }
290 }
#define PERR(format, args...)
Definition: qoflog.h:237
void gnc_pricedb_print_contents ( GNCPriceDB db,
FILE *  f 
)

This simple function can be useful for debugging the pricedb code

Definition at line 2480 of file gnc-pricedb.c.

2481 {
2482  if (!db)
2483  {
2484  PERR("NULL PriceDB\n");
2485  return;
2486  }
2487  if (!f)
2488  {
2489  PERR("NULL FILE*\n");
2490  return;
2491  }
2492 
2493  fprintf(f, "<gnc:pricedb>\n");
2494  gnc_pricedb_foreach_price(db, print_pricedb_adapter, f, FALSE);
2495  fprintf(f, "</gnc:pricedb>\n");
2496 }
#define PERR(format, args...)
Definition: qoflog.h:237
gboolean gnc_pricedb_foreach_price(GNCPriceDB *db, gboolean(*f)(GNCPrice *p, gpointer user_data), gpointer user_data, gboolean stable_order)
Definition: gnc-pricedb.c:2344