📄 valuelist.cpp
字号:
/******************************************************************************
文件名 :ValueList.cpp
版本号 : 1.0
作者 : Amos Peng
生成日期 :2008-07-08
最近修改 :
功能描述 :
函数列表 :
*******************************************************************************/
#include "ValueList.h"
using namespace ExprEval;
// Constructor
CValueList::CValueList()
{
}
// Destructor
CValueList::~CValueList()
{
Clear();
}
// Add value to list
void CValueList::Add(const ::std::string &name, double def, bool constant)
{
// Ensure value does not already exist
if(GetAddress(name))
{
throw(CAlreadyExistsException(name));
}
else
{
// Create value
auto_ptr<CValueListItem> i(new CValueListItem(name, def ,constant));
// Add value to list
m_values.push_back(i.get());
i.release();
}
}
// Add an external value to the list
void CValueList::AddAddress(const ::std::string &name, double *ptr, double def, bool constant)
{
if(GetAddress(name))
{
throw(CAlreadyExistsException(name));
}
else if(ptr == 0)
{
throw(CNullPointerException("ValueList::AddAddress"));
}
else
{
// Create value
auto_ptr<CValueListItem> i(new CValueListItem(name, ptr, def, constant));
// Add value to list
m_values.push_back(i.get());
i.release();
}
}
// Get the address of the value, internal or external
double* CValueList::GetAddress(const ::std::string &name) const
{
size_type pos;
for(pos = 0; pos < m_values.size(); pos++)
{
if(m_values[pos]->GetName() == name)
{
// The name matches
return m_values[pos]->GetAddress();
}
}
// No item found
return 0;
}
// Is the value a constant
bool CValueList::IsConstant(const ::std::string &name) const
{
size_type pos;
for(pos = 0; pos < m_values.size(); pos++)
{
if(m_values[pos]->GetName() == name && m_values[pos]->IsConstant())
{
return true;
}
}
return false;
}
// Number of values in the list
CValueList::size_type CValueList::Count() const
{
return m_values.size();
}
// Get an item
void CValueList::Item(size_type pos, ::std::string *name, double *value) const
{
if(name)
*name = m_values[pos]->GetName();
if(value)
*value = *(m_values[pos]->GetAddress());
}
// Add some default values
void CValueList::AddDefaultValues()
{
// Math constant 'e'
Add("E", EXPREVAL_E, true);
// Math constant PI
Add("PI", EXPREVAL_PI, true);
}
// Reset values
void CValueList::Reset()
{
size_type pos;
for(pos = 0; pos < m_values.size(); pos++)
{
m_values[pos]->Reset();
}
}
// Free values
void CValueList::Clear()
{
size_type pos;
for(pos = 0; pos < m_values.size(); pos++)
{
delete m_values[pos];
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -