diff --git a/src/const/private/const.cpp b/src/const/private/const.cpp index efa8f4b..c1e21b0 100644 --- a/src/const/private/const.cpp +++ b/src/const/private/const.cpp @@ -19,11 +19,7 @@ bool constants::initialize(const std::string& filename) { return load(filename); } -constant constants::get(const std::string& name) { - return constants_[name]; -} - -constant constants::operator[](const std::string& name) const { +constant constants::get(const std::string& name) const { auto it = constants_.find(name); if (it != constants_.end()) { return it->second; @@ -32,6 +28,10 @@ constant constants::operator[](const std::string& name) const { } } +constant constants::operator[](const std::string& name) const { + return this->get(name); +} + bool constants::has(const std::string& name) const { return constants_.find(name) != constants_.end(); } @@ -50,9 +50,9 @@ std::string constants::trim(const std::string& str) { size_t last = str.find_last_not_of(" \t"); return str.substr(first, last - first + 1); } + bool constants::load(const std::string& filename) { std::ifstream file(filename); - std::map constants_temp; if (!file.is_open()) { std::cerr << "Error: Unable to open file " << filename << std::endl; return false; @@ -81,12 +81,12 @@ bool constants::load(const std::string& filename) { // Define exact column widths from Python script int start = 0; - std::string symbol = trim(line.substr(start, col_widths_[0])); start += col_widths_[0]; - std::string name = trim(line.substr(start, col_widths_[1])); start += col_widths_[1]; - std::string valueStr = line.substr(start, col_widths_[2]); start += col_widths_[2]; - std::string unit = trim(line.substr(start, col_widths_[3])); start += col_widths_[3]; // Only trim the unit - std::string uncertaintyStr = line.substr(start, col_widths_[4]); start += col_widths_[4]; - std::string reference = trim(line.substr(start, col_widths_[5])); // Only trim reference + const std::string symbol = trim(line.substr(start, col_widths_[0])); start += col_widths_[0]; + const std::string name = trim(line.substr(start, col_widths_[1])); start += col_widths_[1]; + const std::string valueStr = line.substr(start, col_widths_[2]); start += col_widths_[2]; + const std::string unit = trim(line.substr(start, col_widths_[3])); start += col_widths_[3]; // Only trim the unit + const std::string uncertaintyStr = line.substr(start, col_widths_[4]); start += col_widths_[4]; + const std::string reference = trim(line.substr(start, col_widths_[5])); // Only trim reference // Convert numerical fields safely double value = 0.0, uncertainty = 0.0; @@ -102,11 +102,10 @@ bool constants::load(const std::string& filename) { } // Store in map - constants_temp[symbol] = {name, value, uncertainty, unit, reference}; + constants_.emplace(symbol, constant{name, value, uncertainty, unit, reference}); } file.close(); - constants_ = constants_temp; loaded_ = true; return true; } diff --git a/src/const/public/const.h b/src/const/public/const.h index eae184c..146a221 100644 --- a/src/const/public/const.h +++ b/src/const/public/const.h @@ -11,11 +11,22 @@ * @brief Structure to hold a constant's details. */ struct constant { - 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 + const std::string name; ///< Name of the constant + const double value; ///< Value of the constant + const double uncertainty; ///< Uncertainty in the constant's value + const std::string unit; ///< Unit of the constant + const std::string reference; ///< Reference for the constant's value + + /** + * @brief Parameterized constructor. + * @param name The name of the constant. + * @param value The value of the constant. + * @param uncertainty The uncertainty in the constant's value. + * @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) + : name(name), value(value), uncertainty(uncertainty), unit(unit), reference(reference) {} /** * @brief overload the << operator for pretty printing @@ -81,7 +92,7 @@ public: * @param key The name of the constant to retrieve. * @return The constant associated with the given key. */ - constant get(const std::string& key); + constant get(const std::string& key) const; /** * @brief Overloaded subscript operator to access constants by key.