summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJakob P. Liljenberg <admin@qvantnet.com>2023-08-26 19:29:57 +0200
committerGitHub <noreply@github.com>2023-08-26 19:29:57 +0200
commitc296ac13cd4c16a11e137c309b7452bab096312e (patch)
tree29c65e61347e57066301e8803cb8c4ffa73439bc
parent9a1e760a661c9a160dd83e6d3ab710bf36b19b04 (diff)
parent091c30ab2be074836bb8d9a4f658cec9a5b36303 (diff)
Merge pull request #590 from nobounce/dangling-reference-config
Convert parameters and config keys to std::string_view
-rw-r--r--src/btop_config.cpp28
-rw-r--r--src/btop_config.hpp37
2 files changed, 34 insertions, 31 deletions
diff --git a/src/btop_config.cpp b/src/btop_config.cpp
index 25b68dd..6010189 100644
--- a/src/btop_config.cpp
+++ b/src/btop_config.cpp
@@ -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"
@@ -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"},
@@ -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},
@@ -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},
@@ -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;
@@ -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);
@@ -399,7 +401,7 @@ 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;
@@ -407,7 +409,7 @@ namespace Config {
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!";
@@ -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))
@@ -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)));
diff --git a/src/btop_config.hpp b/src/btop_config.hpp
index 95c6b68..e5f4ac1 100644
--- a/src/btop_config.hpp
+++ b/src/btop_config.hpp
@@ -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;
@@ -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" };
@@ -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();