perf(const): const changed to a singelton

const needds to be accessed all throughout so it has been changed to a singleton to allow for more efficient usage

BREAKING CHANGE: Any previous loads to const will break, also constant->Constant and constants->Constants
This commit is contained in:
2025-02-12 12:53:50 -05:00
parent 4227eacd5b
commit 18ce7bf6de
9 changed files with 96 additions and 71 deletions

View File

@@ -10,7 +10,7 @@
/**
* @brief Structure to hold a constant's details.
*/
struct constant {
struct Constant {
const std::string name; ///< Name of the constant
const double value; ///< Value of the constant
const double uncertainty; ///< Uncertainty in the constant's value
@@ -25,13 +25,13 @@ struct constant {
* @param unit The unit of the constant.
* @param reference The reference for the constant's value.
*/
constant(const std::string& name, double value, double uncertainty, const std::string& unit, const std::string& reference)
Constant(const std::string& name, const double value, const double uncertainty, const std::string& unit, const std::string& reference)
: name(name), value(value), uncertainty(uncertainty), unit(unit), reference(reference) {}
/**
* @brief overload the << operator for pretty printing
*/
friend std::ostream& operator<<(std::ostream& os, const constant& c) {
friend std::ostream& operator<<(std::ostream& os, const Constant& c) {
os << "<" << c.name << ": ";
os << c.value << "±" << c.uncertainty << " ";
os << c.unit << " (" << c.reference << ")>\n";
@@ -42,18 +42,28 @@ struct constant {
/**
* @brief Class to manage a collection of constants.
*/
class constants {
class Constants {
private:
bool loaded_ = false; ///< Flag to indicate if constants are loaded
const int col_widths_[6] = {25, 52, 20, 20, 17, 45}; // From the python script used to generate the constants file
std::map<std::string, constant> constants_; ///< Map to store constants by name
std::map<std::string, Constant> constants_; ///< Map to store constants by name
/**
* @brief Load constants from a file.
* @param filename The name of the file to load constants from.
* @brief Default constructor. Private to avoid direct instantiation
*/
Constants();
/**
* @brief Load constants from the embedded header file.
* @return True if loading was successful, false otherwise.
*/
bool load(const std::string& filename);
bool load();
/**
* @brief Initialize constants.
* @return True if initialization was successful, false otherwise.
*/
bool initialize();
/**
* @brief Trim leading and trailing whitespace from a string.
@@ -63,36 +73,28 @@ private:
std::string trim(const std::string& str);
public:
/**
* @brief Default constructor.
*/
constants();
/**
* @brief Constructor that initializes constants from a file.
* @param filename The name of the file to load constants from.
* @brief get instance of constants singelton
* @return instance of constants
*/
constants(const std::string& filename);
static Constants& getInstance() {
static Constants instance;
return instance;
}
/**
* @brief Check if constants are loaded.
* @return True if constants are loaded, false otherwise.
*/
bool is_loaded() { return loaded_; }
/**
* @brief Initialize constants from a file.
* @param filename The name of the file to load constants from.
* @return True if initialization was successful, false otherwise.
*/
bool initialize(const std::string& filename);
bool isLoaded() { return loaded_; }
/**
* @brief Get a constant by key.
* @param key The name of the constant to retrieve.
* @return The constant associated with the given key.
*/
constant get(const std::string& key) const;
Constant get(const std::string& key) const;
/**
* @brief Overloaded subscript operator to access constants by key.
@@ -100,7 +102,7 @@ public:
* @return The constant associated with the given key.
* @throws std::out_of_range if the key is not found.
*/
constant operator[](const std::string& key) const;
Constant operator[](const std::string& key) const;
/**
* @brief Check if a constant exists by key.