2012-05-10 12:06:41 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2018-01-02 12:06:00 +01:00
|
|
|
* Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/) *
|
2012-05-10 12:06:41 +02: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 *
|
2012-05-11 13:33:57 +02:00
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
2012-05-10 12:06:41 +02:00
|
|
|
******************************************************************************/
|
|
|
|
|
2012-04-02 08:56:30 +02:00
|
|
|
#ifndef OBJECT_H
|
|
|
|
#define OBJECT_H
|
2012-03-28 13:24:49 +02:00
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/i2-base.hpp"
|
|
|
|
#include "base/debug.hpp"
|
2014-11-08 21:17:16 +01:00
|
|
|
#include <boost/smart_ptr/intrusive_ptr.hpp>
|
2016-03-29 12:45:22 +02:00
|
|
|
#include <vector>
|
2014-11-08 21:17:16 +01:00
|
|
|
|
|
|
|
using boost::intrusive_ptr;
|
2013-03-18 19:02:42 +01:00
|
|
|
using boost::dynamic_pointer_cast;
|
|
|
|
using boost::static_pointer_cast;
|
2014-11-06 14:21:22 +01:00
|
|
|
|
2012-03-28 13:24:49 +02:00
|
|
|
namespace icinga
|
|
|
|
{
|
|
|
|
|
2013-06-13 11:33:00 +02:00
|
|
|
class Value;
|
2014-11-07 12:32:25 +01:00
|
|
|
class Object;
|
|
|
|
class Type;
|
2014-12-08 09:12:40 +01:00
|
|
|
class String;
|
2016-04-19 14:47:49 +02:00
|
|
|
struct DebugInfo;
|
2015-08-13 08:52:00 +02:00
|
|
|
class ValidationUtils;
|
2012-06-16 13:06:06 +02:00
|
|
|
|
2017-12-31 07:22:16 +01:00
|
|
|
extern Value Empty;
|
2015-08-04 14:47:44 +02:00
|
|
|
|
2013-07-09 08:42:08 +02:00
|
|
|
#define DECLARE_PTR_TYPEDEFS(klass) \
|
2014-11-08 21:17:16 +01:00
|
|
|
typedef intrusive_ptr<klass> Ptr
|
|
|
|
|
2015-08-18 07:46:04 +02:00
|
|
|
#define IMPL_TYPE_LOOKUP_SUPER() \
|
|
|
|
|
|
|
|
#define IMPL_TYPE_LOOKUP() \
|
|
|
|
static intrusive_ptr<Type> TypeInstance; \
|
2018-01-04 04:25:35 +01:00
|
|
|
virtual intrusive_ptr<Type> GetReflectionType() const override \
|
2015-08-18 07:46:04 +02:00
|
|
|
{ \
|
|
|
|
return TypeInstance; \
|
2014-11-03 00:44:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#define DECLARE_OBJECT(klass) \
|
|
|
|
DECLARE_PTR_TYPEDEFS(klass); \
|
2015-03-05 07:42:13 +01:00
|
|
|
IMPL_TYPE_LOOKUP();
|
2014-11-03 00:44:04 +01:00
|
|
|
|
2018-02-21 13:42:58 +01:00
|
|
|
#define REQUIRE_NOT_NULL(ptr) RequireNotNullInternal(ptr, #ptr)
|
2018-01-30 11:26:07 +01:00
|
|
|
|
|
|
|
void RequireNotNullInternal(const intrusive_ptr<Object>& object, const char *description);
|
|
|
|
|
2018-01-04 18:24:45 +01:00
|
|
|
void DefaultObjectFactoryCheckArgs(const std::vector<Value>& args);
|
|
|
|
|
2014-11-07 12:32:25 +01:00
|
|
|
template<typename T>
|
2016-03-29 12:45:22 +02:00
|
|
|
intrusive_ptr<Object> DefaultObjectFactory(const std::vector<Value>& args)
|
2014-11-07 12:32:25 +01:00
|
|
|
{
|
2018-01-04 18:24:45 +01:00
|
|
|
DefaultObjectFactoryCheckArgs(args);
|
2016-03-29 12:45:22 +02:00
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
return new T();
|
2014-11-07 12:32:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2016-03-29 12:45:22 +02:00
|
|
|
intrusive_ptr<Object> DefaultObjectFactoryVA(const std::vector<Value>& args)
|
|
|
|
{
|
|
|
|
return new T(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef intrusive_ptr<Object> (*ObjectFactory)(const std::vector<Value>&);
|
|
|
|
|
|
|
|
template<typename T, bool VA>
|
2014-11-07 12:32:25 +01:00
|
|
|
struct TypeHelper
|
2016-03-29 12:45:22 +02:00
|
|
|
{
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct TypeHelper<T, false>
|
2014-11-07 12:32:25 +01:00
|
|
|
{
|
2018-01-04 04:25:35 +01:00
|
|
|
static ObjectFactory GetFactory()
|
2014-11-07 12:32:25 +01:00
|
|
|
{
|
|
|
|
return DefaultObjectFactory<T>;
|
|
|
|
}
|
|
|
|
};
|
2014-11-03 00:44:04 +01:00
|
|
|
|
2016-03-29 12:45:22 +02:00
|
|
|
template<typename T>
|
|
|
|
struct TypeHelper<T, true>
|
|
|
|
{
|
2018-01-04 04:25:35 +01:00
|
|
|
static ObjectFactory GetFactory()
|
2016-03-29 12:45:22 +02:00
|
|
|
{
|
|
|
|
return DefaultObjectFactoryVA<T>;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-01-11 07:08:09 +01:00
|
|
|
template<typename T>
|
|
|
|
struct Lazy
|
|
|
|
{
|
|
|
|
using Accessor = std::function<T ()>;
|
|
|
|
|
|
|
|
explicit Lazy(T value)
|
|
|
|
: m_Cached(true), m_Value(value)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
explicit Lazy(Accessor accessor)
|
|
|
|
: m_Accessor(accessor)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
explicit Lazy(const Lazy<U>& other)
|
|
|
|
{
|
|
|
|
if (other.m_Cached) {
|
|
|
|
m_Accessor = Accessor();
|
|
|
|
m_Value = static_cast<T>(other.m_Value);
|
|
|
|
m_Cached = true;
|
|
|
|
} else {
|
|
|
|
auto accessor = other.m_Accessor;
|
|
|
|
m_Accessor = [accessor]() { return static_cast<T>(accessor()); };
|
|
|
|
m_Cached = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
operator Lazy<U>() const
|
|
|
|
{
|
|
|
|
if (m_Cached)
|
2018-01-18 14:45:47 +01:00
|
|
|
return Lazy<U>(static_cast<U>(m_Value));
|
2018-01-11 07:08:09 +01:00
|
|
|
else {
|
|
|
|
Accessor accessor = m_Accessor;
|
2018-01-18 14:45:47 +01:00
|
|
|
return Lazy<U>(static_cast<typename Lazy<U>::Accessor>([accessor]() { return static_cast<U>(accessor()); }));
|
2018-01-11 07:08:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const T& operator()() const
|
|
|
|
{
|
|
|
|
if (!m_Cached) {
|
|
|
|
m_Value = m_Accessor();
|
|
|
|
m_Cached = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Accessor m_Accessor;
|
|
|
|
mutable bool m_Cached{false};
|
|
|
|
mutable T m_Value;
|
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
friend struct Lazy;
|
|
|
|
};
|
|
|
|
|
2012-04-22 16:45:31 +02:00
|
|
|
/**
|
|
|
|
* Base class for all heap-allocated objects. At least one of its methods
|
|
|
|
* has to be virtual for RTTI to work.
|
2012-05-18 22:21:28 +02:00
|
|
|
*
|
|
|
|
* @ingroup base
|
2012-04-22 16:45:31 +02:00
|
|
|
*/
|
2017-12-31 07:22:16 +01:00
|
|
|
class Object
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
2012-05-21 23:42:54 +02:00
|
|
|
public:
|
2015-08-18 07:46:04 +02:00
|
|
|
DECLARE_PTR_TYPEDEFS(Object);
|
2012-03-28 13:24:49 +02:00
|
|
|
|
2018-01-04 09:43:49 +01:00
|
|
|
Object() = default;
|
2018-01-04 04:25:35 +01:00
|
|
|
virtual ~Object();
|
2013-03-10 09:08:59 +01:00
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
virtual String ToString() const;
|
2014-12-08 09:12:40 +01:00
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
virtual intrusive_ptr<Type> GetReflectionType() const;
|
2015-08-18 07:46:04 +02:00
|
|
|
|
2015-08-25 13:53:43 +02:00
|
|
|
virtual void Validate(int types, const ValidationUtils& utils);
|
|
|
|
|
2015-08-04 14:47:44 +02:00
|
|
|
virtual void SetField(int id, const Value& value, bool suppress_events = false, const Value& cookie = Empty);
|
2013-11-04 23:14:34 +01:00
|
|
|
virtual Value GetField(int id) const;
|
2016-04-18 11:29:43 +02:00
|
|
|
virtual Value GetFieldByName(const String& field, bool sandboxed, const DebugInfo& debugInfo) const;
|
|
|
|
virtual void SetFieldByName(const String& field, const Value& value, const DebugInfo& debugInfo);
|
|
|
|
virtual bool HasOwnField(const String& field) const;
|
2016-09-01 07:41:41 +02:00
|
|
|
virtual bool GetOwnField(const String& field, Value *result) const;
|
2018-01-11 07:08:09 +01:00
|
|
|
virtual void ValidateField(int id, const Lazy<Value>& lvalue, const ValidationUtils& utils);
|
2015-08-04 14:47:44 +02:00
|
|
|
virtual void NotifyField(int id, const Value& cookie = Empty);
|
2015-09-22 09:42:30 +02:00
|
|
|
virtual Object::Ptr NavigateField(int id) const;
|
2013-11-04 23:14:34 +01:00
|
|
|
|
2014-12-19 12:19:28 +01:00
|
|
|
#ifdef I2_DEBUG
|
2018-01-04 04:25:35 +01:00
|
|
|
bool OwnsLock() const;
|
2014-12-19 12:19:28 +01:00
|
|
|
#endif /* I2_DEBUG */
|
2012-08-03 13:19:55 +02:00
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
static Object::Ptr GetPrototype();
|
2017-12-13 12:54:14 +01:00
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
virtual Object::Ptr Clone() const;
|
2014-12-12 15:19:23 +01:00
|
|
|
|
2015-08-18 07:46:04 +02:00
|
|
|
static intrusive_ptr<Type> TypeInstance;
|
|
|
|
|
2012-06-16 13:06:06 +02:00
|
|
|
private:
|
2018-01-04 09:43:49 +01:00
|
|
|
Object(const Object& other) = delete;
|
|
|
|
Object& operator=(const Object& rhs) = delete;
|
2012-06-16 16:54:55 +02:00
|
|
|
|
2018-01-04 09:43:49 +01:00
|
|
|
uintptr_t m_References{0};
|
|
|
|
mutable uintptr_t m_Mutex{0};
|
2013-03-01 12:07:52 +01:00
|
|
|
|
2014-12-19 12:19:28 +01:00
|
|
|
#ifdef I2_DEBUG
|
2014-11-12 12:32:14 +01:00
|
|
|
# ifndef _WIN32
|
|
|
|
mutable pthread_t m_LockOwner;
|
|
|
|
# else /* _WIN32 */
|
|
|
|
mutable DWORD m_LockOwner;
|
|
|
|
# endif /* _WIN32 */
|
2014-12-19 12:19:28 +01:00
|
|
|
#endif /* I2_DEBUG */
|
2013-03-01 12:07:52 +01:00
|
|
|
|
2013-03-06 11:03:50 +01:00
|
|
|
friend struct ObjectLock;
|
2014-11-08 21:17:16 +01:00
|
|
|
|
|
|
|
friend void intrusive_ptr_add_ref(Object *object);
|
|
|
|
friend void intrusive_ptr_release(Object *object);
|
2013-02-17 19:14:34 +01:00
|
|
|
};
|
|
|
|
|
2017-12-31 07:22:16 +01:00
|
|
|
Value GetPrototypeField(const Value& context, const String& field, bool not_found_error, const DebugInfo& debugInfo);
|
2016-04-18 11:29:43 +02:00
|
|
|
|
2017-12-31 07:22:16 +01:00
|
|
|
void TypeAddObject(Object *object);
|
|
|
|
void TypeRemoveObject(Object *object);
|
2016-01-19 15:24:17 +01:00
|
|
|
|
2018-01-04 10:36:35 +01:00
|
|
|
void intrusive_ptr_add_ref(Object *object);
|
|
|
|
void intrusive_ptr_release(Object *object);
|
2012-03-28 13:24:49 +02:00
|
|
|
|
2013-11-04 23:14:34 +01:00
|
|
|
template<typename T>
|
|
|
|
class ObjectImpl
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
2012-03-28 13:24:49 +02:00
|
|
|
}
|
|
|
|
|
2012-04-02 08:56:30 +02:00
|
|
|
#endif /* OBJECT_H */
|
2014-11-08 21:17:16 +01:00
|
|
|
|
|
|
|
#include "base/type.hpp"
|