mirror of https://github.com/Icinga/icinga2.git
Implement += operator for arrays.
This commit is contained in:
parent
7005c17cf1
commit
9e1f48049e
|
@ -20,6 +20,7 @@
|
||||||
#include "config/expression.h"
|
#include "config/expression.h"
|
||||||
#include "config/expressionlist.h"
|
#include "config/expressionlist.h"
|
||||||
#include "base/objectlock.h"
|
#include "base/objectlock.h"
|
||||||
|
#include "base/array.h"
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <boost/tuple/tuple.hpp>
|
#include <boost/tuple/tuple.hpp>
|
||||||
#include <boost/smart_ptr/make_shared.hpp>
|
#include <boost/smart_ptr/make_shared.hpp>
|
||||||
|
@ -39,15 +40,20 @@ void Expression::Execute(const Dictionary::Ptr& dictionary) const
|
||||||
|
|
||||||
ExpressionList::Ptr valueExprl;
|
ExpressionList::Ptr valueExprl;
|
||||||
Dictionary::Ptr valueDict;
|
Dictionary::Ptr valueDict;
|
||||||
|
Array::Ptr valueArray;
|
||||||
if (m_Value.IsObjectType<ExpressionList>())
|
if (m_Value.IsObjectType<ExpressionList>())
|
||||||
valueExprl = m_Value;
|
valueExprl = m_Value;
|
||||||
|
|
||||||
if (m_Value.IsObjectType<Dictionary>())
|
if (m_Value.IsObjectType<Dictionary>())
|
||||||
valueDict = m_Value;
|
valueDict = m_Value;
|
||||||
|
|
||||||
|
if (m_Value.IsObjectType<Array>())
|
||||||
|
valueArray = m_Value;
|
||||||
|
|
||||||
newValue = m_Value;
|
newValue = m_Value;
|
||||||
|
|
||||||
Dictionary::Ptr dict;
|
Dictionary::Ptr dict;
|
||||||
|
Array::Ptr array;
|
||||||
|
|
||||||
switch (m_Operator) {
|
switch (m_Operator) {
|
||||||
case OperatorExecute:
|
case OperatorExecute:
|
||||||
|
@ -73,24 +79,20 @@ void Expression::Execute(const Dictionary::Ptr& dictionary) const
|
||||||
if (oldValue.IsObjectType<Dictionary>())
|
if (oldValue.IsObjectType<Dictionary>())
|
||||||
dict = oldValue;
|
dict = oldValue;
|
||||||
|
|
||||||
if (!dict) {
|
if (oldValue.IsObjectType<Array>())
|
||||||
if (!oldValue.IsEmpty()) {
|
array = oldValue;
|
||||||
std::ostringstream message;
|
|
||||||
message << "Wrong argument types for"
|
|
||||||
" += (non-dictionary and"
|
|
||||||
" dictionary) ("
|
|
||||||
<< m_DebugInfo << ")";
|
|
||||||
BOOST_THROW_EXCEPTION(std::invalid_argument(message.str()));
|
|
||||||
}
|
|
||||||
|
|
||||||
dict = boost::make_shared<Dictionary>();
|
|
||||||
}
|
|
||||||
|
|
||||||
newValue = dict;
|
|
||||||
|
|
||||||
if (valueExprl) {
|
if (valueExprl) {
|
||||||
|
if (!dict)
|
||||||
|
dict = boost::make_shared<Dictionary>();
|
||||||
|
|
||||||
valueExprl->Execute(dict);
|
valueExprl->Execute(dict);
|
||||||
|
|
||||||
|
newValue = dict;
|
||||||
} else if (valueDict) {
|
} else if (valueDict) {
|
||||||
|
if (!dict)
|
||||||
|
dict = boost::make_shared<Dictionary>();
|
||||||
|
|
||||||
ObjectLock olock(valueDict);
|
ObjectLock olock(valueDict);
|
||||||
|
|
||||||
String key;
|
String key;
|
||||||
|
@ -98,9 +100,23 @@ void Expression::Execute(const Dictionary::Ptr& dictionary) const
|
||||||
BOOST_FOREACH(boost::tie(key, value), valueDict) {
|
BOOST_FOREACH(boost::tie(key, value), valueDict) {
|
||||||
dict->Set(key, value);
|
dict->Set(key, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
newValue = dict;
|
||||||
|
} else if (valueArray) {
|
||||||
|
if (!array)
|
||||||
|
array = boost::make_shared<Array>();
|
||||||
|
|
||||||
|
|
||||||
|
ObjectLock olock(valueArray);
|
||||||
|
|
||||||
|
BOOST_FOREACH(const Value& value, valueArray) {
|
||||||
|
array->Add(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
newValue = array;
|
||||||
} else {
|
} else {
|
||||||
std::ostringstream message;
|
std::ostringstream message;
|
||||||
message << "+= only works for dictionaries ("
|
message << "+= only works for dictionaries and arrays ("
|
||||||
<< m_DebugInfo << ")";
|
<< m_DebugInfo << ")";
|
||||||
BOOST_THROW_EXCEPTION(std::invalid_argument(message.str()));
|
BOOST_THROW_EXCEPTION(std::invalid_argument(message.str()));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue