2025-02-10 16:47:58 -05:00
|
|
|
#ifndef CONST_H
|
|
|
|
|
#define CONST_H
|
|
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <fstream>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include <map>
|
|
|
|
|
|
2025-02-11 18:02:07 -05:00
|
|
|
/**
|
|
|
|
|
* @brief Structure to hold a constant's details.
|
|
|
|
|
*/
|
2025-02-10 16:47:58 -05:00
|
|
|
struct constant {
|
2025-02-11 18:02:07 -05:00
|
|
|
std::string name; ///< Name of the constant
|
|
|
|
|
double value; ///< Value of the constant
|
|
|
|
|
double uncertainty; ///< Uncertainty in the constant's value
|
|
|
|
|
std::string unit; ///< Unit of the constant
|
|
|
|
|
std::string reference; ///< Reference for the constant's value
|
2025-02-12 10:38:22 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief overload the << operator for pretty printing
|
|
|
|
|
*/
|
|
|
|
|
friend std::ostream& operator<<(std::ostream& os, const constant& c) {
|
|
|
|
|
os << "<" << c.name << ": ";
|
|
|
|
|
os << c.value << "±" << c.uncertainty << " ";
|
|
|
|
|
os << c.unit << " (" << c.reference << ")>\n";
|
|
|
|
|
return os;
|
|
|
|
|
}
|
2025-02-11 18:02:07 -05:00
|
|
|
};
|
2025-02-10 16:47:58 -05:00
|
|
|
|
2025-02-11 18:02:07 -05:00
|
|
|
/**
|
|
|
|
|
* @brief Class to manage a collection of constants.
|
|
|
|
|
*/
|
2025-02-10 16:47:58 -05:00
|
|
|
class constants {
|
|
|
|
|
private:
|
2025-02-11 18:02:07 -05:00
|
|
|
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
|
2025-02-10 16:47:58 -05:00
|
|
|
|
2025-02-11 18:02:07 -05:00
|
|
|
/**
|
|
|
|
|
* @brief Load constants from a file.
|
|
|
|
|
* @param filename The name of the file to load constants from.
|
|
|
|
|
* @return True if loading was successful, false otherwise.
|
|
|
|
|
*/
|
2025-02-10 16:47:58 -05:00
|
|
|
bool load(const std::string& filename);
|
|
|
|
|
|
2025-02-11 18:02:07 -05:00
|
|
|
/**
|
|
|
|
|
* @brief Trim leading and trailing whitespace from a string.
|
|
|
|
|
* @param str The string to trim.
|
|
|
|
|
* @return The trimmed string.
|
|
|
|
|
*/
|
|
|
|
|
std::string trim(const std::string& str);
|
|
|
|
|
|
2025-02-10 16:47:58 -05:00
|
|
|
public:
|
2025-02-11 18:02:07 -05:00
|
|
|
/**
|
|
|
|
|
* @brief Default constructor.
|
|
|
|
|
*/
|
2025-02-10 16:47:58 -05:00
|
|
|
constants();
|
|
|
|
|
|
2025-02-11 18:02:07 -05:00
|
|
|
/**
|
|
|
|
|
* @brief Constructor that initializes constants from a file.
|
|
|
|
|
* @param filename The name of the file to load constants from.
|
|
|
|
|
*/
|
2025-02-10 16:47:58 -05:00
|
|
|
constants(const std::string& filename);
|
|
|
|
|
|
2025-02-11 18:02:07 -05:00
|
|
|
/**
|
|
|
|
|
* @brief Check if constants are loaded.
|
|
|
|
|
* @return True if constants are loaded, false otherwise.
|
|
|
|
|
*/
|
2025-02-10 16:47:58 -05:00
|
|
|
bool is_loaded() { return loaded_; }
|
|
|
|
|
|
2025-02-11 18:02:07 -05:00
|
|
|
/**
|
|
|
|
|
* @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.
|
|
|
|
|
*/
|
2025-02-10 16:47:58 -05:00
|
|
|
bool initialize(const std::string& filename);
|
|
|
|
|
|
2025-02-11 18:02:07 -05:00
|
|
|
/**
|
|
|
|
|
* @brief Get a constant by key.
|
|
|
|
|
* @param key The name of the constant to retrieve.
|
|
|
|
|
* @return The constant associated with the given key.
|
|
|
|
|
*/
|
2025-02-10 16:47:58 -05:00
|
|
|
constant get(const std::string& key);
|
|
|
|
|
|
2025-02-11 18:02:07 -05:00
|
|
|
/**
|
|
|
|
|
* @brief Overloaded subscript operator to access constants by key.
|
|
|
|
|
* @param key The name of the constant to retrieve.
|
|
|
|
|
* @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;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Check if a constant exists by key.
|
|
|
|
|
* @param key The name of the constant to check.
|
|
|
|
|
* @return True if the constant exists, false otherwise.
|
|
|
|
|
*/
|
|
|
|
|
bool has(const std::string& key) const;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Get a list of all constant keys.
|
|
|
|
|
* @return A vector of all constant keys.
|
|
|
|
|
*/
|
|
|
|
|
std::set<std::string> keys() const;
|
2025-02-10 16:47:58 -05:00
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif
|