preprocessor: factorize code that checks for valid symb_id

issue#70
Houtan Bastani 2017-09-12 14:16:29 +02:00
parent 7c884bcae2
commit 4213c42b96
2 changed files with 23 additions and 28 deletions

View File

@ -156,8 +156,7 @@ SymbolTable::changeType(int id, SymbolType newtype) throw (UnknownSymbolIDExcept
if (frozen) if (frozen)
throw FrozenException(); throw FrozenException();
if (id < 0 || id > symbol_table.size()) validateSymbID(id);
throw UnknownSymbolIDException(id);
type_table[id] = newtype; type_table[id] = newtype;
} }
@ -732,8 +731,8 @@ SymbolTable::getAuxiliaryVarsExprNode(int symb_id) const throw (SearchFailedExce
void void
SymbolTable::markPredetermined(int symb_id) throw (UnknownSymbolIDException, FrozenException) SymbolTable::markPredetermined(int symb_id) throw (UnknownSymbolIDException, FrozenException)
{ {
if (symb_id < 0 || symb_id > symbol_table.size()) validateSymbID(symb_id);
throw UnknownSymbolIDException(symb_id);
if (frozen) if (frozen)
throw FrozenException(); throw FrozenException();
@ -745,9 +744,7 @@ SymbolTable::markPredetermined(int symb_id) throw (UnknownSymbolIDException, Fro
bool bool
SymbolTable::isPredetermined(int symb_id) const throw (UnknownSymbolIDException) SymbolTable::isPredetermined(int symb_id) const throw (UnknownSymbolIDException)
{ {
if (symb_id < 0 || symb_id > symbol_table.size()) validateSymbID(symb_id);
throw UnknownSymbolIDException(symb_id);
return (predetermined_variables.find(symb_id) != predetermined_variables.end()); return (predetermined_variables.find(symb_id) != predetermined_variables.end());
} }
@ -760,9 +757,7 @@ SymbolTable::predeterminedNbr() const
void void
SymbolTable::addObservedVariable(int symb_id) throw (UnknownSymbolIDException) SymbolTable::addObservedVariable(int symb_id) throw (UnknownSymbolIDException)
{ {
if (symb_id < 0 || symb_id > symbol_table.size()) validateSymbID(symb_id);
throw UnknownSymbolIDException(symb_id);
assert(getType(symb_id) == eEndogenous); assert(getType(symb_id) == eEndogenous);
varobs.push_back(symb_id); varobs.push_back(symb_id);
} }

View File

@ -219,6 +219,8 @@ private:
int addLeadAuxiliaryVarInternal(bool endo, int index, expr_t arg) throw (FrozenException); int addLeadAuxiliaryVarInternal(bool endo, int index, expr_t arg) throw (FrozenException);
//! Factorized code for Json writing //! Factorized code for Json writing
void writeJsonVarVector(ostream &output, const vector<int> &varvec) const; void writeJsonVarVector(ostream &output, const vector<int> &varvec) const;
//! Factorized code for asserting that 0 <= symb_id <= symbol_table.size()
inline void validateSymbID(int symb_id) const throw (UnknownSymbolIDException);
public: public:
//! Add a symbol //! Add a symbol
/*! Returns the symbol ID */ /*! Returns the symbol ID */
@ -362,6 +364,13 @@ public:
set <int> getOrigEndogenous() const; set <int> getOrigEndogenous() const;
}; };
inline void
SymbolTable::validateSymbID(int symb_id) const throw (UnknownSymbolIDException)
{
if (symb_id < 0 || symb_id > symbol_table.size())
throw UnknownSymbolIDException(symb_id);
}
inline bool inline bool
SymbolTable::exists(const string &name) const SymbolTable::exists(const string &name) const
{ {
@ -372,37 +381,29 @@ SymbolTable::exists(const string &name) const
inline string inline string
SymbolTable::getName(int id) const throw (UnknownSymbolIDException) SymbolTable::getName(int id) const throw (UnknownSymbolIDException)
{ {
if (id < 0 || id > symbol_table.size()) validateSymbID(id);
throw UnknownSymbolIDException(id); return name_table[id];
else
return name_table[id];
} }
inline string inline string
SymbolTable::getTeXName(int id) const throw (UnknownSymbolIDException) SymbolTable::getTeXName(int id) const throw (UnknownSymbolIDException)
{ {
if (id < 0 || id > symbol_table.size()) validateSymbID(id);
throw UnknownSymbolIDException(id); return tex_name_table[id];
else
return tex_name_table[id];
} }
inline string inline string
SymbolTable::getLongName(int id) const throw (UnknownSymbolIDException) SymbolTable::getLongName(int id) const throw (UnknownSymbolIDException)
{ {
if (id < 0 || id > symbol_table.size()) validateSymbID(id);
throw UnknownSymbolIDException(id); return long_name_table[id];
else
return long_name_table[id];
} }
inline SymbolType inline SymbolType
SymbolTable::getType(int id) const throw (UnknownSymbolIDException) SymbolTable::getType(int id) const throw (UnknownSymbolIDException)
{ {
if (id < 0 || id > symbol_table.size()) validateSymbID(id);
throw UnknownSymbolIDException(id); return type_table[id];
else
return type_table[id];
} }
inline SymbolType inline SymbolType
@ -427,8 +428,7 @@ SymbolTable::getTypeSpecificID(int id) const throw (UnknownSymbolIDException, No
if (!frozen) if (!frozen)
throw NotYetFrozenException(); throw NotYetFrozenException();
if (id < 0 || id > symbol_table.size()) validateSymbID(id);
throw UnknownSymbolIDException(id);
return type_specific_ids[id]; return type_specific_ids[id];
} }