Skip to content

Commit

Permalink
More explicit
Browse files Browse the repository at this point in the history
  • Loading branch information
wichtounet committed Oct 15, 2023
1 parent a2dbe6d commit 02f7d58
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 34 deletions.
14 changes: 7 additions & 7 deletions include/date.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ struct date_exception : std::exception {

struct day {
date_type value;
day(date_type value) : value(value) {}
explicit day(date_type value) : value(value) {}

operator date_type() const { return value; }

Expand All @@ -57,7 +57,7 @@ struct day {
struct month {
date_type value;

month(date_type value) : value(value) {}
explicit month(date_type value) : value(value) {}

operator date_type() const { return value; }

Expand All @@ -68,11 +68,11 @@ struct month {
}

month operator-(date_type remove) const {
return {static_cast<date_type>(value - remove)};
return month{static_cast<date_type>(value - remove)};
}

month operator+(date_type add) const {
return {static_cast<date_type>(value + add)};
return month{static_cast<date_type>(value + add)};
}

std::string as_short_string() const {
Expand Down Expand Up @@ -111,7 +111,7 @@ struct month {

struct year {
date_type value;
year(date_type value) : value(value) {}
explicit year(date_type value) : value(value) {}
operator date_type() const { return value; }

year& operator=(date_type new_value){
Expand All @@ -121,11 +121,11 @@ struct year {
}

year operator-(date_type remove) const {
return {static_cast<date_type>(value - remove)};
return year{static_cast<date_type>(value - remove)};
}

year operator+(date_type add) const {
return {static_cast<date_type>(value + add)};
return year{static_cast<date_type>(value + add)};
}

bool is_default() const {
Expand Down
2 changes: 1 addition & 1 deletion src/date.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ std::string budget::date_to_string(const budget::date& date) {
}

budget::month budget::start_month(data_cache & cache, budget::year year){
const budget::month m = min_with_default(cache.expenses() | filter_by_year(year) | to_month, 12);
const budget::month m = min_with_default(cache.expenses() | filter_by_year(year) | to_month, budget::month(12));
return min_with_default(cache.earnings() | filter_by_year(year) | to_month, m);
}

Expand Down
39 changes: 13 additions & 26 deletions src/overview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -480,9 +480,7 @@ void aggregate_overview_fv(const Data & data, budget::writer& w, bool full, bool
}

void add_month_columns(std::vector<std::string>& columns, budget::month sm){
for(unsigned short i = sm; i < 13; ++i){
const budget::month m = i;

for(budget::month m = sm; m.is_valid(); ++m){
columns.emplace_back(m.as_long_string());
}
}
Expand All @@ -507,14 +505,13 @@ inline void generate_total_line(data_cache & cache, std::vector<std::vector<std:

budget::money total_total;
budget::money current_total;
for(unsigned short i = sm; i < 13; ++i){
auto total = totals[i - 1];
for(budget::month m = sm; m.is_valid(); ++m){
auto total = totals[m.value - 1];

last_row.push_back(format_money(total));

total_total += total;

const budget::month m = i;
if(m < sm + current_months){
current_total += total;
}
Expand Down Expand Up @@ -614,9 +611,7 @@ void display_values(budget::writer& w, budget::year year, const std::string& tit
const budget::year last_year = year - date_type(1);
budget::money total;

for(unsigned short j = sm; j < 13; ++j){
const budget::month m = j;

for(budget::month m = sm; m.is_valid(); ++m){
budget::money month_total;

for(auto& value : values){
Expand Down Expand Up @@ -744,7 +739,7 @@ void budget::overview_module::handle(std::vector<std::string>& args) {
throw budget_exception("Too many arguments to overview aggregate");
}
} else if (subcommand == "account") {
auto ask_for_account = [&today](budget::month m = {0}, budget::year y = {0}) {
auto ask_for_account = [&today](budget::month m = budget::month{0}, budget::year y = budget::year{0}) {
if (m.is_default()) {
m = today.month();
}
Expand Down Expand Up @@ -828,9 +823,7 @@ void budget::display_local_balance(budget::writer& w, budget::year year, bool cu

//Fill the table

for(unsigned short i = sm; i < 13; ++i){
const budget::month m = i;

for(budget::month m = sm; m.is_valid(); ++m){
for(auto& account : all_accounts(w.cache, year, m)){
budget::money total_expenses;
budget::money total_earnings;
Expand All @@ -850,7 +843,7 @@ void budget::display_local_balance(budget::writer& w, budget::year year, bool cu

account_totals[account.name] += month_total;

totals[i - 1] += month_total;
totals[m.value - 1] += month_total;

if(m < sm + current_months){
account_current_totals[account.name] += month_total;
Expand Down Expand Up @@ -885,9 +878,7 @@ void budget::display_local_balance(budget::writer& w, budget::year year, bool cu

budget::money total;

for (unsigned short i = sm; i < 13; ++i) {
const budget::month m = i;

for(budget::month m = sm; m.is_valid(); ++m){
auto status = compute_month_status(w.cache, year - date_type(1), m);

contents.back().push_back(format_money(status.balance));
Expand Down Expand Up @@ -1007,9 +998,7 @@ void budget::display_balance(budget::writer& w, budget::year year, bool relaxed,

//Fill the table

for(unsigned short i = sm; i <= 12; ++i){
const budget::month m = i;

for(budget::month m = sm; m.is_valid(); ++m){
for(const auto& account : all_accounts(w.cache, year, m)){
budget::money total_expenses;
budget::money total_earnings;
Expand All @@ -1023,10 +1012,10 @@ void budget::display_balance(budget::writer& w, budget::year year, bool relaxed,
total_earnings = fold_left_auto(all_earnings_month(w.cache, account.id, year, m) | to_amount);
}

auto month_total = account_previous[account.name][i - 1] + account.amount - total_expenses + total_earnings;
account_previous[account.name][i] = month_total;
auto month_total = account_previous[account.name][m.value - 1] + account.amount - total_expenses + total_earnings;
account_previous[account.name][m.value] = month_total;

totals[i - 1] += month_total;
totals[m.value - 1] += month_total;

contents[row_mapping[account.name]].push_back(format_money(month_total));
}
Expand All @@ -1041,9 +1030,7 @@ void budget::display_balance(budget::writer& w, budget::year year, bool relaxed,

budget::money total;

for(unsigned short i = sm; i < 13; ++i){
const budget::month m = i;

for(budget::month m = sm; m.is_valid(); ++m){
auto status = compute_month_status(w.cache, year - date_type(1), m);

total += status.balance;
Expand Down

0 comments on commit 02f7d58

Please sign in to comment.