From 9e1f48049e98616d7acffe30c41a7dfc8652a4ee Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Mon, 18 Mar 2013 12:55:32 +0100 Subject: [PATCH] Implement += operator for arrays. --- lib/config/expression.cpp | 46 ++++++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/lib/config/expression.cpp b/lib/config/expression.cpp index e5d7af74e..2a86607f3 100644 --- a/lib/config/expression.cpp +++ b/lib/config/expression.cpp @@ -20,6 +20,7 @@ #include "config/expression.h" #include "config/expressionlist.h" #include "base/objectlock.h" +#include "base/array.h" #include #include #include @@ -39,15 +40,20 @@ void Expression::Execute(const Dictionary::Ptr& dictionary) const ExpressionList::Ptr valueExprl; Dictionary::Ptr valueDict; + Array::Ptr valueArray; if (m_Value.IsObjectType()) valueExprl = m_Value; if (m_Value.IsObjectType()) valueDict = m_Value; + if (m_Value.IsObjectType()) + valueArray = m_Value; + newValue = m_Value; Dictionary::Ptr dict; + Array::Ptr array; switch (m_Operator) { case OperatorExecute: @@ -73,24 +79,20 @@ void Expression::Execute(const Dictionary::Ptr& dictionary) const if (oldValue.IsObjectType()) dict = oldValue; - if (!dict) { - if (!oldValue.IsEmpty()) { - 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(); - } - - newValue = dict; + if (oldValue.IsObjectType()) + array = oldValue; if (valueExprl) { + if (!dict) + dict = boost::make_shared(); + valueExprl->Execute(dict); + + newValue = dict; } else if (valueDict) { + if (!dict) + dict = boost::make_shared(); + ObjectLock olock(valueDict); String key; @@ -98,9 +100,23 @@ void Expression::Execute(const Dictionary::Ptr& dictionary) const BOOST_FOREACH(boost::tie(key, value), valueDict) { dict->Set(key, value); } + + newValue = dict; + } else if (valueArray) { + if (!array) + array = boost::make_shared(); + + + ObjectLock olock(valueArray); + + BOOST_FOREACH(const Value& value, valueArray) { + array->Add(value); + } + + newValue = array; } else { std::ostringstream message; - message << "+= only works for dictionaries (" + message << "+= only works for dictionaries and arrays (" << m_DebugInfo << ")"; BOOST_THROW_EXCEPTION(std::invalid_argument(message.str())); } -- 2.40.0