Skip to content

Commit

Permalink
[base] Make IndexError consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
ischoegl authored and speth committed Feb 14, 2025
1 parent fd1a410 commit 0720efb
Show file tree
Hide file tree
Showing 12 changed files with 29 additions and 31 deletions.
3 changes: 2 additions & 1 deletion include/cantera/base/Solution.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ class Solution : public std::enable_shared_from_this<Solution>
//! Get the name of an adjacent phase by index
string adjacentName(size_t i) const {
if (i >= m_adjacent.size()) {
throw IndexError("Solution::adjacentName", "m_adjacent", i, m_adjacent.size()-1);
throw IndexError("Solution::adjacentName", "m_adjacent",
i, m_adjacent.size());
}
return m_adjacent.at(i)->name();
}
Expand Down
18 changes: 8 additions & 10 deletions include/cantera/base/ctexceptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,16 +175,13 @@ class IndexError : public CanteraError
/*!
* This class indicates an out-of-bounds array index.
*
* @param func String name for the function within which the error was
* generated.
* @param arrayName name of the corresponding array
* @param m This is the value of the out-of-bounds index.
* @param mmax This is the maximum allowed value of the index. The
* minimum allowed value is assumed to be 0. The special
* value npos indicates that the array is empty.
* @param func String name for the function within which the error was generated.
* @param arrayName Name of the corresponding array or empty string @c "".
* @param m Value of the out-of-bounds index.
* @param arrSize Size of the array.
*/
IndexError(const string& func, const string& arrayName, size_t m, size_t mmax) :
CanteraError(func), arrayName_(arrayName), m_(m), mmax_(mmax) {}
IndexError(const string& func, const string& arrayName, size_t m, size_t arrSize) :
CanteraError(func), arrayName_(arrayName), m_(m), m_size(arrSize) {}

~IndexError() throw() override {};
string getMessage() const override;
Expand All @@ -194,7 +191,8 @@ class IndexError : public CanteraError

private:
string arrayName_;
size_t m_, mmax_;
size_t m_;
size_t m_size;
};

//! An error indicating that an unimplemented function has been called
Expand Down
4 changes: 2 additions & 2 deletions include/cantera/oneD/Domain1D.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class Domain1D
//! Throws an exception if n is greater than nComponents()-1
void checkComponentIndex(size_t n) const {
if (n >= m_nv) {
throw IndexError("Domain1D::checkComponentIndex", "points", n, m_nv-1);
throw IndexError("Domain1D::checkComponentIndex", "points", n, m_nv);
}
}

Expand All @@ -175,7 +175,7 @@ class Domain1D
//! Throws an exception if n is greater than nPoints()-1
void checkPointIndex(size_t n) const {
if (n >= m_points) {
throw IndexError("Domain1D::checkPointIndex", "points", n, m_points-1);
throw IndexError("Domain1D::checkPointIndex", "points", n, m_points);
}
}

Expand Down
3 changes: 1 addition & 2 deletions include/cantera/oneD/OneDim.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ class OneDim
//! Throws an exception if n is greater than nDomains()-1
void checkDomainIndex(size_t n) const {
if (n >= m_dom.size()) {
throw IndexError("OneDim::checkDomainIndex", "domains", n,
m_dom.size()-1);
throw IndexError("OneDim::checkDomainIndex", "domains", n, m_dom.size());
}
}

Expand Down
2 changes: 1 addition & 1 deletion interfaces/sourcegen/sourcegen/clib/templates.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ clib-accessor: |-
{% if checks %}
## accessor uses index checker (see: sol3_adjacent)
if ({{ c_args[1] }} < 0 || {{ c_args[1] }} >= {{ base }}Cabinet::at({{ handle }})->{{ checks[0] }}()) {
throw IndexError("{{ c_func }}", "", {{ c_args[1] }}, {{ base }}Cabinet::at({{ handle }})->{{ checks[0] }}() - 1);
throw IndexError("{{ c_func }}", "", {{ c_args[1] }}, {{ base }}Cabinet::at({{ handle }})->{{ checks[0] }}());
}
{% endif %}{# checks #}
{% if shared %}
Expand Down
2 changes: 1 addition & 1 deletion src/base/SolutionArray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,7 @@ void SolutionArray::setLoc(int loc, bool restore)
} else if (static_cast<size_t>(m_active[loc_]) == m_loc) {
return;
} else if (loc_ >= m_size) {
throw IndexError("SolutionArray::setLoc", "indices", loc_, m_size - 1);
throw IndexError("SolutionArray::setLoc", "indices", loc_, m_size);
}
m_loc = static_cast<size_t>(m_active[loc_]);
if (restore) {
Expand Down
7 changes: 4 additions & 3 deletions src/base/ctexceptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,16 @@ string ArraySizeError::getMessage() const

string IndexError::getMessage() const
{
if (mmax_ == npos) {
if (m_size == 0) {
return fmt::format("IndexError: index {} given, but array{} is empty.",
m_, arrayName_.empty() ? arrayName_ : " "+arrayName_);
}
if (arrayName_ == "") {
return fmt::format("IndexError: {} outside valid range of 0 to {}.", m_, mmax_);
return fmt::format("IndexError: {} outside valid range of 0 to {}.",
m_, m_size - 1);
}
return fmt::format("IndexError: {}[{}] outside valid range of 0 to {}.",
arrayName_, m_, mmax_);
arrayName_, m_, m_size - 1);
}

} // namespace Cantera
6 changes: 3 additions & 3 deletions src/equil/MultiPhase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ ThermoPhase& MultiPhase::phase(size_t n)
void MultiPhase::checkPhaseIndex(size_t m) const
{
if (m >= nPhases()) {
throw IndexError("MultiPhase::checkPhaseIndex", "phase", m, nPhases()-1);
throw IndexError("MultiPhase::checkPhaseIndex", "phase", m, nPhases());
}
}

