Skip to content

Commit

Permalink
Merge pull request aristocratos#590 from nobounce/dangling-reference-…
Browse files Browse the repository at this point in the history
…config

Convert parameters and config keys to std::string_view
  • Loading branch information
aristocratos committed Aug 26, 2023
2 parents 9a1e760 + 091c30a commit c296ac1
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 31 deletions.
28 changes: 15 additions & 13 deletions src/btop_config.cpp
Expand Up @@ -17,11 +17,13 @@ tab-size = 4
*/

#include <array>
#include <ranges>
#include <atomic>
#include <fstream>
#include <ranges>
#include <string_view>

#include <fmt/core.h>

#include "btop_config.hpp"
#include "btop_shared.hpp"
#include "btop_tools.hpp"
Expand Down Expand Up @@ -193,7 +195,7 @@ namespace Config {
"#* The level set includes all lower levels, i.e. \"DEBUG\" will show all logging info."}
};

unordered_flat_map<string, string> strings = {
unordered_flat_map<std::string_view, string> strings = {
{"color_theme", "Default"},
{"shown_boxes", "cpu mem net proc"},
{"graph_symbol", "braille"},
Expand All @@ -219,9 +221,9 @@ namespace Config {
{"proc_command", ""},
{"selected_name", ""},
};
unordered_flat_map<string, string> stringsTmp;
unordered_flat_map<std::string_view, string> stringsTmp;

unordered_flat_map<string, bool> bools = {
unordered_flat_map<std::string_view, bool> bools = {
{"theme_background", true},
{"truecolor", true},
{"rounded_corners", true},
Expand Down Expand Up @@ -267,9 +269,9 @@ namespace Config {
{"show_detailed", false},
{"proc_filtering", false},
};
unordered_flat_map<string, bool> boolsTmp;
unordered_flat_map<std::string_view, bool> boolsTmp;

unordered_flat_map<string, int> ints = {
unordered_flat_map<std::string_view, int> ints = {
{"update_ms", 2000},
{"net_download", 100},
{"net_upload", 100},
Expand All @@ -280,9 +282,9 @@ namespace Config {
{"proc_selected", 0},
{"proc_last_selected", 0},
};
unordered_flat_map<string, int> intsTmp;
unordered_flat_map<std::string_view, int> intsTmp;

bool _locked(const string& name) {
bool _locked(const std::string_view name) {
atomic_wait(writelock, true);
if (not write_new and rng::find_if(descriptions, [&name](const auto& a) { return a.at(0) == name; }) != descriptions.end())
write_new = true;
Expand Down Expand Up @@ -369,7 +371,7 @@ namespace Config {

string validError;

bool intValid(const string& name, const string& value) {
bool intValid(const std::string_view name, const string& value) {
int i_value;
try {
i_value = stoi(value);
Expand Down Expand Up @@ -399,15 +401,15 @@ namespace Config {
return false;
}

bool stringValid(const string& name, const string& value) {
bool stringValid(const std::string_view name, const string& value) {
if (name == "log_level" and not v_contains(Logger::log_levels, value))
validError = "Invalid log_level: " + value;

else if (name == "graph_symbol" and not v_contains(valid_graph_symbols, value))
validError = "Invalid graph symbol identifier: " + value;

else if (name.starts_with("graph_symbol_") and (value != "default" and not v_contains(valid_graph_symbols, value)))
validError = "Invalid graph symbol identifier for" + name + ": " + value;
validError = fmt::format("Invalid graph symbol identifier for {}: {}", name, value);

else if (name == "shown_boxes" and not value.empty() and not check_boxes(value))
validError = "Invalid box name(s) in shown_boxes!";
Expand Down Expand Up @@ -456,7 +458,7 @@ namespace Config {
return false;
}

string getAsString(const string& name) {
string getAsString(const std::string_view name) {
if (bools.contains(name))
return (bools.at(name) ? "True" : "False");
else if (ints.contains(name))
Expand All @@ -466,7 +468,7 @@ namespace Config {
return "";
}

void flip(const string& name) {
void flip(const std::string_view name) {
if (_locked(name)) {
if (boolsTmp.contains(name)) boolsTmp.at(name) = not boolsTmp.at(name);
else boolsTmp.insert_or_assign(name, (not bools.at(name)));
Expand Down
37 changes: 19 additions & 18 deletions src/btop_config.hpp
Expand Up @@ -20,9 +20,10 @@ tab-size = 4

#include <string>
#include <vector>
#include <robin_hood.h>
#include <filesystem>

#include <robin_hood.h>

using std::string;
using std::vector;
using robin_hood::unordered_flat_map;
Expand All @@ -33,12 +34,12 @@ namespace Config {
extern std::filesystem::path conf_dir;
extern std::filesystem::path conf_file;

extern unordered_flat_map<string, string> strings;
extern unordered_flat_map<string, string> stringsTmp;
extern unordered_flat_map<string, bool> bools;
extern unordered_flat_map<string, bool> boolsTmp;
extern unordered_flat_map<string, int> ints;
extern unordered_flat_map<string, int> intsTmp;
extern unordered_flat_map<std::string_view, string> strings;
extern unordered_flat_map<std::string_view, string> stringsTmp;
extern unordered_flat_map<std::string_view, bool> bools;
extern unordered_flat_map<std::string_view, bool> boolsTmp;
extern unordered_flat_map<std::string_view, int> ints;
extern unordered_flat_map<std::string_view, int> intsTmp;

const vector<string> valid_graph_symbols = { "braille", "block", "tty" };
const vector<string> valid_graph_symbols_def = { "default", "braille", "block", "tty" };
Expand All @@ -62,44 +63,44 @@ namespace Config {
//* Apply selected preset
void apply_preset(const string& preset);

bool _locked(const string& name);
bool _locked(const std::string_view name);

//* Return bool for config key <name>
inline bool getB(const string& name) { return bools.at(name); }
inline bool getB(const std::string_view name) { return bools.at(name); }

//* Return integer for config key <name>
inline const int& getI(const string& name) { return ints.at(name); }
inline const int& getI(const std::string_view name) { return ints.at(name); }

//* Return string for config key <name>
inline const string& getS(const string& name) { return strings.at(name); }
inline const string& getS(const std::string_view name) { return strings.at(name); }

string getAsString(const string& name);
string getAsString(const std::string_view name);

extern string validError;

bool intValid(const string& name, const string& value);
bool stringValid(const string& name, const string& value);
bool intValid(const std::string_view name, const string& value);
bool stringValid(const std::string_view name, const string& value);

//* Set config key <name> to bool <value>
inline void set(const string& name, bool value) {
inline void set(const std::string_view name, bool value) {
if (_locked(name)) boolsTmp.insert_or_assign(name, value);
else bools.at(name) = value;
}

//* Set config key <name> to int <value>
inline void set(const string& name, const int& value) {
inline void set(const std::string_view name, const int& value) {
if (_locked(name)) intsTmp.insert_or_assign(name, value);
else ints.at(name) = value;
}

//* Set config key <name> to string <value>
inline void set(const string& name, const string& value) {
inline void set(const std::string_view name, const string& value) {
if (_locked(name)) stringsTmp.insert_or_assign(name, value);
else strings.at(name) = value;
}

//* Flip config key bool <name>
void flip(const string& name);
void flip(const std::string_view name);

//* Lock config and cache changes until unlocked
void lock();
Expand Down

0 comments on commit c296ac1

Please sign in to comment.