2013-03-10 09:08:59 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2016-01-12 08:29:59 +01:00
|
|
|
* Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/) *
|
2013-03-10 09:08:59 +01:00
|
|
|
* *
|
|
|
|
* 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-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"
|
2013-03-16 21:18:53 +01:00
|
|
|
#include <boost/foreach.hpp>
|
2013-03-10 09:08:59 +01:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2015-08-17 08:14:04 +02:00
|
|
|
REGISTER_PRIMITIVE_TYPE(Array, Object, Array::GetPrototype());
|
2014-10-31 08:49:14 +01:00
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
Value Array::Get(unsigned int index) const
|
|
|
|
{
|
|
|
|
ObjectLock olock(this);
|
|
|
|
|
|
|
|
return m_Data.at(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a value in the array.
|
|
|
|
*
|
|
|
|
* @param index The index.
|
|
|
|
* @param value The value.
|
|
|
|
*/
|
|
|
|
void Array::Set(unsigned int index, const Value& value)
|
|
|
|
{
|
|
|
|
ObjectLock olock(this);
|
|
|
|
|
|
|
|
m_Data.at(index) = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a value to the array.
|
|
|
|
*
|
|
|
|
* @param value The value.
|
|
|
|
*/
|
|
|
|
void Array::Add(const Value& value)
|
|
|
|
{
|
|
|
|
ObjectLock olock(this);
|
|
|
|
|
|
|
|
m_Data.push_back(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the number of elements in the array.
|
|
|
|
*
|
|
|
|
* @returns Number of elements.
|
|
|
|
*/
|
|
|
|
size_t Array::GetLength(void) const
|
|
|
|
{
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
void Array::Insert(unsigned int index, const Value& value)
|
|
|
|
{
|
|
|
|
ObjectLock olock(this);
|
|
|
|
|
|
|
|
ASSERT(index <= m_Data.size());
|
|
|
|
|
|
|
|
m_Data.insert(m_Data.begin() + index, value);
|
|
|
|
}
|
|
|
|
|
2013-03-10 09:08:59 +01:00
|
|
|
/**
|
|
|
|
* Removes the specified index from the array.
|
|
|
|
*
|
|
|
|
* @param index The index.
|
|
|
|
*/
|
|
|
|
void Array::Remove(unsigned int index)
|
|
|
|
{
|
|
|
|
ObjectLock olock(this);
|
|
|
|
|
|
|
|
m_Data.erase(m_Data.begin() + index);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes the item specified by the iterator from the array.
|
|
|
|
*
|
|
|
|
* @param it The iterator.
|
|
|
|
*/
|
|
|
|
void Array::Remove(Array::Iterator it)
|
|
|
|
{
|
2013-04-19 11:27:18 +02:00
|
|
|
ASSERT(OwnsLock());
|
|
|
|
|
2013-03-10 09:08:59 +01:00
|
|
|
m_Data.erase(it);
|
|
|
|
}
|
|
|
|
|
2014-03-20 14:25:40 +01:00
|
|
|
void Array::Resize(size_t new_size)
|
|
|
|
{
|
|
|
|
ObjectLock olock(this);
|
|
|
|
|
|
|
|
m_Data.resize(new_size);
|
|
|
|
}
|
|
|
|
|
2014-03-24 11:23:05 +01:00
|
|
|
void Array::Clear(void)
|
|
|
|
{
|
|
|
|
ObjectLock olock(this);
|
|
|
|
|
|
|
|
m_Data.clear();
|
|
|
|
}
|
|
|
|
|
2015-03-04 10:58:22 +01:00
|
|
|
void Array::Reserve(size_t new_size)
|
|
|
|
{
|
|
|
|
ObjectLock olock(this);
|
|
|
|
|
|
|
|
m_Data.reserve(new_size);
|
|
|
|
}
|
|
|
|
|
2014-03-20 13:02:02 +01:00
|
|
|
void Array::CopyTo(const Array::Ptr& dest) const
|
|
|
|
{
|
|
|
|
ObjectLock olock(this);
|
|
|
|
ObjectLock xlock(dest);
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
Array::Ptr Array::ShallowClone(void) const
|
|
|
|
{
|
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.
|
|
|
|
*
|
|
|
|
* @returns a copy of the array.
|
|
|
|
*/
|
|
|
|
Object::Ptr Array::Clone(void) const
|
|
|
|
{
|
|
|
|
Array::Ptr arr = new Array();
|
|
|
|
|
|
|
|
ObjectLock olock(this);
|
|
|
|
BOOST_FOREACH(const Value& val, m_Data) {
|
|
|
|
arr->Add(val.Clone());
|
|
|
|
}
|
|
|
|
|
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
2015-09-23 09:06:15 +02:00
|
|
|
Array::Ptr Array::Reverse(void) const
|
|
|
|
{
|
|
|
|
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
|
|
|
|
|
|
|
String Array::ToString(void) const
|
|
|
|
{
|
|
|
|
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();
|
|
|
|
}
|