icinga2/lib/config/expression.cpp

619 lines
18 KiB
C++
Raw Normal View History

/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software Foundation *
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/
2014-10-16 17:44:06 +02:00
#include "config/expression.hpp"
2014-05-25 16:23:35 +02:00
#include "config/configitem.hpp"
#include "config/configitembuilder.hpp"
#include "config/applyrule.hpp"
#include "config/objectrule.hpp"
#include "base/array.hpp"
2014-10-26 19:59:49 +01:00
#include "base/json.hpp"
2014-05-25 16:23:35 +02:00
#include "base/scriptfunction.hpp"
#include "base/scriptvariable.hpp"
#include "base/utility.hpp"
#include "base/objectlock.hpp"
#include "base/object.hpp"
2014-10-19 14:21:12 +02:00
#include "base/logger.hpp"
#include "base/configerror.hpp"
#include <boost/foreach.hpp>
#include <boost/exception_ptr.hpp>
2014-11-06 19:35:47 +01:00
#include <boost/lexical_cast.hpp>
#include <boost/exception/errinfo_nested_exception.hpp>
using namespace icinga;
2014-11-09 19:48:28 +01:00
Expression::~Expression(void)
{ }
2014-11-06 19:35:47 +01:00
Value Expression::Evaluate(const Object::Ptr& context, DebugHint *dhint) const
{
try {
#ifdef _DEBUG
2014-11-09 19:48:28 +01:00
std::ostringstream msgbuf;
ShowCodeFragment(msgbuf, GetDebugInfo(), false);
Log(LogDebug, "Expression")
<< "Executing:\n" << msgbuf.str();
#endif /* _DEBUG */
2014-11-09 19:48:28 +01:00
return DoEvaluate(context, dhint);
} catch (const std::exception& ex) {
if (boost::get_error_info<boost::errinfo_nested_exception>(ex))
throw;
else
2014-11-09 19:48:28 +01:00
BOOST_THROW_EXCEPTION(ConfigError("Error while evaluating expression: " + String(ex.what()))
<< boost::errinfo_nested_exception(boost::current_exception())
<< errinfo_debuginfo(GetDebugInfo()));
}
}
2014-11-09 19:48:28 +01:00
const DebugInfo& Expression::GetDebugInfo(void) const
{
2014-11-09 19:48:28 +01:00
static DebugInfo debugInfo;
return debugInfo;
}
2014-11-09 19:48:28 +01:00
std::vector<Expression *> icinga::MakeIndexer(const String& index1)
{
2014-11-09 19:48:28 +01:00
std::vector<Expression *> result;
result.push_back(MakeLiteral(index1));
return result;
}
2014-11-09 19:48:28 +01:00
void DictExpression::MakeInline(void)
{
2014-11-09 19:48:28 +01:00
m_Inline = true;
}
2014-11-09 19:48:28 +01:00
LiteralExpression::LiteralExpression(const Value& value)
: m_Value(value)
{ }
Value LiteralExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
{
2014-11-09 19:48:28 +01:00
return m_Value;
}
2014-11-09 19:48:28 +01:00
const DebugInfo& DebuggableExpression::GetDebugInfo(void) const
{
2014-11-09 19:48:28 +01:00
return m_DebugInfo;
}
2014-11-09 19:48:28 +01:00
Value VariableExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
{
2014-11-06 19:35:47 +01:00
Object::Ptr scope = context;
2014-03-24 09:10:37 +01:00
while (scope) {
2014-11-09 19:48:28 +01:00
if (HasField(scope, m_Variable))
return GetField(scope, m_Variable);
2014-03-24 09:10:37 +01:00
2014-11-06 19:35:47 +01:00
scope = GetField(scope, "__parent");
2014-03-24 09:10:37 +01:00
}
2014-11-09 19:48:28 +01:00
return ScriptVariable::Get(m_Variable);
}
2014-11-09 19:48:28 +01:00
Value NegateExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
{
2014-11-09 19:48:28 +01:00
return ~(long)m_Operand->Evaluate(context);
}
2014-11-09 19:48:28 +01:00
Value LogicalNegateExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
{
2014-11-09 19:48:28 +01:00
return !m_Operand->Evaluate(context).ToBool();
}
2014-11-09 19:48:28 +01:00
Value AddExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
{
2014-11-09 19:48:28 +01:00
return m_Operand1->Evaluate(context) + m_Operand2->Evaluate(context);
}
2014-11-09 19:48:28 +01:00
Value SubtractExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
{
2014-11-09 19:48:28 +01:00
return m_Operand1->Evaluate(context) - m_Operand2->Evaluate(context);
}
2014-11-09 19:48:28 +01:00
Value MultiplyExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
{
2014-11-09 19:48:28 +01:00
return m_Operand1->Evaluate(context) * m_Operand2->Evaluate(context);
}
2014-11-09 19:48:28 +01:00
Value DivideExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
{
2014-11-09 19:48:28 +01:00
return m_Operand1->Evaluate(context) / m_Operand2->Evaluate(context);
}
2014-11-09 19:48:28 +01:00
Value BinaryAndExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
{
2014-11-09 19:48:28 +01:00
return m_Operand1->Evaluate(context) & m_Operand2->Evaluate(context);
}
2014-11-09 19:48:28 +01:00
Value BinaryOrExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
{
2014-11-09 19:48:28 +01:00
return m_Operand1->Evaluate(context) | m_Operand2->Evaluate(context);
}
2014-11-09 19:48:28 +01:00
Value ShiftLeftExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
{
2014-11-09 19:48:28 +01:00
return m_Operand1->Evaluate(context) << m_Operand2->Evaluate(context);
}
2014-11-09 19:48:28 +01:00
Value ShiftRightExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
{
2014-11-09 19:48:28 +01:00
return m_Operand1->Evaluate(context) >> m_Operand2->Evaluate(context);
}
2014-11-09 19:48:28 +01:00
Value EqualExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
{
2014-11-09 19:48:28 +01:00
return m_Operand1->Evaluate(context) == m_Operand2->Evaluate(context);
}
2014-11-09 19:48:28 +01:00
Value NotEqualExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
{
2014-11-09 19:48:28 +01:00
return m_Operand1->Evaluate(context) != m_Operand2->Evaluate(context);
}
2014-11-09 19:48:28 +01:00
Value LessThanExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
{
2014-11-09 19:48:28 +01:00
return m_Operand1->Evaluate(context) < m_Operand2->Evaluate(context);
}
2014-11-09 19:48:28 +01:00
Value GreaterThanExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
{
2014-11-09 19:48:28 +01:00
return m_Operand1->Evaluate(context) > m_Operand2->Evaluate(context);
}
2014-11-09 19:48:28 +01:00
Value LessThanOrEqualExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
{
2014-11-09 19:48:28 +01:00
return m_Operand1->Evaluate(context) <= m_Operand2->Evaluate(context);
}
2014-11-09 19:48:28 +01:00
Value GreaterThanOrEqualExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
{
2014-11-09 19:48:28 +01:00
return m_Operand1->Evaluate(context) >= m_Operand2->Evaluate(context);
}
2014-11-09 19:48:28 +01:00
Value InExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
{
2014-11-09 19:48:28 +01:00
Value right = m_Operand2->Evaluate(context);
2014-03-28 12:17:22 +01:00
if (right.IsEmpty())
return false;
else if (!right.IsObjectType<Array>())
2014-10-26 19:59:49 +01:00
BOOST_THROW_EXCEPTION(ConfigError("Invalid right side argument for 'in' operator: " + JsonEncode(right)));
2014-11-09 19:48:28 +01:00
Value left = m_Operand1->Evaluate(context);
Array::Ptr arr = right;
return arr->Contains(left);
}
2014-11-09 19:48:28 +01:00
Value NotInExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
{
2014-11-09 19:48:28 +01:00
Value right = m_Operand2->Evaluate(context);
if (right.IsEmpty())
return false;
else if (!right.IsObjectType<Array>())
BOOST_THROW_EXCEPTION(ConfigError("Invalid right side argument for 'in' operator: " + JsonEncode(right)));
Value left = m_Operand1->Evaluate(context);
Array::Ptr arr = right;
return !arr->Contains(left);
}
2014-11-09 19:48:28 +01:00
Value LogicalAndExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
{
2014-11-09 19:48:28 +01:00
return m_Operand1->Evaluate(context).ToBool() && m_Operand2->Evaluate(context).ToBool();
}
2014-11-09 19:48:28 +01:00
Value LogicalOrExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
{
2014-11-09 19:48:28 +01:00
return m_Operand1->Evaluate(context).ToBool() || m_Operand2->Evaluate(context).ToBool();
}
2014-11-09 19:48:28 +01:00
Value FunctionCallExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
{
2014-11-09 19:48:28 +01:00
Value funcName = m_FName->Evaluate(context);
ScriptFunction::Ptr func;
if (funcName.IsObjectType<ScriptFunction>())
func = funcName;
else
func = ScriptFunction::GetByName(funcName);
if (!func)
BOOST_THROW_EXCEPTION(ConfigError("Function '" + funcName + "' does not exist."));
std::vector<Value> arguments;
2014-11-09 19:48:28 +01:00
BOOST_FOREACH(Expression *arg, m_Args) {
arguments.push_back(arg->Evaluate(context));
}
return func->Invoke(arguments);
}
2014-11-09 19:48:28 +01:00
Value ArrayExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
{
Array::Ptr result = new Array();
2014-11-09 19:48:28 +01:00
BOOST_FOREACH(Expression *aexpr, m_Expressions) {
result->Add(aexpr->Evaluate(context));
}
return result;
2013-11-03 10:41:30 +01:00
}
2014-11-09 19:48:28 +01:00
Value DictExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
{
Dictionary::Ptr result = new Dictionary();
2014-11-06 19:35:47 +01:00
result->Set("__parent", context);
2014-03-24 09:10:37 +01:00
2014-11-09 19:48:28 +01:00
BOOST_FOREACH(Expression *aexpr, m_Expressions) {
Object::Ptr acontext = m_Inline ? context : result;
aexpr->Evaluate(acontext, dhint);
2014-11-09 19:48:28 +01:00
if (HasField(acontext, "__result"))
break;
}
Dictionary::Ptr xresult = result->ShallowClone();
xresult->Remove("__parent");
return xresult;
}
2014-11-09 19:48:28 +01:00
Value SetExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
{
DebugHint *sdhint = dhint;
Value parent, object;
String index;
2014-11-09 19:48:28 +01:00
for (Array::SizeType i = 0; i < m_Indexer.size(); i++) {
Expression *indexExpr = m_Indexer[i];
2014-11-06 19:35:47 +01:00
String tempindex = indexExpr->Evaluate(context, dhint);
2014-11-09 19:48:28 +01:00
if (i == m_Indexer.size() - 1)
index = tempindex;
if (i == 0) {
2014-11-06 19:35:47 +01:00
parent = context;
object = GetField(context, tempindex);
} else {
parent = object;
2014-11-09 19:48:28 +01:00
LiteralExpression *eparent = MakeLiteral(parent);
LiteralExpression *eindex = MakeLiteral(tempindex);
2014-11-09 19:48:28 +01:00
IndexerExpression eip(eparent, eindex, m_DebugInfo);
object = eip.Evaluate(context, dhint);
}
if (sdhint)
sdhint = sdhint->GetChild(index);
2014-11-09 19:48:28 +01:00
if (i != m_Indexer.size() - 1 && object.IsEmpty()) {
object = new Dictionary();
2014-11-06 19:35:47 +01:00
SetField(parent, tempindex, object);
}
}
2014-11-09 19:48:28 +01:00
Value right = m_Operand2->Evaluate(context, dhint);
2014-11-09 19:48:28 +01:00
if (m_Op != OpSetLiteral) {
Expression *lhs = MakeLiteral(object);
Expression *rhs = MakeLiteral(right);
2014-11-09 19:48:28 +01:00
switch (m_Op) {
case OpSetAdd:
2014-11-09 19:48:28 +01:00
right = AddExpression(lhs, rhs, m_DebugInfo).Evaluate(context, dhint);
break;
case OpSetSubtract:
2014-11-09 19:48:28 +01:00
right = SubtractExpression(lhs, rhs, m_DebugInfo).Evaluate(context, dhint);
break;
case OpSetMultiply:
2014-11-09 19:48:28 +01:00
right = MultiplyExpression(lhs, rhs, m_DebugInfo).Evaluate(context, dhint);
break;
case OpSetDivide:
2014-11-09 19:48:28 +01:00
right = DivideExpression(lhs, rhs, m_DebugInfo).Evaluate(context, dhint);
break;
default:
VERIFY(!"Invalid opcode.");
}
}
2014-11-06 19:35:47 +01:00
SetField(parent, index, right);
if (sdhint)
2014-11-09 19:48:28 +01:00
sdhint->AddMessage("=", m_DebugInfo);
return right;
}
2014-11-09 19:48:28 +01:00
Value IndexerExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
{
2014-11-09 19:48:28 +01:00
Value value = m_Operand1->Evaluate(context);
Value index = m_Operand2->Evaluate(context);
2014-03-28 12:17:22 +01:00
if (value.IsObjectType<Dictionary>()) {
Dictionary::Ptr dict = value;
return dict->Get(index);
} else if (value.IsObjectType<Array>()) {
Array::Ptr arr = value;
return arr->Get(index);
} else if (value.IsObject()) {
2014-03-28 12:17:22 +01:00
Object::Ptr object = value;
2014-11-03 00:44:04 +01:00
Type::Ptr type = object->GetReflectionType();
2014-03-28 12:17:22 +01:00
if (!type)
BOOST_THROW_EXCEPTION(ConfigError("Dot operator applied to object which does not support reflection"));
int field = type->GetFieldId(index);
if (field == -1)
BOOST_THROW_EXCEPTION(ConfigError("Tried to access invalid property '" + index + "'"));
return object->GetField(field);
2014-03-28 16:32:15 +01:00
} else if (value.IsEmpty()) {
return Empty;
2014-03-28 12:17:22 +01:00
} else {
BOOST_THROW_EXCEPTION(ConfigError("Dot operator cannot be applied to type '" + value.GetTypeName() + "'"));
}
}
2014-11-09 19:48:28 +01:00
Value ImportExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
{
2014-11-09 19:48:28 +01:00
Value type = m_Type->Evaluate(context);
Value name = m_Name->Evaluate(context);
ConfigItem::Ptr item = ConfigItem::GetObject(type, name);
if (!item)
BOOST_THROW_EXCEPTION(ConfigError("Import references unknown template: '" + name + "'"));
2014-11-09 19:48:28 +01:00
item->GetExpression()->Evaluate(context, dhint);
return Empty;
}
2014-11-09 19:48:28 +01:00
Value Expression::FunctionWrapper(const std::vector<Value>& arguments,
const std::vector<String>& funcargs, const boost::shared_ptr<Expression>& expr, const Object::Ptr& scope)
{
2014-11-09 19:48:28 +01:00
if (arguments.size() < funcargs.size())
BOOST_THROW_EXCEPTION(ConfigError("Too few arguments for function"));
Dictionary::Ptr context = new Dictionary();
2014-11-06 19:35:47 +01:00
context->Set("__parent", scope);
2014-11-09 19:48:28 +01:00
for (std::vector<Value>::size_type i = 0; i < std::min(arguments.size(), funcargs.size()); i++)
context->Set(funcargs[i], arguments[i]);
2014-11-06 19:35:47 +01:00
expr->Evaluate(context);
return context->Get("__result");
}
2014-11-09 19:48:28 +01:00
Value FunctionExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
{
2014-11-09 19:48:28 +01:00
ScriptFunction::Ptr func = new ScriptFunction(boost::bind(&Expression::FunctionWrapper, _1, m_Args, m_Expression, context));
2014-11-09 19:48:28 +01:00
if (!m_Name.IsEmpty())
ScriptFunction::Register(m_Name, func);
return func;
}
2014-11-09 19:48:28 +01:00
Value ApplyExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
{
2014-11-09 19:48:28 +01:00
String name = m_Name->Evaluate(context, dhint);
2014-11-09 19:48:28 +01:00
ApplyRule::AddRule(m_Type, m_Target, name, m_Expression, m_Filter, m_FKVar, m_FVVar, m_FTerm, m_DebugInfo, context);
return Empty;
}
2014-11-09 19:48:28 +01:00
Value ObjectExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
{
2014-11-09 19:48:28 +01:00
String name;
2014-11-09 19:48:28 +01:00
if (m_Name)
name = m_Name->Evaluate(context, dhint);
2014-11-09 19:48:28 +01:00
ConfigItemBuilder::Ptr item = new ConfigItemBuilder(m_DebugInfo);
String checkName = name;
2014-11-09 19:48:28 +01:00
if (!m_Abstract) {
Type::Ptr ptype = Type::GetByName(m_Type);
NameComposer *nc = dynamic_cast<NameComposer *>(ptype.get());
if (nc)
checkName = nc->MakeName(name, Dictionary::Ptr());
}
if (!checkName.IsEmpty()) {
2014-11-09 19:48:28 +01:00
ConfigItem::Ptr oldItem = ConfigItem::GetObject(m_Type, checkName);
if (oldItem) {
std::ostringstream msgbuf;
2014-11-09 19:48:28 +01:00
msgbuf << "Object '" << name << "' of type '" << m_Type << "' re-defined: " << m_DebugInfo << "; previous definition: " << oldItem->GetDebugInfo();
BOOST_THROW_EXCEPTION(ConfigError(msgbuf.str()) << errinfo_debuginfo(m_DebugInfo));
}
}
2014-11-09 19:48:28 +01:00
item->SetType(m_Type);
if (name.FindFirstOf("!") != String::NPos) {
std::ostringstream msgbuf;
2014-11-09 19:48:28 +01:00
msgbuf << "Name for object '" << name << "' of type '" << m_Type << "' is invalid: Object names may not contain '!'";
BOOST_THROW_EXCEPTION(ConfigError(msgbuf.str()) << errinfo_debuginfo(m_DebugInfo));
}
item->SetName(name);
2014-11-09 19:48:28 +01:00
item->AddExpression(new OwnedExpression(m_Expression));
item->SetAbstract(m_Abstract);
2014-11-06 19:35:47 +01:00
item->SetScope(context);
2014-11-09 19:48:28 +01:00
item->SetZone(m_Zone);
item->Compile()->Register();
if (m_Filter)
2014-11-09 19:48:28 +01:00
ObjectRule::AddRule(m_Type, name, m_Filter, m_DebugInfo, context);
return Empty;
}
2014-05-10 11:26:56 +02:00
2014-11-09 19:48:28 +01:00
Value ForExpression::DoEvaluate(const Object::Ptr& context, DebugHint *dhint) const
2014-05-10 11:26:56 +02:00
{
2014-11-09 19:48:28 +01:00
Value value = m_Value->Evaluate(context, dhint);
2014-05-10 11:26:56 +02:00
if (value.IsObjectType<Array>()) {
2014-11-09 19:48:28 +01:00
if (!m_FVVar.IsEmpty())
BOOST_THROW_EXCEPTION(ConfigError("Cannot use dictionary iterator for array.") << errinfo_debuginfo(m_DebugInfo));
2014-05-10 11:26:56 +02:00
Array::Ptr arr = value;
ObjectLock olock(arr);
BOOST_FOREACH(const Value& value, arr) {
Dictionary::Ptr xcontext = new Dictionary();
2014-11-06 19:35:47 +01:00
xcontext->Set("__parent", context);
2014-11-09 19:48:28 +01:00
xcontext->Set(m_FKVar, value);
2014-11-09 19:48:28 +01:00
m_Expression->Evaluate(xcontext, dhint);
}
} else if (value.IsObjectType<Dictionary>()) {
2014-11-09 19:48:28 +01:00
if (m_FVVar.IsEmpty())
BOOST_THROW_EXCEPTION(ConfigError("Cannot use array iterator for dictionary.") << errinfo_debuginfo(m_DebugInfo));
Dictionary::Ptr dict = value;
ObjectLock olock(dict);
BOOST_FOREACH(const Dictionary::Pair& kv, dict) {
Dictionary::Ptr xcontext = new Dictionary();
2014-11-06 19:35:47 +01:00
xcontext->Set("__parent", context);
2014-11-09 19:48:28 +01:00
xcontext->Set(m_FKVar, kv.first);
xcontext->Set(m_FVVar, kv.second);
2014-11-09 19:48:28 +01:00
m_Expression->Evaluate(xcontext, dhint);
}
} else
2014-11-09 19:48:28 +01:00
BOOST_THROW_EXCEPTION(ConfigError("Invalid type in __for expression: " + value.GetTypeName()) << errinfo_debuginfo(m_DebugInfo));
2014-05-10 11:26:56 +02:00
return Empty;
}
Dictionary::Ptr DebugHint::ToDictionary(void) const
{
Dictionary::Ptr result = new Dictionary();
Array::Ptr messages = new Array();
typedef std::pair<String, DebugInfo> MessageType;
BOOST_FOREACH(const MessageType& message, Messages) {
Array::Ptr amsg = new Array();
amsg->Add(message.first);
amsg->Add(message.second.Path);
amsg->Add(message.second.FirstLine);
amsg->Add(message.second.FirstColumn);
amsg->Add(message.second.LastLine);
amsg->Add(message.second.LastColumn);
messages->Add(amsg);
}
result->Set("messages", messages);
Dictionary::Ptr properties = new Dictionary();
typedef std::map<String, DebugHint>::value_type ChildType;
BOOST_FOREACH(const ChildType& kv, Children) {
properties->Set(kv.first, kv.second.ToDictionary());
}
result->Set("properties", properties);
return result;
}
2014-11-06 19:35:47 +01:00
bool Expression::HasField(const Object::Ptr& context, const String& field)
{
Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(context);
if (dict)
return dict->Contains(field);
else {
Type::Ptr type = context->GetReflectionType();
if (!type)
return false;
return type->GetFieldId(field) != -1;
}
}
Value Expression::GetField(const Object::Ptr& context, const String& field)
{
Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(context);
if (dict)
return dict->Get(field);
else {
Type::Ptr type = context->GetReflectionType();
if (!type)
return Empty;
int fid = type->GetFieldId(field);
if (fid == -1)
return Empty;
return context->GetField(fid);
}
}
void Expression::SetField(const Object::Ptr& context, const String& field, const Value& value)
{
Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(context);
if (dict)
dict->Set(field, value);
else {
Type::Ptr type = context->GetReflectionType();
if (!type)
BOOST_THROW_EXCEPTION(ConfigError("Cannot set field on object."));
int fid = type->GetFieldId(field);
if (fid == -1)
BOOST_THROW_EXCEPTION(ConfigError("Attribute '" + field + "' does not exist."));
try {
context->SetField(fid, value);
} catch (const boost::bad_lexical_cast&) {
BOOST_THROW_EXCEPTION(ConfigError("Attribute '" + field + "' cannot be set to value of type '" + value.GetTypeName() + "'"));
} catch (const std::bad_cast&) {
BOOST_THROW_EXCEPTION(ConfigError("Attribute '" + field + "' cannot be set to value of type '" + value.GetTypeName() + "'"));
}
}
}