1
0
mirror of https://github.com/aportelli/LatAnalyze.git synced 2025-04-11 03:20:46 +01:00

key existence test in maps

This commit is contained in:
Antonin Portelli 2014-01-23 15:06:50 +01:00
parent ebb82aeefe
commit 249facbe69
3 changed files with 27 additions and 12 deletions

View File

@ -1,6 +1,7 @@
#ifndef LATAN_GLOBAL_HPP_ #ifndef LATAN_GLOBAL_HPP_
#define LATAN_GLOBAL_HPP_ #define LATAN_GLOBAL_HPP_
#include <map>
#include <string> #include <string>
#include <latan/Eigen/Dense> #include <latan/Eigen/Dense>
@ -46,6 +47,13 @@ std::string strFrom(T x)
return stream.str(); return stream.str();
} }
// key test in maps
template <typename K, typename T>
bool keyExists(const K &key, const std::map<K, T> &map)
{
return (map.find(key) != map.end());
}
LATAN_END_CPPDECL LATAN_END_CPPDECL
#include <latan/Exceptions.hpp> #include <latan/Exceptions.hpp>

View File

@ -52,8 +52,8 @@ public:
protected: protected:
// data access // data access
void deleteData(void); void deleteData(void);
template <typename IoObj> template <typename IoT>
const IoObj& getData(const std::string dataName); const IoT& getData(const std::string dataName);
protected: protected:
std::string name_; std::string name_;
unsigned int mode_; unsigned int mode_;
@ -61,12 +61,12 @@ protected:
}; };
// Template implementations // Template implementations
template <typename IoObj> template <typename IoT>
const IoObj& File::getData(const std::string dataName) const IoT& File::getData(const std::string dataName)
{ {
if (data_.find(dataName) != data_.end()) if (keyExists(dataName, data_))
{ {
return dynamic_cast<const IoObj&>(*(data_[dataName])); return dynamic_cast<const IoT&>(*(data_[dataName]));
} }
else else
{ {
@ -103,8 +103,8 @@ public:
// destructor // destructor
virtual ~AsciiFile(void); virtual ~AsciiFile(void);
// access // access
template <typename IoObj> template <typename IoT>
const IoObj& read(const std::string name); const IoT& read(const std::string name);
// tests // tests
virtual bool isOpen(void) const; virtual bool isOpen(void) const;
// Io // Io
@ -124,8 +124,8 @@ private:
}; };
// Template implementations // Template implementations
template <typename IoObj> template <typename IoT>
const IoObj& AsciiFile::read(const std::string dataName) const IoT& AsciiFile::read(const std::string dataName)
{ {
if ((mode_ & FileMode::read)&&(isOpen())) if ((mode_ & FileMode::read)&&(isOpen()))
{ {
@ -134,7 +134,7 @@ const IoObj& AsciiFile::read(const std::string dataName)
parse(); parse();
} }
return getData<IoObj>(dataName); return getData<IoT>(dataName);
} }
else else
{ {

View File

@ -118,7 +118,14 @@ void Push::operator()(std::stack<double> &dStack, VarTable &vTable)
} }
else else
{ {
dStack.push(vTable[name_]); if (keyExists(name_, vTable))
{
dStack.push(vTable[name_]);
}
else
{
LATAN_ERROR(Range, "unknown variable '" + name_ + "'");
}
} }
} }