mirror of
https://github.com/Icinga/icinga2.git
synced 2025-09-23 17:57:54 +02:00
Allow intrusive_ptr<const T> for objects
This allows using ref-counted pointers to const objects. Adds a second typedef so that T::ConstPtr can be used similar to how T::Ptr currently is.
This commit is contained in:
parent
0a08fcc0e1
commit
a49ec1015d
@ -201,14 +201,14 @@ Value icinga::GetPrototypeField(const Value& context, const String& field, bool
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef I2_LEAK_DEBUG
|
#ifdef I2_LEAK_DEBUG
|
||||||
void icinga::TypeAddObject(Object *object)
|
void icinga::TypeAddObject(const Object *object)
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> lock(l_ObjectCountLock);
|
std::unique_lock<std::mutex> lock(l_ObjectCountLock);
|
||||||
String typeName = Utility::GetTypeName(typeid(*object));
|
String typeName = Utility::GetTypeName(typeid(*object));
|
||||||
l_ObjectCounts[typeName]++;
|
l_ObjectCounts[typeName]++;
|
||||||
}
|
}
|
||||||
|
|
||||||
void icinga::TypeRemoveObject(Object *object)
|
void icinga::TypeRemoveObject(const Object *object)
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> lock(l_ObjectCountLock);
|
std::unique_lock<std::mutex> lock(l_ObjectCountLock);
|
||||||
String typeName = Utility::GetTypeName(typeid(*object));
|
String typeName = Utility::GetTypeName(typeid(*object));
|
||||||
@ -239,7 +239,7 @@ INITIALIZE_ONCE([]() {
|
|||||||
});
|
});
|
||||||
#endif /* I2_LEAK_DEBUG */
|
#endif /* I2_LEAK_DEBUG */
|
||||||
|
|
||||||
void icinga::intrusive_ptr_add_ref(Object *object)
|
void icinga::intrusive_ptr_add_ref(const Object *object)
|
||||||
{
|
{
|
||||||
#ifdef I2_LEAK_DEBUG
|
#ifdef I2_LEAK_DEBUG
|
||||||
if (object->m_References.fetch_add(1) == 0u)
|
if (object->m_References.fetch_add(1) == 0u)
|
||||||
@ -249,7 +249,7 @@ void icinga::intrusive_ptr_add_ref(Object *object)
|
|||||||
#endif /* I2_LEAK_DEBUG */
|
#endif /* I2_LEAK_DEBUG */
|
||||||
}
|
}
|
||||||
|
|
||||||
void icinga::intrusive_ptr_release(Object *object)
|
void icinga::intrusive_ptr_release(const Object *object)
|
||||||
{
|
{
|
||||||
auto previous (object->m_References.fetch_sub(1));
|
auto previous (object->m_References.fetch_sub(1));
|
||||||
|
|
||||||
|
@ -31,7 +31,8 @@ class ValidationUtils;
|
|||||||
extern const Value Empty;
|
extern const Value Empty;
|
||||||
|
|
||||||
#define DECLARE_PTR_TYPEDEFS(klass) \
|
#define DECLARE_PTR_TYPEDEFS(klass) \
|
||||||
typedef intrusive_ptr<klass> Ptr
|
typedef intrusive_ptr<klass> Ptr; \
|
||||||
|
typedef intrusive_ptr<const klass> ConstPtr
|
||||||
|
|
||||||
#define IMPL_TYPE_LOOKUP_SUPER() \
|
#define IMPL_TYPE_LOOKUP_SUPER() \
|
||||||
|
|
||||||
@ -192,7 +193,7 @@ private:
|
|||||||
Object(const Object& other) = delete;
|
Object(const Object& other) = delete;
|
||||||
Object& operator=(const Object& rhs) = delete;
|
Object& operator=(const Object& rhs) = delete;
|
||||||
|
|
||||||
std::atomic<uint_fast64_t> m_References;
|
mutable std::atomic<uint_fast64_t> m_References;
|
||||||
mutable std::recursive_mutex m_Mutex;
|
mutable std::recursive_mutex m_Mutex;
|
||||||
|
|
||||||
#ifdef I2_DEBUG
|
#ifdef I2_DEBUG
|
||||||
@ -202,17 +203,17 @@ private:
|
|||||||
|
|
||||||
friend struct ObjectLock;
|
friend struct ObjectLock;
|
||||||
|
|
||||||
friend void intrusive_ptr_add_ref(Object *object);
|
friend void intrusive_ptr_add_ref(const Object *object);
|
||||||
friend void intrusive_ptr_release(Object *object);
|
friend void intrusive_ptr_release(const Object *object);
|
||||||
};
|
};
|
||||||
|
|
||||||
Value GetPrototypeField(const Value& context, const String& field, bool not_found_error, const DebugInfo& debugInfo);
|
Value GetPrototypeField(const Value& context, const String& field, bool not_found_error, const DebugInfo& debugInfo);
|
||||||
|
|
||||||
void TypeAddObject(Object *object);
|
void TypeAddObject(const Object *object);
|
||||||
void TypeRemoveObject(Object *object);
|
void TypeRemoveObject(const Object *object);
|
||||||
|
|
||||||
void intrusive_ptr_add_ref(Object *object);
|
void intrusive_ptr_add_ref(const Object *object);
|
||||||
void intrusive_ptr_release(Object *object);
|
void intrusive_ptr_release(const Object *object);
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
class ObjectImpl
|
class ObjectImpl
|
||||||
|
@ -12,8 +12,8 @@ namespace icinga
|
|||||||
|
|
||||||
class SharedObject;
|
class SharedObject;
|
||||||
|
|
||||||
inline void intrusive_ptr_add_ref(SharedObject *object);
|
inline void intrusive_ptr_add_ref(const SharedObject *object);
|
||||||
inline void intrusive_ptr_release(SharedObject *object);
|
inline void intrusive_ptr_release(const SharedObject *object);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Seamless and polymorphistic base for any class to create shared pointers of.
|
* Seamless and polymorphistic base for any class to create shared pointers of.
|
||||||
@ -23,8 +23,8 @@ inline void intrusive_ptr_release(SharedObject *object);
|
|||||||
*/
|
*/
|
||||||
class SharedObject
|
class SharedObject
|
||||||
{
|
{
|
||||||
friend void intrusive_ptr_add_ref(SharedObject *object);
|
friend void intrusive_ptr_add_ref(const SharedObject *object);
|
||||||
friend void intrusive_ptr_release(SharedObject *object);
|
friend void intrusive_ptr_release(const SharedObject *object);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
inline SharedObject() : m_References(0)
|
inline SharedObject() : m_References(0)
|
||||||
@ -38,15 +38,15 @@ protected:
|
|||||||
~SharedObject() = default;
|
~SharedObject() = default;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Atomic<uint_fast64_t> m_References;
|
mutable Atomic<uint_fast64_t> m_References;
|
||||||
};
|
};
|
||||||
|
|
||||||
inline void intrusive_ptr_add_ref(SharedObject *object)
|
inline void intrusive_ptr_add_ref(const SharedObject *object)
|
||||||
{
|
{
|
||||||
object->m_References.fetch_add(1);
|
object->m_References.fetch_add(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void intrusive_ptr_release(SharedObject *object)
|
inline void intrusive_ptr_release(const SharedObject *object)
|
||||||
{
|
{
|
||||||
if (object->m_References.fetch_sub(1) == 1u) {
|
if (object->m_References.fetch_sub(1) == 1u) {
|
||||||
delete object;
|
delete object;
|
||||||
|
@ -16,13 +16,13 @@ template<class T>
|
|||||||
class Shared;
|
class Shared;
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
inline void intrusive_ptr_add_ref(Shared<T> *object)
|
inline void intrusive_ptr_add_ref(const Shared<T> *object)
|
||||||
{
|
{
|
||||||
object->m_References.fetch_add(1);
|
object->m_References.fetch_add(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
inline void intrusive_ptr_release(Shared<T> *object)
|
inline void intrusive_ptr_release(const Shared<T> *object)
|
||||||
{
|
{
|
||||||
if (object->m_References.fetch_sub(1) == 1u) {
|
if (object->m_References.fetch_sub(1) == 1u) {
|
||||||
delete object;
|
delete object;
|
||||||
@ -38,11 +38,12 @@ inline void intrusive_ptr_release(Shared<T> *object)
|
|||||||
template<class T>
|
template<class T>
|
||||||
class Shared : public T
|
class Shared : public T
|
||||||
{
|
{
|
||||||
friend void intrusive_ptr_add_ref<>(Shared<T> *object);
|
friend void intrusive_ptr_add_ref<>(const Shared<T> *object);
|
||||||
friend void intrusive_ptr_release<>(Shared<T> *object);
|
friend void intrusive_ptr_release<>(const Shared<T> *object);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
typedef boost::intrusive_ptr<Shared> Ptr;
|
typedef boost::intrusive_ptr<Shared> Ptr;
|
||||||
|
typedef boost::intrusive_ptr<const Shared> ConstPtr;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Like std::make_shared, but for this class.
|
* Like std::make_shared, but for this class.
|
||||||
@ -94,7 +95,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Atomic<uint_fast64_t> m_References;
|
mutable Atomic<uint_fast64_t> m_References;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user