Skip to content

Commit

Permalink
clang-tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
wichtounet committed Aug 9, 2023
1 parent f1079bc commit 749c416
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 62 deletions.
13 changes: 7 additions & 6 deletions include/assets.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ bool asset_exists(const std::string& asset);
bool share_asset_exists(const std::string& asset);

budget::asset get_asset(size_t id);
budget::asset get_asset(std::string name);
budget::asset get_asset(std::string_view name);

budget::asset_value get_asset_value(size_t id);
budget::asset_share get_asset_share(size_t id);
Expand Down Expand Up @@ -237,22 +237,23 @@ budget::money get_portfolio_value();
budget::money get_net_worth_cash();

budget::money get_net_worth(data_cache & cache);
budget::money get_net_worth(budget::date d, data_cache & cache);
budget::money get_net_worth(const budget::date& d, data_cache& cache);

budget::money get_fi_net_worth(data_cache & cache);
budget::money get_fi_net_worth(budget::date d, data_cache & cache);
budget::money get_fi_net_worth(const budget::date& d, data_cache& cache);

// The value of an asset in its own currency
budget::money get_asset_value(const budget::asset & asset, data_cache & cache);
budget::money get_asset_value(const budget::asset & asset, budget::date d, data_cache & cache);
budget::money get_asset_value(const budget::asset& asset, const budget::date& d, data_cache& cache);

// The value of an asset in the default currency
budget::money get_asset_value_conv(const budget::asset & asset, data_cache & cache);
budget::money get_asset_value_conv(const budget::asset & asset, budget::date d, data_cache & cache);
budget::money get_asset_value_conv(const budget::asset& asset, const budget::date& d, data_cache& cache);

// The value of an asset in a specific currency
budget::money get_asset_value_conv(const budget::asset & asset, const std::string& currency, data_cache & cache);
budget::money get_asset_value_conv(const budget::asset & asset, budget::date d, const std::string& currency, data_cache & cache);
budget::money get_asset_value_conv(const budget::asset& asset, const budget::date& d, const std::string& currency,
data_cache& cache);

