2019-02-25 14:48:22 +01:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2013-03-10 09:08:59 +01:00
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/array.hpp"
|
|
|
|
#include "base/objectlock.hpp"
|
|
|
|
#include "base/debug.hpp"
|
2014-10-31 08:49:14 +01:00
|
|
|
#include "base/primitivetype.hpp"
|
2014-12-12 15:19:23 +01:00
|
|
|
#include "base/dictionary.hpp"
|
2015-10-26 11:05:24 +01:00
|
|
|
#include "base/configwriter.hpp"
|
2016-04-18 11:29:43 +02:00
|
|
|
#include "base/convert.hpp"
|
|
|
|
#include "base/exception.hpp"
|
2013-03-10 09:08:59 +01:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2018-01-03 06:01:02 +01:00
|
|
|
template class std::vector<Value>;
|
|
|
|
|
2015-08-17 08:14:04 +02:00
|
|
|
REGISTER_PRIMITIVE_TYPE(Array, Object, Array::GetPrototype());
|
2014-10-31 08:49:14 +01:00
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
Array::Array(const ArrayData& other)
|
|
|
|
: m_Data(other)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
Array::Array(ArrayData&& other)
|
|
|
|
: m_Data(std::move(other))
|
|
|
|
{ }
|
|
|
|
|
2018-01-03 06:01:02 +01:00
|
|
|
Array::Array(std::initializer_list<Value> init)
|
|
|
|
: m_Data(init)
|
|
|
|
{ }
|
|
|
|
|
2013-03-10 09:08:59 +01:00
|
|
|
/**
|
|
|
|
* Restrieves a value from an array.
|
|
|
|
*
|
2015-06-26 15:37:47 +02:00
|
|
|
* @param index The index.
|
2013-03-10 09:08:59 +01:00
|
|
|
* @returns The value.
|
|
|
|
*/
|
2017-12-14 08:47:04 +01:00
|
|
|
Value Array::Get(SizeType index) const
|
2013-03-10 09:08:59 +01:00
|
|
|
{
|
|
|
|
ObjectLock olock(this);
|
|
|
|
|
|
|
|
return m_Data.at(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a value in the array.
|
|
|
|
*
|
|
|
|
* @param index The index.
|
|
|
|
* @param value The value.
|
2018-08-07 13:55:41 +02:00
|
|
|
* @param overrideFrozen Whether to allow modifying frozen arrays.
|
2013-03-10 09:08:59 +01:00
|
|
|
*/
|
2018-08-07 13:55:41 +02:00
|
|
|
void Array::Set(SizeType index, const Value& value, bool overrideFrozen)
|
2013-03-10 09:08:59 +01:00
|
|
|
{
|
|
|
|
ObjectLock olock(this);
|
|
|
|
|
2018-08-07 13:55:41 +02:00
|
|
|
if (m_Frozen && !overrideFrozen)
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Value in array must not be modified."));
|
2018-01-30 12:19:34 +01:00
|
|
|
|
2013-03-10 09:08:59 +01:00
|
|
|
m_Data.at(index) = value;
|
|
|
|
}
|
|
|
|
|
2016-08-27 07:35:04 +02:00
|
|
|
/**
|
|
|
|
* Sets a value in the array.
|
|
|
|
*
|
|
|
|
* @param index The index.
|
|
|
|
* @param value The value.
|
2018-08-07 13:55:41 +02:00
|
|
|
* @param overrideFrozen Whether to allow modifying frozen arrays.
|
2016-08-27 07:35:04 +02:00
|
|
|
*/
|
2018-08-07 13:55:41 +02:00
|
|
|
void Array::Set(SizeType index, Value&& value, bool overrideFrozen)
|
2016-08-27 07:35:04 +02:00
|
|
|
{
|
|
|
|
ObjectLock olock(this);
|
|
|
|
|
2018-08-07 13:55:41 +02:00
|
|
|
if (m_Frozen && !overrideFrozen)
|
2018-01-30 12:19:34 +01:00
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Array must not be modified."));
|
|
|
|
|
2016-08-27 07:35:04 +02:00
|
|
|
m_Data.at(index).Swap(value);
|
|
|
|
}
|
|
|
|
|
2013-03-10 09:08:59 +01:00
|
|
|
/**
|
|
|
|
* Adds a value to the array.
|
|
|
|
*
|
|
|
|
* @param value The value.
|
2018-08-07 13:55:41 +02:00
|
|
|
* @param overrideFrozen Whether to allow modifying frozen arrays.
|
2013-03-10 09:08:59 +01:00
|
|
|
*/
|
2018-08-07 13:55:41 +02:00
|
|
|
void Array::Add(Value value, bool overrideFrozen)
|
2013-03-10 09:08:59 +01:00
|
|
|
{
|
|
|
|
ObjectLock olock(this);
|
|
|
|
|
2018-08-07 13:55:41 +02:00
|
|
|
if (m_Frozen && !overrideFrozen)
|
2018-01-30 12:19:34 +01:00
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Array must not be modified."));
|
|
|
|
|
2018-01-04 08:54:18 +01:00
|
|
|
m_Data.push_back(std::move(value));
|
2016-08-27 07:35:04 +02:00
|
|
|
}
|
|
|
|
|
2018-01-03 06:01:02 +01:00
|
|
|
/**
|
|
|
|
* Returns an iterator to the beginning of the array.
|
|
|
|
*
|
|
|
|
* Note: Caller must hold the object lock while using the iterator.
|
|
|
|
*
|
|
|
|
* @returns An iterator.
|
|
|
|
*/
|
2018-01-04 04:25:35 +01:00
|
|
|
Array::Iterator Array::Begin()
|
2018-01-03 06:01:02 +01:00
|
|
|
{
|
|
|
|
ASSERT(OwnsLock());
|
|
|
|
|
|
|
|
return m_Data.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an iterator to the end of the array.
|
|
|
|
*
|
|
|
|
* Note: Caller must hold the object lock while using the iterator.
|
|
|
|
*
|
|
|
|
* @returns An iterator.
|
|
|
|
*/
|
2018-01-04 04:25:35 +01:00
|
|
|
Array::Iterator Array::End()
|
2018-01-03 06:01:02 +01:00
|
|
|
{
|
|
|
|
ASSERT(OwnsLock());
|
|
|
|
|
|
|
|
return m_Data.end();
|
|
|
|
}
|
|
|
|
|
2013-03-10 09:08:59 +01:00
|
|
|
/**
|
|
|
|
* Returns the number of elements in the array.
|
|
|
|
*
|
|
|
|
* @returns Number of elements.
|
|
|
|
*/
|
2018-01-04 04:25:35 +01:00
|
|
|
size_t Array::GetLength() const
|
2013-03-10 09:08:59 +01:00
|
|
|
{
|
|
|
|
ObjectLock olock(this);
|
|
|
|
|
|
|
|
return m_Data.size();
|
|
|
|
}
|
|
|
|
|
2014-05-02 00:38:46 +02:00
|
|
|
/**
|
|
|
|
* Checks whether the array contains the specified value.
|
|
|
|
*
|
|
|
|
* @param value The value.
|
|
|
|
* @returns true if the array contains the value, false otherwise.
|
|
|
|
*/
|
2014-11-15 08:22:09 +01:00
|
|
|
bool Array::Contains(const Value& value) const
|
2014-05-02 00:38:46 +02:00
|
|
|
{
|
|
|
|
ObjectLock olock(this);
|
|
|
|
|
|
|
|
return (std::find(m_Data.begin(), m_Data.end(), value) != m_Data.end());
|
|
|
|
}
|
|
|
|
|
2013-12-12 13:53:27 +01:00
|
|
|
/**
|
|
|
|
* Insert the given value at the specified index
|
|
|
|
*
|
|
|
|
* @param index The index
|
|
|
|
* @param value The value to add
|
2018-08-07 13:55:41 +02:00
|
|
|
* @param overrideFrozen Whether to allow modifying frozen arrays.
|
2013-12-12 13:53:27 +01:00
|
|
|
*/
|
2018-08-07 13:55:41 +02:00
|
|
|
void Array::Insert(SizeType index, Value value, bool overrideFrozen)
|
2013-12-12 13:53:27 +01:00
|
|
|
{
|
|
|
|
ObjectLock olock(this);
|
|
|
|
|
|
|
|
ASSERT(index <= m_Data.size());
|
|
|
|
|
2018-08-07 13:55:41 +02:00
|
|
|
if (m_Frozen && !overrideFrozen)
|
2018-01-30 12:19:34 +01:00
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Array must not be modified."));
|
|
|
|
|
2018-01-04 08:54:18 +01:00
|
|
|
m_Data.insert(m_Data.begin() + index, std::move(value));
|
2013-12-12 13:53:27 +01:00
|
|
|
}
|
|
|
|
|
2013-03-10 09:08:59 +01:00
|
|
|
/**
|
|
|
|
* Removes the specified index from the array.
|
|
|
|
*
|
|
|
|
* @param index The index.
|
2018-08-07 13:55:41 +02:00
|
|
|
* @param overrideFrozen Whether to allow modifying frozen arrays.
|
2013-03-10 09:08:59 +01:00
|
|
|
*/
|
2018-08-07 13:55:41 +02:00
|
|
|
void Array::Remove(SizeType index, bool overrideFrozen)
|
2013-03-10 09:08:59 +01:00
|
|
|
{
|
|
|
|
ObjectLock olock(this);
|
|
|
|
|
2018-08-07 13:55:41 +02:00
|
|
|
if (m_Frozen && !overrideFrozen)
|
2018-01-30 12:19:34 +01:00
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Array must not be modified."));
|
|
|
|
|
2019-06-28 16:12:58 +02:00
|
|
|
if (index >= m_Data.size())
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Index to remove must be within bounds."));
|
|
|
|
|
2013-03-10 09:08:59 +01:00
|
|
|
m_Data.erase(m_Data.begin() + index);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes the item specified by the iterator from the array.
|
|
|
|
*
|
|
|
|
* @param it The iterator.
|
2018-08-07 13:55:41 +02:00
|
|
|
* @param overrideFrozen Whether to allow modifying frozen arrays.
|
2013-03-10 09:08:59 +01:00
|
|
|
*/
|
2018-08-07 13:55:41 +02:00
|
|
|
void Array::Remove(Array::Iterator it, bool overrideFrozen)
|
2013-03-10 09:08:59 +01:00
|
|
|
{
|
2013-04-19 11:27:18 +02:00
|
|
|
ASSERT(OwnsLock());
|
|
|
|
|
2018-08-07 13:55:41 +02:00
|
|
|
if (m_Frozen && !overrideFrozen)
|
2018-01-30 12:19:34 +01:00
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Array must not be modified."));
|
|
|
|
|
2013-03-10 09:08:59 +01:00
|
|
|
m_Data.erase(it);
|
|
|
|
}
|
|
|
|
|
2018-08-07 13:55:41 +02:00
|
|
|
void Array::Resize(SizeType newSize, bool overrideFrozen)
|
2014-03-20 14:25:40 +01:00
|
|
|
{
|
|
|
|
ObjectLock olock(this);
|
|
|
|
|
2018-08-07 13:55:41 +02:00
|
|
|
if (m_Frozen && !overrideFrozen)
|
2018-01-30 12:19:34 +01:00
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Array must not be modified."));
|
|
|
|
|
2017-12-14 08:47:04 +01:00
|
|
|
m_Data.resize(newSize);
|
2014-03-20 14:25:40 +01:00
|
|
|
}
|
|
|
|
|
2018-08-07 13:55:41 +02:00
|
|
|
void Array::Clear(bool overrideFrozen)
|
2014-03-24 11:23:05 +01:00
|
|
|
{
|
|
|
|
ObjectLock olock(this);
|
|
|
|
|
2018-08-07 13:55:41 +02:00
|
|
|
if (m_Frozen && !overrideFrozen)
|
2018-01-30 12:19:34 +01:00
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Array must not be modified."));
|
|
|
|
|
2014-03-24 11:23:05 +01:00
|
|
|
m_Data.clear();
|
|
|
|
}
|
|
|
|
|
2018-08-07 13:55:41 +02:00
|
|
|
void Array::Reserve(SizeType newSize, bool overrideFrozen)
|
2015-03-04 10:58:22 +01:00
|
|
|
{
|
|
|
|
ObjectLock olock(this);
|
|
|
|
|
2018-08-07 13:55:41 +02:00
|
|
|
if (m_Frozen && !overrideFrozen)
|
2018-01-30 12:19:34 +01:00
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Array must not be modified."));
|
|
|
|
|
2017-12-14 08:47:04 +01:00
|
|
|
m_Data.reserve(newSize);
|
2015-03-04 10:58:22 +01:00
|
|
|
}
|
|
|
|
|
2014-03-20 13:02:02 +01:00
|
|
|
void Array::CopyTo(const Array::Ptr& dest) const
|
|
|
|
{
|
|
|
|
ObjectLock olock(this);
|
|
|
|
ObjectLock xlock(dest);
|
|
|
|
|
2018-01-30 12:19:34 +01:00
|
|
|
if (dest->m_Frozen)
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Array must not be modified."));
|
|
|
|
|
2014-03-20 13:02:02 +01:00
|
|
|
std::copy(m_Data.begin(), m_Data.end(), std::back_inserter(dest->m_Data));
|
|
|
|
}
|
|
|
|
|
2013-03-10 09:08:59 +01:00
|
|
|
/**
|
|
|
|
* Makes a shallow copy of an array.
|
|
|
|
*
|
|
|
|
* @returns a copy of the array.
|
|
|
|
*/
|
2018-01-04 04:25:35 +01:00
|
|
|
Array::Ptr Array::ShallowClone() const
|
2013-03-10 09:08:59 +01:00
|
|
|
{
|
2014-11-08 21:17:16 +01:00
|
|
|
Array::Ptr clone = new Array();
|
2014-03-20 13:02:02 +01:00
|
|
|
CopyTo(clone);
|
2013-03-10 09:08:59 +01:00
|
|
|
return clone;
|
|
|
|
}
|
2014-11-04 15:19:33 +01:00
|
|
|
|
2015-08-17 13:59:49 +02:00
|
|
|
/**
|
|
|
|
* Makes a deep clone of an array
|
|
|
|
* and its elements.
|
2017-12-19 15:50:05 +01:00
|
|
|
*
|
2015-08-17 13:59:49 +02:00
|
|
|
* @returns a copy of the array.
|
|
|
|
*/
|
2018-01-04 04:25:35 +01:00
|
|
|
Object::Ptr Array::Clone() const
|
2015-08-17 13:59:49 +02:00
|
|
|
{
|
2018-01-11 11:17:38 +01:00
|
|
|
ArrayData arr;
|
2017-12-13 12:54:14 +01:00
|
|
|
|
2015-08-17 13:59:49 +02:00
|
|
|
ObjectLock olock(this);
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Value& val : m_Data) {
|
2018-01-11 11:17:38 +01:00
|
|
|
arr.push_back(val.Clone());
|
2015-08-17 13:59:49 +02:00
|
|
|
}
|
2017-12-13 12:54:14 +01:00
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
return new Array(std::move(arr));
|
2015-08-17 13:59:49 +02:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
Array::Ptr Array::Reverse() const
|
2015-09-23 09:06:15 +02:00
|
|
|
{
|
|
|
|
Array::Ptr result = new Array();
|
|
|
|
|
|
|
|
ObjectLock olock(this);
|
|
|
|
ObjectLock xlock(result);
|
|
|
|
|
|
|
|
std::copy(m_Data.rbegin(), m_Data.rend(), std::back_inserter(result->m_Data));
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2015-10-26 11:05:24 +01:00
|
|
|
|
2018-08-07 13:55:41 +02:00
|
|
|
void Array::Sort(bool overrideFrozen)
|
2016-08-14 22:24:51 +02:00
|
|
|
{
|
|
|
|
ObjectLock olock(this);
|
2018-01-30 12:19:34 +01:00
|
|
|
|
2018-08-07 13:55:41 +02:00
|
|
|
if (m_Frozen && !overrideFrozen)
|
2018-01-30 12:19:34 +01:00
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Array must not be modified."));
|
|
|
|
|
2016-08-14 22:24:51 +02:00
|
|
|
std::sort(m_Data.begin(), m_Data.end());
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
String Array::ToString() const
|
2015-10-26 11:05:24 +01:00
|
|
|
{
|
|
|
|
std::ostringstream msgbuf;
|
2015-11-04 16:42:19 +01:00
|
|
|
ConfigWriter::EmitArray(msgbuf, 1, const_cast<Array *>(this));
|
2015-10-26 11:05:24 +01:00
|
|
|
return msgbuf.str();
|
|
|
|
}
|
2016-04-18 11:29:43 +02:00
|
|
|
|
2019-07-12 14:36:55 +02:00
|
|
|
Value Array::Join(const Value& separator) const
|
|
|
|
{
|
|
|
|
Value result;
|
|
|
|
bool first = true;
|
|
|
|
|
|
|
|
ObjectLock olock(this);
|
|
|
|
|
|
|
|
for (const Value& item : m_Data) {
|
|
|
|
if (first) {
|
|
|
|
first = false;
|
|
|
|
} else {
|
|
|
|
result = result + separator;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = result + item;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-05-09 16:55:14 +02:00
|
|
|
Array::Ptr Array::Unique() const
|
|
|
|
{
|
|
|
|
std::set<Value> result;
|
|
|
|
|
|
|
|
ObjectLock olock(this);
|
|
|
|
|
|
|
|
for (const Value& item : m_Data) {
|
|
|
|
result.insert(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Array::FromSet(result);
|
|
|
|
}
|
|
|
|
|
2018-01-30 12:19:34 +01:00
|
|
|
void Array::Freeze()
|
|
|
|
{
|
|
|
|
ObjectLock olock(this);
|
|
|
|
m_Frozen = true;
|
|
|
|
}
|
|
|
|
|
2016-04-18 11:29:43 +02:00
|
|
|
Value Array::GetFieldByName(const String& field, bool sandboxed, const DebugInfo& debugInfo) const
|
|
|
|
{
|
|
|
|
int index;
|
|
|
|
|
|
|
|
try {
|
|
|
|
index = Convert::ToLong(field);
|
|
|
|
} catch (...) {
|
|
|
|
return Object::GetFieldByName(field, sandboxed, debugInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjectLock olock(this);
|
|
|
|
|
2016-08-24 19:59:13 +02:00
|
|
|
if (index < 0 || static_cast<size_t>(index) >= GetLength())
|
2016-04-18 11:29:43 +02:00
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("Array index '" + Convert::ToString(index) + "' is out of bounds.", debugInfo));
|
|
|
|
|
|
|
|
return Get(index);
|
|
|
|
}
|
|
|
|
|
2018-08-07 13:55:41 +02:00
|
|
|
void Array::SetFieldByName(const String& field, const Value& value, bool overrideFrozen, const DebugInfo& debugInfo)
|
2016-04-18 11:29:43 +02:00
|
|
|
{
|
|
|
|
ObjectLock olock(this);
|
|
|
|
|
|
|
|
int index = Convert::ToLong(field);
|
2016-08-24 19:59:13 +02:00
|
|
|
|
|
|
|
if (index < 0)
|
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("Array index '" + Convert::ToString(index) + "' is out of bounds.", debugInfo));
|
|
|
|
|
|
|
|
if (static_cast<size_t>(index) >= GetLength())
|
2018-08-07 13:55:41 +02:00
|
|
|
Resize(index + 1, overrideFrozen);
|
2016-08-24 19:59:13 +02:00
|
|
|
|
2018-08-07 13:55:41 +02:00
|
|
|
Set(index, value, overrideFrozen);
|
2016-04-18 11:29:43 +02:00
|
|
|
}
|
2018-01-03 06:01:02 +01:00
|
|
|
|
2018-01-04 08:54:18 +01:00
|
|
|
Array::Iterator icinga::begin(const Array::Ptr& x)
|
2018-01-03 06:01:02 +01:00
|
|
|
{
|
|
|
|
return x->Begin();
|
|
|
|
}
|
|
|
|
|
2018-01-04 08:54:18 +01:00
|
|
|
Array::Iterator icinga::end(const Array::Ptr& x)
|
2018-01-03 06:01:02 +01:00
|
|
|
{
|
|
|
|
return x->End();
|
|
|
|
}
|