1
0
mirror of https://github.com/aportelli/LatAnalyze.git synced 2024-11-10 00:45:36 +00: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_
#define LATAN_GLOBAL_HPP_
#include <map>
#include <string>
#include <latan/Eigen/Dense>
@ -46,6 +47,13 @@ std::string strFrom(T x)
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
#include <latan/Exceptions.hpp>

View File

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

View File

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