2012-05-10 12:06:41 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2015-01-22 12:00:23 +01:00
|
|
|
* Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org) *
|
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"
|
2013-12-17 12:23:19 +01:00
|
|
|
#include "base/thinmutex.hpp"
|
2013-03-18 22:40:40 +01:00
|
|
|
|
2014-12-19 12:19:28 +01:00
|
|
|
#ifndef I2_DEBUG
|
2013-03-15 18:21:29 +01:00
|
|
|
#include <boost/thread/mutex.hpp>
|
2014-12-19 12:19:28 +01:00
|
|
|
#else /* I2_DEBUG */
|
2013-03-18 22:40:40 +01:00
|
|
|
#include <boost/thread/recursive_mutex.hpp>
|
2014-12-19 12:19:28 +01:00
|
|
|
#endif /* I2_DEBUG */
|
2013-03-18 22:40:40 +01:00
|
|
|
|
2015-03-29 00:03:47 +01:00
|
|
|
#include <boost/thread/condition_variable.hpp>
|
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
#include <boost/smart_ptr/intrusive_ptr.hpp>
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
#include <boost/tuple/tuple.hpp>
|
2014-04-03 15:36:13 +02:00
|
|
|
using boost::tie;
|
2013-03-18 19:02:42 +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;
|
2012-06-16 13:06:06 +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-03-05 07:42:13 +01:00
|
|
|
#define IMPL_TYPE_LOOKUP() \
|
2014-11-08 21:17:16 +01:00
|
|
|
static intrusive_ptr<Type> TypeInstance; \
|
|
|
|
virtual intrusive_ptr<Type> GetReflectionType(void) const \
|
|
|
|
{ \
|
|
|
|
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
|
|
|
|
2014-11-07 12:32:25 +01:00
|
|
|
template<typename T>
|
2014-11-08 21:17:16 +01:00
|
|
|
intrusive_ptr<Object> DefaultObjectFactory(void)
|
2014-11-07 12:32:25 +01:00
|
|
|
{
|
2014-11-08 21:17:16 +01:00
|
|
|
return new T();
|
2014-11-07 12:32:25 +01:00
|
|
|
}
|
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
typedef intrusive_ptr<Object> (*ObjectFactory)(void);
|
2013-11-04 23:14:34 +01:00
|
|
|
|
2014-11-07 12:32:25 +01:00
|
|
|
template<typename T>
|
|
|
|
struct TypeHelper
|
|
|
|
{
|
|
|
|
static ObjectFactory GetFactory(void)
|
|
|
|
{
|
|
|
|
return DefaultObjectFactory<T>;
|
|
|
|
}
|
|
|
|
};
|
2014-11-03 00:44:04 +01:00
|
|
|
|
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
|
|
|
*/
|
2014-11-08 21:17:16 +01:00
|
|
|
class I2_BASE_API Object
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
2012-05-21 23:42:54 +02:00
|
|
|
public:
|
2014-11-03 00:44:04 +01:00
|
|
|
DECLARE_OBJECT(Object);
|
2012-03-28 13:24:49 +02:00
|
|
|
|
2013-03-10 09:08:59 +01:00
|
|
|
Object(void);
|
|
|
|
virtual ~Object(void);
|
|
|
|
|
2014-12-08 09:12:40 +01:00
|
|
|
virtual String ToString(void) const;
|
|
|
|
|
2013-11-04 23:14:34 +01:00
|
|
|
virtual void SetField(int id, const Value& value);
|
|
|
|
virtual Value GetField(int id) const;
|
|
|
|
|
2014-12-19 12:19:28 +01:00
|
|
|
#ifdef I2_DEBUG
|
2013-03-04 15:52:42 +01:00
|
|
|
bool OwnsLock(void) const;
|
2014-12-19 12:19:28 +01:00
|
|
|
#endif /* I2_DEBUG */
|
2012-08-03 13:19:55 +02:00
|
|
|
|
2014-11-11 23:06:47 +01:00
|
|
|
void InflateMutex(void);
|
|
|
|
|
2014-12-12 15:19:23 +01:00
|
|
|
static Object::Ptr GetPrototype(void);
|
|
|
|
|
2012-06-16 13:06:06 +02:00
|
|
|
private:
|
2012-06-16 16:54:55 +02:00
|
|
|
Object(const Object& other);
|
2012-08-07 21:02:12 +02:00
|
|
|
Object& operator=(const Object& rhs);
|
2012-06-16 16:54:55 +02:00
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
uintptr_t m_References;
|
2013-12-17 12:23:19 +01:00
|
|
|
mutable ThinMutex m_Mutex;
|
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
|
|
|
};
|
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
inline void intrusive_ptr_add_ref(Object *object)
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
2014-11-08 21:17:16 +01:00
|
|
|
#ifdef _WIN32
|
|
|
|
InterlockedIncrement(&object->m_References);
|
|
|
|
#else /* _WIN32 */
|
|
|
|
__sync_add_and_fetch(&object->m_References, 1);
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
}
|
2012-03-28 13:24:49 +02:00
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
inline void intrusive_ptr_release(Object *object)
|
|
|
|
{
|
|
|
|
uintptr_t refs;
|
|
|
|
#ifdef _WIN32
|
|
|
|
refs = InterlockedDecrement(&object->m_References);
|
|
|
|
#else /* _WIN32 */
|
|
|
|
refs = __sync_sub_and_fetch(&object->m_References, 1);
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
|
|
|
if (refs == 0)
|
|
|
|
delete 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"
|