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