Expand Down Expand Up @@ -720,7 +720,7 @@ void MultiPhase::setTemperature(const double T)
void MultiPhase::checkElementIndex(size_t m) const
{
if (m >= m_nel) {
throw IndexError("MultiPhase::checkElementIndex", "elements", m, m_nel-1);
throw IndexError("MultiPhase::checkElementIndex", "elements", m, m_nel);
}
}

Expand Down Expand Up @@ -749,7 +749,7 @@ size_t MultiPhase::elementIndex(const string& name) const
void MultiPhase::checkSpeciesIndex(size_t k) const
{
if (k >= m_nsp) {
throw IndexError("MultiPhase::checkSpeciesIndex", "species", k, m_nsp-1);
throw IndexError("MultiPhase::checkSpeciesIndex", "species", k, m_nsp);
}
}

Expand Down
7 changes: 3 additions & 4 deletions src/kinetics/Kinetics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ namespace Cantera
void Kinetics::checkReactionIndex(size_t i) const
{
if (i >= nReactions()) {
throw IndexError("Kinetics::checkReactionIndex", "reactions", i,
nReactions()-1);
throw IndexError("Kinetics::checkReactionIndex", "reactions", i, nReactions());
}
}

Expand Down Expand Up @@ -62,7 +61,7 @@ void Kinetics::checkReactionArraySize(size_t ii) const
void Kinetics::checkPhaseIndex(size_t m) const
{
if (m >= nPhases()) {
throw IndexError("Kinetics::checkPhaseIndex", "phase", m, nPhases()-1);
throw IndexError("Kinetics::checkPhaseIndex", "phase", m, nPhases());
}
}

Expand All @@ -81,7 +80,7 @@ shared_ptr<ThermoPhase> Kinetics::reactionPhase() const
void Kinetics::checkSpeciesIndex(size_t k) const
{
if (k >= m_kk) {
throw IndexError("Kinetics::checkSpeciesIndex", "species", k, m_kk-1);
throw IndexError("Kinetics::checkSpeciesIndex", "species", k, m_kk);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/thermo/Phase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ size_t Phase::nElements() const
void Phase::checkElementIndex(size_t m) const
{
if (m >= m_mm) {
throw IndexError("Phase::checkElementIndex", "elements", m, m_mm-1);
throw IndexError("Phase::checkElementIndex", "elements", m, m_mm);
}
}

Expand Down Expand Up @@ -153,7 +153,7 @@ const vector<string>& Phase::speciesNames() const
void Phase::checkSpeciesIndex(size_t k) const
{
if (k >= m_kk) {
throw IndexError("Phase::checkSpeciesIndex", "species", k, m_kk-1);
throw IndexError("Phase::checkSpeciesIndex", "species", k, m_kk);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/transport/Transport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Cantera
void Transport::checkSpeciesIndex(size_t k) const
{
if (k >= m_nsp) {
throw IndexError("Transport::checkSpeciesIndex", "species", k, m_nsp-1);
throw IndexError("Transport::checkSpeciesIndex", "species", k, m_nsp);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/zeroD/ReactorNet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ double ReactorNet::sensitivity(size_t k, size_t p)
}
if (p >= m_sens_params.size()) {
throw IndexError("ReactorNet::sensitivity",
"m_sens_params", p, m_sens_params.size()-1);
"m_sens_params", p, m_sens_params.size());
}
double denom = m_integ->solution(k);
if (denom == 0.0) {
Expand Down

0 comments on commit 0720efb

Please sign in to comment.