// Utilities for assets
void update_asset_class_allocation(budget::asset& asset, budget::asset_class & clas, budget::money alloc);
Expand Down
2 changes: 1 addition & 1 deletion include/budget_exception.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace budget {
struct budget_exception : std::exception {
budget_exception(std::string message, bool should_log = false);

~budget_exception() throw();
~budget_exception() noexcept = default;

/*!
* Return the error message.
Expand Down
65 changes: 30 additions & 35 deletions src/assets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,9 @@

using namespace budget;

namespace ranges = std::ranges;
namespace views = std::ranges::views;

namespace {

static data_handler<asset> assets { "assets", "assets.data" };
data_handler<asset> assets{"assets", "assets.data"};

std::vector<std::string> get_asset_names(data_cache& cache) {
return to_vector(cache.user_assets() | to_name);
Expand All @@ -60,7 +57,7 @@ std::map<std::string, std::string> budget::asset::get_params() const {
params["input_active"] = active ? "true" : "false";

// The asset classes allocation
for (auto & [clas_id, alloc] : classes) {
for (const auto& [clas_id, alloc] : classes) {
params["input_class_" + to_string(clas_id)] = budget::to_string(alloc);
}

Expand All @@ -81,7 +78,7 @@ void budget::assets_module::handle(const std::vector<std::string>& args){
if(args.size() == 1){
show_assets(w);
} else {
auto& subcommand = args[1];
const auto& subcommand = args[1];

if(subcommand == "show"){
show_assets(w);
Expand Down Expand Up @@ -250,7 +247,7 @@ void budget::assets_module::handle(const std::vector<std::string>& args){
if(args.size() == 2){
budget::show_asset_classes(w);
} else {
auto& subsubcommand = args[2];
const auto& subsubcommand = args[2];

if(subsubcommand == "add"){
asset_class clas;
Expand Down Expand Up @@ -318,7 +315,7 @@ void budget::assets_module::handle(const std::vector<std::string>& args){
if(args.size() == 2){
budget::show_asset_values(w);
} else {
auto& subsubcommand = args[2];
const auto& subsubcommand = args[2];

if(subsubcommand == "set"){
asset_value asset_value;
Expand Down Expand Up @@ -372,7 +369,7 @@ void budget::assets_module::handle(const std::vector<std::string>& args){
} else if (subsubcommand == "delete") {
enough_args(args, 4);

size_t id = to_number<size_t>(args[3]);
const auto id = to_number<size_t>(args[3]);

auto value = get_asset_value(id);

Expand All @@ -391,7 +388,7 @@ void budget::assets_module::handle(const std::vector<std::string>& args){
if (args.size() == 2) {
list_asset_shares(w);
} else {
auto& subsubcommand = args[2];
const auto& subsubcommand = args[2];

if (subsubcommand == "add") {
if (get_share_asset_names(w.cache).empty()) {
Expand Down Expand Up @@ -421,13 +418,13 @@ void budget::assets_module::handle(const std::vector<std::string>& args){
} else if (subsubcommand == "test") {
enough_args(args, 4);

auto quote = args[3];
const auto& quote = args[3];

std::cout << quote << ":" << budget::share_price(quote) << std::endl;
} else if(subsubcommand == "edit"){
enough_args(args, 4);

size_t id = to_number<size_t>(args[3]);
const auto id = to_number<size_t>(args[3]);

if (!asset_share_exists(id)) {
throw budget_exception("There are no asset share with id " + args[3]);
Expand All @@ -449,7 +446,7 @@ void budget::assets_module::handle(const std::vector<std::string>& args){
} else if (subsubcommand == "delete") {
enough_args(args, 4);

size_t id = to_number<size_t>(args[3]);
const auto id = to_number<size_t>(args[3]);

if (!asset_share_exists(id)) {
throw budget_exception("There are no asset share with id " + args[3]);
Expand Down Expand Up @@ -506,7 +503,7 @@ budget::asset budget::get_asset(size_t id){
return assets[id];
}

budget::asset budget::get_asset(std::string name){
budget::asset budget::get_asset(std::string_view name) {
for (auto& asset : assets.data() | filter_by_name(name)){
return asset;
}
Expand Down Expand Up @@ -705,9 +702,8 @@ std::vector<asset> budget::all_assets(){
budget::date budget::asset_start_date(data_cache& cache, const budget::asset& asset) {
if (asset.share_based) {
return min_with_default(cache.asset_shares() | filter_by_asset(asset.id) | to_date, budget::local_day());
} else {
return min_with_default(cache.asset_values() | filter_by_asset(asset.id) | to_date, budget::local_day());
}
return min_with_default(cache.asset_values() | filter_by_asset(asset.id) | to_date, budget::local_day());
}

budget::date budget::asset_start_date(data_cache & cache) {
Expand Down Expand Up @@ -747,7 +743,7 @@ std::string to_percent(double p){
}

void budget::show_assets(budget::writer& w){
if (!assets.size()) {
if (assets.empty()) {
w << "No assets" << end_of_line;
return;
}
Expand Down Expand Up @@ -956,7 +952,7 @@ void budget::show_asset_values(budget::writer& w, bool liability){
for (auto& clas : w.cache.asset_classes()) {
bool found = false;

for (auto& [class_id, alloc] : asset.classes) {
for (const auto& [class_id, alloc] : asset.classes) {
if (class_id == clas.id) {
auto class_amount = amount * (float(alloc) / 100.0);
line.emplace_back(to_string(class_amount));
Expand Down Expand Up @@ -1062,7 +1058,7 @@ void budget::show_asset_values(budget::writer& w, bool liability){

auto liabilities = w.cache.liabilities();

if (liabilities.size()) {
if (!liabilities.empty()) {
contents.emplace_back(columns.size(), "");

for (const auto& [liability, amount] : liabilities | expand_value(w.cache) | not_zero) {
Expand Down Expand Up @@ -1098,7 +1094,7 @@ void budget::show_asset_values(budget::writer& w, bool liability){

}

budget::money net_worth = assets_total - liabilities_total;
budget::money const net_worth = assets_total - liabilities_total;

{
std::vector<std::string> line(columns.size(), "");
Expand All @@ -1123,7 +1119,7 @@ void budget::show_asset_values(budget::writer& w, bool liability){

std::vector<std::vector<std::string>> contents;

std::map<std::string, budget::money> classes;
const std::map<std::string, budget::money> classes;

budget::money total;

Expand Down Expand Up @@ -1190,7 +1186,7 @@ budget::money budget::get_net_worth(data_cache & cache){
return get_net_worth(budget::local_day(), cache);
}

budget::money budget::get_net_worth(budget::date d, data_cache & cache) {
budget::money budget::get_net_worth(const budget::date& d, data_cache& cache) {
return fold_left_auto(cache.user_assets() | to_value_conv(cache, d))
- fold_left_auto(cache.liabilities() | to_value_conv(cache, d));
}
Expand All @@ -1199,19 +1195,19 @@ budget::money budget::get_fi_net_worth(data_cache & cache){
return get_fi_net_worth(budget::local_day(), cache);
}

budget::money budget::get_fi_net_worth(budget::date d, data_cache & cache) {
budget::money budget::get_fi_net_worth(const budget::date& d, data_cache& cache) {
budget::money total;

for (const auto& [asset, value] : cache.user_assets() | is_fi | expand_value_conv(cache, d)) {
for (auto& [class_id, alloc] : asset.classes) {
for (const auto& [class_id, alloc] : asset.classes) {
if (get_asset_class(class_id).fi) {
total += value * (float(alloc) / float(100));
}
}
}

for (const auto& [asset, value] : cache.liabilities() | is_fi | expand_value_conv(cache, d)) {
for (auto& [class_id, alloc] : asset.classes) {
for (const auto& [class_id, alloc] : asset.classes) {
if (get_asset_class(class_id).fi) {
total -= value * (float(alloc) / float(100));
}
Expand All @@ -1228,7 +1224,7 @@ budget::money budget::get_net_worth_cash(){

namespace {

int get_shares(const budget::asset& asset, budget::date d, data_cache & cache) {
int get_shares(const budget::asset& asset, const budget::date& d, data_cache& cache) {
int64_t shares = 0;

for (auto& asset_share : cache.sorted_asset_shares()) {
Expand All @@ -1252,9 +1248,9 @@ int get_shares(const budget::asset& asset, budget::date d, data_cache & cache) {
// 2) If this becomes too high, we can also store the value an asset id for each
// possible date (in one pass of all asset values of an asset)

budget::money budget::get_asset_value(const budget::asset & asset, budget::date d, data_cache & cache) {
budget::money budget::get_asset_value(const budget::asset& asset, const budget::date& d, data_cache& cache) {
if (asset.share_based) [[unlikely]] {
int64_t shares = get_shares(asset, d, cache);
const int64_t shares = get_shares(asset, d, cache);

if (shares > 0) {
return static_cast<int>(shares) * share_price(asset.ticker, d);
Expand Down Expand Up @@ -1287,33 +1283,32 @@ budget::money budget::get_asset_value_conv(const budget::asset & asset, data_cac
return get_asset_value_conv(asset, budget::local_day(), cache);
}

budget::money budget::get_asset_value_conv(const budget::asset & asset, budget::date d, data_cache & cache) {
budget::money budget::get_asset_value_conv(const budget::asset& asset, const budget::date& d, data_cache& cache) {
auto amount = get_asset_value(asset, d, cache);

if (amount) {
return amount * exchange_rate(asset.currency, d);
} else {
return amount;
}
return amount;
}

budget::money budget::get_asset_value_conv(const budget::asset & asset, const std::string& currency, data_cache & cache) {
return get_asset_value_conv(asset, budget::local_day(), currency, cache);
}

budget::money budget::get_asset_value_conv(const budget::asset & asset, budget::date d, const std::string& currency, data_cache & cache) {
budget::money budget::get_asset_value_conv(const budget::asset& asset, const budget::date& d,
const std::string& currency, data_cache& cache) {
auto amount = get_asset_value(asset, d, cache);

if (amount) {
return amount * exchange_rate(asset.currency, currency, d);
} else {
return amount;
}
return amount;
}

bool budget::is_ticker_active(data_cache & cache, std::string_view ticker) {
for (auto & asset : cache.assets() | share_based_only | filter_by_ticker(ticker)) {
int64_t shares = get_shares(asset, local_day(), cache);
const int64_t shares = get_shares(asset, local_day(), cache);

if (shares > 0) {
return true;
Expand Down
6 changes: 3 additions & 3 deletions src/budget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ struct module_runner {
std::vector<std::string> args;
bool handled = false;

module_runner(std::vector<std::string>&& args) : args(std::forward<std::vector<std::string>>(args)) {
explicit module_runner(std::vector<std::string>&& args) : args(std::forward<std::vector<std::string>>(args)) {
//Nothing to init
}

Expand Down Expand Up @@ -179,7 +179,7 @@ std::string exec_command(const std::string& command) {

FILE* stream = popen(command.c_str(), "r");

while (fgets(buffer, 1024, stream) != NULL) {
while (fgets(buffer, 1024, stream) != nullptr) {
output << buffer;
}

Expand All @@ -198,7 +198,7 @@ bool has_enough_colors(){
} //end of anonymous namespace

int main(int argc, const char* argv[]) {
std::locale global_locale("");
const std::locale global_locale("");
std::locale::global(global_locale);

budget::init_logging(argc, const_cast<char**>(argv));
Expand Down
6 changes: 1 addition & 5 deletions src/budget_exception.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@

budget::budget_exception::budget_exception(std::string message, bool should_log) : message_(std::move(message)), should_log_(should_log) {}

budget::budget_exception::~budget_exception() throw() {}

const char* budget::budget_exception::what() const throw() {
return message_.c_str();
}
const char* budget::budget_exception::what() const noexcept { return message_.c_str(); }

const std::string& budget::budget_exception::message() const {
return message_;
Expand Down
Loading

0 comments on commit 749c416

Please sign in to comment.