Skip to content

Commit

Permalink
Merge pull request #21 from allanleal/improve-const-correctness
Browse files Browse the repository at this point in the history
Improve const-correctness for classes Database and ThermoEngine
  • Loading branch information
gdmiron authored Apr 24, 2020
2 parents 6993519 + a28b89a commit ec2cb95
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 70 deletions.
34 changes: 17 additions & 17 deletions ThermoFun/Database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ struct Database::Impl
}

template<typename Key, typename Value>
auto collectValues(const std::map<Key, Value>& map) -> std::vector<Value>
auto collectValues(const std::map<Key, Value>& map) const -> std::vector<Value>
{
std::vector<Value> collection;
collection.reserve(map.size());
Expand Down Expand Up @@ -114,34 +114,34 @@ struct Database::Impl
reactions_map = reactions;
}

auto getElements() -> std::vector<Element>
auto getElements() const -> std::vector<Element>
{
return collectValues(elements_map);
}

auto getSubstances() -> std::vector<Substance>
auto getSubstances() const -> std::vector<Substance>
{
return collectValues(substances_map);
}

auto getReactions() -> std::vector<Reaction>
auto getReactions() const -> std::vector<Reaction>
{
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&
Expand Down Expand Up @@ -426,32 +426,32 @@ auto Database::mapReactions() const -> const ReactionsMap&
return pimpl->mapReactions();
}

auto Database::getElements() -> std::vector<Element>
auto Database::getElements() const -> std::vector<Element>
{
return pimpl->getElements();
}

auto Database::getSubstances() -> std::vector<Substance>
auto Database::getSubstances() const -> std::vector<Substance>
{
return pimpl->getSubstances();
}

auto Database::getReactions() -> std::vector<Reaction>
auto Database::getReactions() const -> std::vector<Reaction>
{
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();
}
Expand All @@ -471,7 +471,7 @@ auto Database::containsReaction(std::string symbol) const -> bool
return pimpl->containsReaction(symbol);
}

auto Database::parseSubstanceFormula(std::string formula_) -> std::map<Element, double>
auto Database::parseSubstanceFormula(std::string formula_) const -> std::map<Element, double>
{
std::set<ElementKey> elements;
std::map<Element, double> map;
Expand Down
16 changes: 8 additions & 8 deletions ThermoFun/Database.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -98,13 +98,13 @@ class Database
auto addMapReactions(const ReactionsMap& reactions) -> void;

/// Return all elements in the database
auto getElements() -> std::vector<Element>;
auto getElements() const -> std::vector<Element>;

/// Return all substances in the database
auto getSubstances() -> std::vector<Substance>;
auto getSubstances() const -> std::vector<Substance>;

/// Return all reactions in the database
auto getReactions() -> std::vector<Reaction>;
auto getReactions() const -> std::vector<Reaction>;

/// Returns the map of elements in the database
auto mapElements() const -> const ElementsMap&;
Expand All @@ -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&;
Expand All @@ -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<Element, double>;
auto parseSubstanceFormula(std::string formula) const -> std::map<Element, double>;

private:
struct Impl;
Expand Down
46 changes: 23 additions & 23 deletions ThermoFun/ThermoEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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))
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -812,7 +812,7 @@ auto ThermoEngine::appendData(std::vector<std::string> jsonRecords, std::string
pimpl->database.appendData(jsonRecords, _label);
}

auto ThermoEngine::parseSubstanceFormula(std::string formula) -> std::map<Element, double>
auto ThermoEngine::parseSubstanceFormula(std::string formula) const -> std::map<Element, double>
{
return pimpl->database.parseSubstanceFormula(formula);
}
Expand Down
Loading

0 comments on commit ec2cb95

Please sign in to comment.