From a28b89ac4ee25d9b44c8294c3eb1e7ad41db7df7 Mon Sep 17 00:00:00 2001 From: Allan Leal Date: Thu, 23 Apr 2020 11:00:34 +0200 Subject: [PATCH] Improve const-correctness for classes Database and ThermoEngine --- ThermoFun/Database.cpp | 34 ++++++++++---------- ThermoFun/Database.h | 16 +++++----- ThermoFun/ThermoEngine.cpp | 46 +++++++++++++-------------- ThermoFun/ThermoEngine.h | 24 +++++++------- python/pyThermoFun/pyThermoEngine.cpp | 20 ++++++------ 5 files changed, 70 insertions(+), 70 deletions(-) diff --git a/ThermoFun/Database.cpp b/ThermoFun/Database.cpp index cca0255..0c3d066 100644 --- a/ThermoFun/Database.cpp +++ b/ThermoFun/Database.cpp @@ -57,7 +57,7 @@ struct Database::Impl } template - auto collectValues(const std::map& map) -> std::vector + auto collectValues(const std::map& map) const -> std::vector { std::vector collection; collection.reserve(map.size()); @@ -114,34 +114,34 @@ struct Database::Impl reactions_map = reactions; } - auto getElements() -> std::vector + auto getElements() const -> std::vector { return collectValues(elements_map); } - auto getSubstances() -> std::vector + auto getSubstances() const -> std::vector { return collectValues(substances_map); } - auto getReactions() -> std::vector + auto getReactions() const -> std::vector { return collectValues(reactions_map); } - auto numberOfElements() -> size_t + auto numberOfElements() const -> size_t { - return collectValues(elements_map).size(); + return elements_map.size(); } - auto numberOfSubstances() -> size_t + auto numberOfSubstances() const -> size_t { - return collectValues(substances_map).size(); + return substances_map.size(); } - auto numberOfReactions() -> size_t + auto numberOfReactions() const -> size_t { - return collectValues(reactions_map).size(); + return reactions_map.size(); } auto getElement(std::string symbol) -> Element& @@ -426,32 +426,32 @@ auto Database::mapReactions() const -> const ReactionsMap& return pimpl->mapReactions(); } -auto Database::getElements() -> std::vector +auto Database::getElements() const -> std::vector { return pimpl->getElements(); } -auto Database::getSubstances() -> std::vector +auto Database::getSubstances() const -> std::vector { return pimpl->getSubstances(); } -auto Database::getReactions() -> std::vector +auto Database::getReactions() const -> std::vector { return pimpl->getReactions(); } -auto Database::numberOfElements() -> size_t +auto Database::numberOfElements() const -> size_t { return pimpl->numberOfElements(); } -auto Database::numberOfSubstances() -> size_t +auto Database::numberOfSubstances() const -> size_t { return pimpl->numberOfSubstances(); } -auto Database::numberOfReactions() -> size_t +auto Database::numberOfReactions() const -> size_t { return pimpl->numberOfReactions(); } @@ -471,7 +471,7 @@ auto Database::containsReaction(std::string symbol) const -> bool return pimpl->containsReaction(symbol); } -auto Database::parseSubstanceFormula(std::string formula_) -> std::map +auto Database::parseSubstanceFormula(std::string formula_) const -> std::map { std::set elements; std::map map; diff --git a/ThermoFun/Database.h b/ThermoFun/Database.h index b52ecd8..0768543 100644 --- a/ThermoFun/Database.h +++ b/ThermoFun/Database.h @@ -31,7 +31,7 @@ class Database /** * @brief Construct a new Database object - * + * * @param filename name/path of the file or a string containing a ThermoDataSet in JSON format */ explicit Database(std::string filename); @@ -98,13 +98,13 @@ class Database auto addMapReactions(const ReactionsMap& reactions) -> void; /// Return all elements in the database - auto getElements() -> std::vector; + auto getElements() const -> std::vector; /// Return all substances in the database - auto getSubstances() -> std::vector; + auto getSubstances() const -> std::vector; /// Return all reactions in the database - auto getReactions() -> std::vector; + auto getReactions() const -> std::vector; /// Returns the map of elements in the database auto mapElements() const -> const ElementsMap&; @@ -116,13 +116,13 @@ class Database auto mapReactions() const -> const ReactionsMap&; /// Returns the number of elements in the databse - auto numberOfElements() -> size_t; + auto numberOfElements() const -> size_t; /// Returns the number of substances in the databse - auto numberOfSubstances() -> size_t; + auto numberOfSubstances() const -> size_t; /// Returns the number of reactions in the database - auto numberOfReactions() -> size_t; + auto numberOfReactions() const -> size_t; /// Return a element in the database auto getElement(std::string symbol) const -> const Element&; @@ -148,7 +148,7 @@ class Database /// Pareses a given substance formula present in the database /// @param formula /// @return map of Elements and coefficients - auto parseSubstanceFormula(std::string formula) -> std::map; + auto parseSubstanceFormula(std::string formula) const -> std::map; private: struct Impl; diff --git a/ThermoFun/ThermoEngine.cpp b/ThermoFun/ThermoEngine.cpp index 91a1f44..b5305e1 100644 --- a/ThermoFun/ThermoEngine.cpp +++ b/ThermoFun/ThermoEngine.cpp @@ -142,7 +142,7 @@ struct ThermoEngine::Impl tps.gibbs_energy -= (Tr * entropyElements); } - auto getThermoPreferencesReaction(const Reaction &reaction) -> ThermoPreferences + auto getThermoPreferencesReaction(const Reaction &reaction) const -> ThermoPreferences { ThermoPreferences preferences; @@ -158,7 +158,7 @@ struct ThermoEngine::Impl return preferences; } - auto getThermoPreferencesSubstance(const Substance &substance) -> ThermoPreferences + auto getThermoPreferencesSubstance(const Substance &substance) const -> ThermoPreferences { ThermoPreferences preferences; preferences.workSubstance = substance; @@ -409,12 +409,12 @@ struct ThermoEngine::Impl return tps; } - auto electroPropertiesSolvent(double T, double &P, std::string solvent) -> ElectroPropertiesSolvent + auto electroPropertiesSolvent(double T, double &P, std::string solvent) const -> ElectroPropertiesSolvent { return electroPropertiesSolvent(T, P, database.getSubstance(solvent)); } - auto electroPropertiesSolvent(double T, double &P, const Substance &solvent) -> ElectroPropertiesSolvent + auto electroPropertiesSolvent(double T, double &P, const Substance &solvent) const -> ElectroPropertiesSolvent { ThermoPreferences pref = getThermoPreferencesSubstance(solvent); PropertiesSolvent ps = propertiesSolvent(T, P, solvent); @@ -452,12 +452,12 @@ struct ThermoEngine::Impl return eps; } - auto propertiesSolvent(double T, double &P, std::string solvent) -> PropertiesSolvent + auto propertiesSolvent(double T, double &P, std::string solvent) const -> PropertiesSolvent { return propertiesSolvent(T, P, database.getSubstance(solvent)); } - auto propertiesSolvent(double T, double &P, const Substance &solvent) -> PropertiesSolvent + auto propertiesSolvent(double T, double &P, const Substance &solvent) const -> PropertiesSolvent { ThermoPreferences pref = getThermoPreferencesSubstance(solvent); PropertiesSolvent ps; @@ -494,7 +494,7 @@ struct ThermoEngine::Impl return ps; } - auto reacDCthermoProperties(double T, double &P, Substance subst) -> ThermoPropertiesSubstance + auto reacDCthermoProperties(double T, double &P, Substance subst) const -> ThermoPropertiesSubstance { ThermoPropertiesSubstance tps, reacTps; ThermoPropertiesReaction tpr; @@ -552,7 +552,7 @@ struct ThermoEngine::Impl return tps; } - auto thermoPropertiesReaction(double T, double &P, std::string symbol) -> ThermoPropertiesReaction + auto thermoPropertiesReaction(double T, double &P, std::string symbol) const -> ThermoPropertiesReaction { // if the thermoPropertiesReaction function is called using a reaction equation if (!database.containsReaction(symbol) && (symbol.find("=") != std::string::npos)) @@ -565,7 +565,7 @@ struct ThermoEngine::Impl return thermoPropertiesReaction(T, P, database.getReaction(symbol)); } - auto thermoPropertiesReaction(double T, double &P, const Reaction &reaction) -> ThermoPropertiesReaction + auto thermoPropertiesReaction(double T, double &P, const Reaction &reaction) const -> ThermoPropertiesReaction { ThermoPropertiesReaction tpr; ThermoPreferences pref = getThermoPreferencesReaction(reaction); @@ -660,15 +660,15 @@ struct ThermoEngine::Impl else tpr = thermoPropertiesReactionFromReactants(T, P, reaction); - return tpr; + return tpr; } - auto thermoPropertiesReactionFromReactants(double T, double &P, std::string symbol) -> ThermoPropertiesReaction + auto thermoPropertiesReactionFromReactants(double T, double &P, std::string symbol) const -> ThermoPropertiesReaction { return thermoPropertiesReactionFromReactants(T, P, database.getReaction(symbol)); } - auto thermoPropertiesReactionFromReactants(double T, double &P, const Reaction& reaction) -> ThermoPropertiesReaction + auto thermoPropertiesReactionFromReactants(double T, double &P, const Reaction& reaction) const -> ThermoPropertiesReaction { ThermoPropertiesReaction tpr; tpr.reaction_heat_capacity_cp = 0.0; @@ -736,53 +736,53 @@ ThermoEngine::ThermoEngine(const ThermoEngine &other) { } -auto ThermoEngine::thermoPropertiesSubstance(double T, double &P, std::string substance) -> ThermoPropertiesSubstance +auto ThermoEngine::thermoPropertiesSubstance(double T, double &P, std::string substance) const -> ThermoPropertiesSubstance { return pimpl->thermo_properties_substance_fn(T, P, P, substance); } -auto ThermoEngine::electroPropertiesSolvent(double T, double &P, std::string solvent) -> ElectroPropertiesSolvent +auto ThermoEngine::electroPropertiesSolvent(double T, double &P, std::string solvent) const -> ElectroPropertiesSolvent { return pimpl->electro_properties_solvent_fn(T, P, P, solvent); } -auto ThermoEngine::propertiesSolvent(double T, double &P, std::string solvent) -> PropertiesSolvent +auto ThermoEngine::propertiesSolvent(double T, double &P, std::string solvent) const -> PropertiesSolvent { return pimpl->properties_solvent_fn(T, P, P, solvent); } -auto ThermoEngine::thermoPropertiesSubstance(double T, double &P, const Substance& substance) -> ThermoPropertiesSubstance +auto ThermoEngine::thermoPropertiesSubstance(double T, double &P, const Substance& substance) const -> ThermoPropertiesSubstance { return pimpl->thermoPropertiesSubstance(T, P, substance); } -auto ThermoEngine::electroPropertiesSolvent(double T, double &P, const Substance& solvent) -> ElectroPropertiesSolvent +auto ThermoEngine::electroPropertiesSolvent(double T, double &P, const Substance& solvent) const -> ElectroPropertiesSolvent { return pimpl->electroPropertiesSolvent(T, P, solvent); } -auto ThermoEngine::propertiesSolvent(double T, double &P, const Substance& solvent) -> PropertiesSolvent +auto ThermoEngine::propertiesSolvent(double T, double &P, const Substance& solvent) const -> PropertiesSolvent { return pimpl->propertiesSolvent(T, P, solvent); } // Reaction -auto ThermoEngine::thermoPropertiesReaction(double T, double &P, std::string reaction) -> ThermoPropertiesReaction +auto ThermoEngine::thermoPropertiesReaction(double T, double &P, std::string reaction) const -> ThermoPropertiesReaction { return pimpl->thermo_properties_reaction_fn(T, P, P, reaction); } -auto ThermoEngine::thermoPropertiesReactionFromReactants(double T, double &P, std::string symbol) -> ThermoPropertiesReaction +auto ThermoEngine::thermoPropertiesReactionFromReactants(double T, double &P, std::string symbol) const -> ThermoPropertiesReaction { return pimpl->thermoPropertiesReactionFromReactants(T, P, symbol); } -auto ThermoEngine::thermoPropertiesReaction(double T, double &P, const Reaction& reaction) -> ThermoPropertiesReaction +auto ThermoEngine::thermoPropertiesReaction(double T, double &P, const Reaction& reaction) const -> ThermoPropertiesReaction { return pimpl->thermoPropertiesReaction(T, P, reaction); } -auto ThermoEngine::thermoPropertiesReactionFromReactants(double T, double &P, const Reaction& reaction) -> ThermoPropertiesReaction +auto ThermoEngine::thermoPropertiesReactionFromReactants(double T, double &P, const Reaction& reaction) const -> ThermoPropertiesReaction { return pimpl->thermoPropertiesReactionFromReactants(T, P, reaction); } @@ -812,7 +812,7 @@ auto ThermoEngine::appendData(std::vector jsonRecords, std::string pimpl->database.appendData(jsonRecords, _label); } -auto ThermoEngine::parseSubstanceFormula(std::string formula) -> std::map +auto ThermoEngine::parseSubstanceFormula(std::string formula) const -> std::map { return pimpl->database.parseSubstanceFormula(formula); } diff --git a/ThermoFun/ThermoEngine.h b/ThermoFun/ThermoEngine.h index 38a2326..3841045 100644 --- a/ThermoFun/ThermoEngine.h +++ b/ThermoFun/ThermoEngine.h @@ -33,7 +33,7 @@ class ThermoEngine public: /** * @brief Construct a new Thermo Engine object - * + * * @param filename name/path of the file or a string containing a ThermoDataSet in JSON format */ explicit ThermoEngine(const std::string filename); @@ -73,67 +73,67 @@ class ThermoEngine /// @param T The temperature value (in units of K) /// @param P The pressure value (in units of Pa) /// @param substance The symbol of the substance - auto thermoPropertiesSubstance(double T, double &P, std::string substance) -> ThermoPropertiesSubstance; + auto thermoPropertiesSubstance(double T, double &P, std::string substance) const -> ThermoPropertiesSubstance; /// Calculate the thermodynamic properties of a substance. /// @param T The temperature value (in units of K) /// @param P The pressure value (in units of Pa) /// @param substance substance object - auto thermoPropertiesSubstance(double T, double &P, const Substance& substance) -> ThermoPropertiesSubstance; + auto thermoPropertiesSubstance(double T, double &P, const Substance& substance) const -> ThermoPropertiesSubstance; /// Calculate the electro-chemical properties of a substance. /// @param T The temperature value (in units of K) /// @param P The pressure value (in units of Pa) /// @param solvent The symbol of the substance solvent - auto electroPropertiesSolvent(double T, double &P, std::string solvent) -> ElectroPropertiesSolvent; + auto electroPropertiesSolvent(double T, double &P, std::string solvent) const -> ElectroPropertiesSolvent; /// Calculate the electro-chemical properties of a substance. /// @param T The temperature value (in units of K) /// @param P The pressure value (in units of Pa) /// @param solvent substance solvent object - auto electroPropertiesSolvent(double T, double &P, const Substance& solvent) -> ElectroPropertiesSolvent; + auto electroPropertiesSolvent(double T, double &P, const Substance& solvent) const -> ElectroPropertiesSolvent; /// Calculate the physical properties of a substance. /// @param T The temperature value (in units of K) /// @param P The pressure value (in units of Pa) /// @param solvent The symbol of the substance solvent - auto propertiesSolvent(double T, double &P, std::string solvent) -> PropertiesSolvent; + auto propertiesSolvent(double T, double &P, std::string solvent) const -> PropertiesSolvent; /// Calculate the physical properties of a substance. /// @param T The temperature value (in units of K) /// @param P The pressure value (in units of Pa) /// @param solvent substance solvent object - auto propertiesSolvent(double T, double &P, const Substance& solvent) -> PropertiesSolvent; + auto propertiesSolvent(double T, double &P, const Substance& solvent) const -> PropertiesSolvent; // Reaction /// Calculate the thermodynamic properties of a reaction. /// @param T The temperature value (in units of K) /// @param P The pressure value (in units of Pa) /// @param reaction The symbol of the reaction - auto thermoPropertiesReaction(double T, double &P, std::string reaction) -> ThermoPropertiesReaction; + auto thermoPropertiesReaction(double T, double &P, std::string reaction) const -> ThermoPropertiesReaction; /// Calculate the thermodynamic properties of a reaction. /// @param T The temperature value (in units of K) /// @param P The pressure value (in units of Pa) /// @param reaction the reaction object - auto thermoPropertiesReaction(double T, double &P, const Reaction& reaction) -> ThermoPropertiesReaction; + auto thermoPropertiesReaction(double T, double &P, const Reaction& reaction) const -> ThermoPropertiesReaction; /// Calculate the thermodynamic properties of a reaction from the substances participating in the reaction. /// @param T The temperature value (in units of K) /// @param P The pressure value (in units of Pa) /// @param reaction The symbol of the reaction - auto thermoPropertiesReactionFromReactants(double T, double &P, std::string reaction) -> ThermoPropertiesReaction; + auto thermoPropertiesReactionFromReactants(double T, double &P, std::string reaction) const -> ThermoPropertiesReaction; /// Calculate the thermodynamic properties of a reaction from the substances participating in the reaction. /// @param T The temperature value (in units of K) /// @param P The pressure value (in units of Pa) /// @param reaction the reaction object - auto thermoPropertiesReactionFromReactants(double T, double &P, const Reaction& reaction) -> ThermoPropertiesReaction; + auto thermoPropertiesReactionFromReactants(double T, double &P, const Reaction& reaction) const -> ThermoPropertiesReaction; /// Pareses a given substance formula present in the database /// @param formula /// @return map of Elements and coefficients - auto parseSubstanceFormula(std::string formula) -> std::map; + auto parseSubstanceFormula(std::string formula) const -> std::map; private: struct Impl; diff --git a/python/pyThermoFun/pyThermoEngine.cpp b/python/pyThermoFun/pyThermoEngine.cpp index 77aaea2..cee2e7c 100644 --- a/python/pyThermoFun/pyThermoEngine.cpp +++ b/python/pyThermoFun/pyThermoEngine.cpp @@ -35,17 +35,17 @@ void exportThermoEngine(py::module& m) auto appendData1 = static_cast(&ThermoEngine::appendData); auto appendData2 = static_cast, std::string)>(&ThermoEngine::appendData); - auto thermoPropertiesSubstance1 = static_cast(&ThermoEngine::thermoPropertiesSubstance); - auto electroPropertiesSolvent1 = static_cast(&ThermoEngine::electroPropertiesSolvent); - auto propertiesSolvent1 = static_cast(&ThermoEngine::propertiesSolvent); - auto thermoPropertiesReaction1 = static_cast(&ThermoEngine::thermoPropertiesReaction); - auto thermoPropertiesReactionFromReactants1 = static_cast(&ThermoEngine::thermoPropertiesReactionFromReactants); + auto thermoPropertiesSubstance1 = static_cast(&ThermoEngine::thermoPropertiesSubstance); + auto electroPropertiesSolvent1 = static_cast(&ThermoEngine::electroPropertiesSolvent); + auto propertiesSolvent1 = static_cast(&ThermoEngine::propertiesSolvent); + auto thermoPropertiesReaction1 = static_cast(&ThermoEngine::thermoPropertiesReaction); + auto thermoPropertiesReactionFromReactants1 = static_cast(&ThermoEngine::thermoPropertiesReactionFromReactants); - auto thermoPropertiesSubstance2 = static_cast(&ThermoEngine::thermoPropertiesSubstance); - auto electroPropertiesSolvent2 = static_cast(&ThermoEngine::electroPropertiesSolvent); - auto propertiesSolvent2 = static_cast(&ThermoEngine::propertiesSolvent); - auto thermoPropertiesReaction2 = static_cast(&ThermoEngine::thermoPropertiesReaction); - auto thermoPropertiesReactionFromReactants2 = static_cast(&ThermoEngine::thermoPropertiesReactionFromReactants); + auto thermoPropertiesSubstance2 = static_cast(&ThermoEngine::thermoPropertiesSubstance); + auto electroPropertiesSolvent2 = static_cast(&ThermoEngine::electroPropertiesSolvent); + auto propertiesSolvent2 = static_cast(&ThermoEngine::propertiesSolvent); + auto thermoPropertiesReaction2 = static_cast(&ThermoEngine::thermoPropertiesReaction); + auto thermoPropertiesReactionFromReactants2 = static_cast(&ThermoEngine::thermoPropertiesReactionFromReactants); py::class_(m, "ThermoEngine") // .def(py::init<>())