icinga2/lib/base/object.hpp

160 lines
4.0 KiB
C++
Raw Normal View History

/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
* *
* 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. *
******************************************************************************/
#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"
#include "base/thinmutex.hpp"
2013-03-18 22:40:40 +01:00
#include <boost/thread/thread.hpp>
#ifndef _DEBUG
2013-03-15 18:21:29 +01:00
#include <boost/thread/mutex.hpp>
2013-03-18 22:40:40 +01:00
#else /* _DEBUG */
#include <boost/thread/recursive_mutex.hpp>
#endif /* _DEBUG */
#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;
#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
{
class Value;
2014-11-07 12:32:25 +01:00
class Object;
class Type;
class String;
#define DECLARE_PTR_TYPEDEFS(klass) \
typedef intrusive_ptr<klass> Ptr
#define IMPL_TYPE_LOOKUP(klass) \
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); \
IMPL_TYPE_LOOKUP(klass);
2014-11-07 12:32:25 +01:00
template<typename T>
intrusive_ptr<Object> DefaultObjectFactory(void)
2014-11-07 12:32:25 +01:00
{
return new T();
2014-11-07 12:32:25 +01:00
}
typedef intrusive_ptr<Object> (*ObjectFactory)(void);
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
*/
class I2_BASE_API Object
2012-03-28 13:24:49 +02:00
{
public:
2014-11-03 00:44:04 +01:00
DECLARE_OBJECT(Object);
2012-03-28 13:24:49 +02:00
Object(void);
virtual ~Object(void);
virtual String ToString(void) const;
virtual void SetField(int id, const Value& value);
virtual Value GetField(int id) const;
2013-03-04 15:52:42 +01:00
#ifdef _DEBUG
bool OwnsLock(void) const;
#endif /* _DEBUG */
2012-08-03 13:19:55 +02:00
2014-11-11 23:06:47 +01:00
void InflateMutex(void);
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
uintptr_t m_References;
mutable ThinMutex m_Mutex;
2013-03-01 12:07:52 +01:00
#ifdef _DEBUG
# ifndef _WIN32
mutable pthread_t m_LockOwner;
# else /* _WIN32 */
mutable DWORD m_LockOwner;
# endif /* _WIN32 */
2013-03-02 09:07:47 +01:00
#endif /* _DEBUG */
2013-03-01 12:07:52 +01:00
2013-03-06 11:03:50 +01:00
friend struct ObjectLock;
friend void intrusive_ptr_add_ref(Object *object);
friend void intrusive_ptr_release(Object *object);
2013-02-17 19:14:34 +01:00
};
inline void intrusive_ptr_add_ref(Object *object)
2012-03-28 13:24:49 +02: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
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
template<typename T>
class ObjectImpl
{
};
2012-03-28 13:24:49 +02:00
}
#endif /* OBJECT_H */
#include "base/type.hpp"