summaryrefslogtreecommitdiffstats
path: root/lib/base/object.hpp
blob: 5a90cfa64f55b81b403f6b3c8d06ff37f3f35ff3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */

#ifndef OBJECT_H
#define OBJECT_H

#include "base/i2-base.hpp"
#include "base/debug.hpp"
#include <boost/smart_ptr/intrusive_ptr.hpp>
#include <atomic>
#include <cstddef>
#include <cstdint>
#include <mutex>
#include <thread>
#include <vector>

using boost::intrusive_ptr;
using boost::dynamic_pointer_cast;
using boost::static_pointer_cast;

namespace icinga
{

class Value;
class Object;
class Type;
class String;
struct DebugInfo;
class ValidationUtils;

extern Value Empty;

#define DECLARE_PTR_TYPEDEFS(klass) \
	typedef intrusive_ptr<klass> Ptr

#define IMPL_TYPE_LOOKUP_SUPER() 					\

#define IMPL_TYPE_LOOKUP() 							\
	static intrusive_ptr<Type> TypeInstance;				\
	virtual intrusive_ptr<Type> GetReflectionType() const override		\
	{									\
		return TypeInstance;						\
	}

#define DECLARE_OBJECT(klass) \
	DECLARE_PTR_TYPEDEFS(klass); \
	IMPL_TYPE_LOOKUP();

#define REQUIRE_NOT_NULL(ptr) RequireNotNullInternal(ptr, #ptr)

void RequireNotNullInternal(const intrusive_ptr<Object>& object, const char *description);

void DefaultObjectFactoryCheckArgs(const std::vector<Value>& args);

template<typename T>
intrusive_ptr<Object> DefaultObjectFactory(const std::vector<Value>& args)
{
	DefaultObjectFactoryCheckArgs(args);

	return new T();
}

template<typename T>
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>
struct TypeHelper
{
};

template<typename T>
struct TypeHelper<T, false>
{
	static ObjectFactory GetFactory()
	{
		return DefaultObjectFactory<T>;
	}
};

template<typename T>
struct TypeHelper<T, true>
{
	static ObjectFactory GetFactory()
	{
		return DefaultObjectFactoryVA<T>;
	}
};

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)
			return Lazy<U>(static_cast<U>(m_Value));
		else {
			Accessor accessor = m_Accessor;
			return Lazy<U>(static_cast<typename Lazy<U>::Accessor>([accessor]() { return static_cast<U>(accessor()); }));
		}
	}

	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;
};

/**
 * Base class for all heap-allocated objects. At least one of its methods
 * has to be virtual for RTTI to work.
 *
 * @ingroup base
 */
class Object
{
public:
	DECLARE_PTR_TYPEDEFS(Object);

	Object();
	virtual ~Object();

	virtual String ToString() const;

	virtual intrusive_ptr<Type> GetReflectionType() const;

	virtual void Validate(int types, const ValidationUtils& utils);

	virtual void SetField(int id, const Value& value, bool suppress_events = false, const Value& cookie = Empty);
	virtual Value GetField(int id) const;
	virtual Value GetFieldByName(const String& field, bool sandboxed, const DebugInfo& debugInfo) const;
	virtual void SetFieldByName(const String& field, const Value& value, bool overrideFrozen, const DebugInfo& debugInfo);
	virtual bool HasOwnField(const String& field) const;
	virtual bool GetOwnField(const String& field, Value *result) const;
	virtual void ValidateField(int id, const Lazy<Value>& lvalue, const ValidationUtils& utils);
	virtual void NotifyField(int id, const Value& cookie = Empty);
	virtual Object::Ptr NavigateField(int id) const;

#ifdef I2_DEBUG
	bool OwnsLock() const;
#endif /* I2_DEBUG */

	static Object::Ptr GetPrototype();

	virtual Object::Ptr Clone() const;

	static intrusive_ptr<Type> TypeInstance;

private:
	Object(const Object& other) = delete;
	Object& operator=(const Object& rhs) = delete;

	std::atomic<uint_fast64_t> m_References;
	mutable std::recursive_mutex m_Mutex;

#ifdef I2_DEBUG
	mutable std::atomic<std::thread::id> m_LockOwner;
	mutable size_t m_LockCount = 0;
#endif /* I2_DEBUG */

	friend struct ObjectLock;

	friend void intrusive_ptr_add_ref(Object *object);
	friend void intrusive_ptr_release(Object *object);
};

Value GetPrototypeField(const Value& context, const String& field, bool not_found_error, const DebugInfo& debugInfo);

void TypeAddObject(Object *object);
void TypeRemoveObject(Object *object);

void intrusive_ptr_add_ref(Object *object);
void intrusive_ptr_release(Object *object);

template<typename T>
class ObjectImpl
{
};

}

#endif /* OBJECT_H */

#include "base/type.hpp"