2013-03-10 09:08:59 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2018-01-02 12:06:00 +01:00
|
|
|
* Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/) *
|
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. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
#ifndef ARRAY_H
|
|
|
|
#define ARRAY_H
|
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/i2-base.hpp"
|
2015-07-21 16:09:19 +02:00
|
|
|
#include "base/objectlock.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/value.hpp"
|
2014-08-25 15:12:39 +02:00
|
|
|
#include <boost/range/iterator.hpp>
|
2013-03-16 21:18:53 +01:00
|
|
|
#include <vector>
|
2015-10-19 17:31:18 +02:00
|
|
|
#include <set>
|
2013-03-16 21:18:53 +01:00
|
|
|
|
2013-03-10 09:08:59 +01:00
|
|
|
namespace icinga
|
|
|
|
{
|
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
typedef std::vector<Value> ArrayData;
|
|
|
|
|
2013-03-10 09:08:59 +01:00
|
|
|
/**
|
|
|
|
* An array of Value items.
|
|
|
|
*
|
|
|
|
* @ingroup base
|
|
|
|
*/
|
2018-01-04 06:11:04 +01:00
|
|
|
class Array final : public Object
|
2013-03-10 09:08:59 +01:00
|
|
|
{
|
|
|
|
public:
|
2014-11-03 00:44:04 +01:00
|
|
|
DECLARE_OBJECT(Array);
|
2013-03-10 09:08:59 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* An iterator that can be used to iterate over array elements.
|
|
|
|
*/
|
2013-03-16 21:18:53 +01:00
|
|
|
typedef std::vector<Value>::iterator Iterator;
|
2013-03-10 09:08:59 +01:00
|
|
|
|
2014-05-11 06:00:34 +02:00
|
|
|
typedef std::vector<Value>::size_type SizeType;
|
|
|
|
|
2018-01-04 09:43:49 +01:00
|
|
|
Array() = default;
|
2018-01-11 11:17:38 +01:00
|
|
|
Array(const ArrayData& other);
|
|
|
|
Array(ArrayData&& other);
|
2018-01-03 06:01:02 +01:00
|
|
|
Array(std::initializer_list<Value> init);
|
2015-03-28 16:13:44 +01:00
|
|
|
|
2017-12-14 08:47:04 +01:00
|
|
|
Value Get(SizeType index) const;
|
|
|
|
void Set(SizeType index, const Value& value);
|
|
|
|
void Set(SizeType index, Value&& value);
|
2018-01-04 08:54:18 +01:00
|
|
|
void Add(Value value);
|
2013-03-10 09:08:59 +01:00
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
Iterator Begin();
|
|
|
|
Iterator End();
|
2013-03-10 09:08:59 +01:00
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
size_t GetLength() const;
|
2014-11-15 08:22:09 +01:00
|
|
|
bool Contains(const Value& value) const;
|
2013-03-10 09:08:59 +01:00
|
|
|
|
2018-01-04 08:54:18 +01:00
|
|
|
void Insert(SizeType index, Value value);
|
2017-12-14 08:47:04 +01:00
|
|
|
void Remove(SizeType index);
|
2013-03-10 09:08:59 +01:00
|
|
|
void Remove(Iterator it);
|
|
|
|
|
2017-12-14 08:47:04 +01:00
|
|
|
void Resize(SizeType newSize);
|
2018-01-04 04:25:35 +01:00
|
|
|
void Clear();
|
2014-03-20 14:25:40 +01:00
|
|
|
|
2017-12-14 08:47:04 +01:00
|
|
|
void Reserve(SizeType newSize);
|
2015-03-04 10:58:22 +01:00
|
|
|
|
2014-03-20 13:02:02 +01:00
|
|
|
void CopyTo(const Array::Ptr& dest) const;
|
2018-01-04 04:25:35 +01:00
|
|
|
Array::Ptr ShallowClone() const;
|
2013-03-10 09:08:59 +01:00
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
static Object::Ptr GetPrototype();
|
2014-12-12 15:19:23 +01:00
|
|
|
|
2015-07-21 16:09:19 +02:00
|
|
|
template<typename T>
|
|
|
|
static Array::Ptr FromVector(const std::vector<T>& v)
|
|
|
|
{
|
|
|
|
Array::Ptr result = new Array();
|
|
|
|
ObjectLock olock(result);
|
|
|
|
std::copy(v.begin(), v.end(), std::back_inserter(result->m_Data));
|
|
|
|
return result;
|
|
|
|
}
|
2015-10-19 17:31:18 +02:00
|
|
|
|
|
|
|
template<typename T>
|
2018-01-04 04:25:35 +01:00
|
|
|
std::set<T> ToSet()
|
2015-10-19 17:31:18 +02:00
|
|
|
{
|
|
|
|
ObjectLock olock(this);
|
|
|
|
return std::set<T>(Begin(), End());
|
|
|
|
}
|
|
|
|
|
2016-07-29 10:47:13 +02:00
|
|
|
template<typename T>
|
|
|
|
static Array::Ptr FromSet(const std::set<T>& v)
|
|
|
|
{
|
|
|
|
Array::Ptr result = new Array();
|
|
|
|
ObjectLock olock(result);
|
|
|
|
std::copy(v.begin(), v.end(), std::back_inserter(result->m_Data));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-01-04 05:12:56 +01:00
|
|
|
Object::Ptr Clone() const override;
|
2015-07-21 16:09:19 +02:00
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
Array::Ptr Reverse() const;
|
2015-09-23 09:06:15 +02:00
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void Sort();
|
2016-08-14 22:24:51 +02:00
|
|
|
|
2018-01-04 05:12:56 +01:00
|
|
|
String ToString() const override;
|
2015-10-26 11:05:24 +01:00
|
|
|
|
2018-05-09 16:55:14 +02:00
|
|
|
Array::Ptr Unique() const;
|
2018-01-30 12:19:34 +01:00
|
|
|
void Freeze();
|
|
|
|
|
2018-01-04 05:12:56 +01:00
|
|
|
Value GetFieldByName(const String& field, bool sandboxed, const DebugInfo& debugInfo) const override;
|
|
|
|
void SetFieldByName(const String& field, const Value& value, const DebugInfo& debugInfo) override;
|
2016-04-18 11:29:43 +02:00
|
|
|
|
2013-03-10 09:08:59 +01:00
|
|
|
private:
|
2013-03-16 21:18:53 +01:00
|
|
|
std::vector<Value> m_Data; /**< The data for the array. */
|
2018-01-30 12:19:34 +01:00
|
|
|
bool m_Frozen{false};
|
2013-03-10 09:08:59 +01:00
|
|
|
};
|
|
|
|
|
2018-01-04 08:54:18 +01:00
|
|
|
Array::Iterator begin(const Array::Ptr& x);
|
|
|
|
Array::Iterator end(const Array::Ptr& x);
|
2013-03-10 09:08:59 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-01-03 06:01:02 +01:00
|
|
|
extern template class std::vector<icinga::Value>;
|
2013-03-10 09:08:59 +01:00
|
|
|
|
|
|
|
#endif /* ARRAY_H */
|