Skip to content

Commit

Permalink
[gnc-pricedb.cpp] use std::pair instead of a struct for PriceTuple
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherlam committed Aug 4, 2023
1 parent 6b741dd commit 2d730e3
Showing 1 changed file with 13 additions and 22 deletions.
35 changes: 13 additions & 22 deletions libgnucash/engine/gnc-pricedb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2371,17 +2371,12 @@ gnc_pricedb_lookup_nearest_before_t64 (GNCPriceDB *db,
}


typedef struct
{
GNCPrice *from;
GNCPrice *to;
} PriceTuple;
using PriceTuple = std::pair<GNCPrice*, GNCPrice*>;

static PriceTuple
extract_common_prices (PriceList *from_prices, PriceList *to_prices,
const gnc_commodity *from, const gnc_commodity *to)
{
PriceTuple retval = {NULL, NULL};
GList *from_node = NULL, *to_node = NULL;
GNCPrice *from_price = NULL, *to_price = NULL;

Expand Down Expand Up @@ -2411,31 +2406,27 @@ extract_common_prices (PriceList *from_prices, PriceList *to_prices,
break;
}
if (from_price == NULL || to_price == NULL)
return retval;
return {nullptr, nullptr};
gnc_price_ref(from_price);
gnc_price_ref(to_price);
retval.from = from_price;
retval.to = to_price;
return retval;
return {from_price, to_price};
}


static gnc_numeric
convert_price (const gnc_commodity *from, const gnc_commodity *to, PriceTuple tuple)
{
gnc_commodity *from_com = gnc_price_get_commodity (tuple.from);
gnc_commodity *from_cur = gnc_price_get_currency (tuple.from);
gnc_commodity *to_com = gnc_price_get_commodity (tuple.to);
gnc_commodity *to_cur = gnc_price_get_currency (tuple.to);
gnc_numeric from_val = gnc_price_get_value (tuple.from);
gnc_numeric to_val = gnc_price_get_value (tuple.to);
gnc_numeric price;
gnc_commodity *from_com = gnc_price_get_commodity (tuple.first);
gnc_commodity *from_cur = gnc_price_get_currency (tuple.first);
gnc_commodity *to_com = gnc_price_get_commodity (tuple.second);
gnc_commodity *to_cur = gnc_price_get_currency (tuple.second);
gnc_numeric from_val = gnc_price_get_value (tuple.first);
gnc_numeric to_val = gnc_price_get_value (tuple.second);
int no_round = GNC_HOW_DENOM_EXACT | GNC_HOW_RND_NEVER;
gnc_numeric price = gnc_numeric_div (to_val, from_val, GNC_DENOM_AUTO, no_round);

price = gnc_numeric_div (to_val, from_val, GNC_DENOM_AUTO, no_round);

gnc_price_unref (tuple.from);
gnc_price_unref (tuple.to);
gnc_price_unref (tuple.first);
gnc_price_unref (tuple.second);

if (from_cur == from && to_cur == to)
return price;
Expand Down Expand Up @@ -2489,7 +2480,7 @@ indirect_price_conversion (GNCPriceDB *db, const gnc_commodity *from,
tuple = extract_common_prices (from_prices, to_prices, from, to);
gnc_price_list_destroy (from_prices);
gnc_price_list_destroy (to_prices);
if (tuple.from)
if (tuple.first)
return convert_price (from, to, tuple);
return zero;
}
Expand Down

0 comments on commit 2d730e3

Please sign in to comment.