diff --git a/lib/base/object.cpp b/lib/base/object.cpp index 5c7c67a8e..0c1e69e8e 100644 --- a/lib/base/object.cpp +++ b/lib/base/object.cpp @@ -201,14 +201,14 @@ Value icinga::GetPrototypeField(const Value& context, const String& field, bool } #ifdef I2_LEAK_DEBUG -void icinga::TypeAddObject(Object *object) +void icinga::TypeAddObject(const Object *object) { std::unique_lock lock(l_ObjectCountLock); String typeName = Utility::GetTypeName(typeid(*object)); l_ObjectCounts[typeName]++; } -void icinga::TypeRemoveObject(Object *object) +void icinga::TypeRemoveObject(const Object *object) { std::unique_lock lock(l_ObjectCountLock); String typeName = Utility::GetTypeName(typeid(*object)); @@ -239,7 +239,7 @@ INITIALIZE_ONCE([]() { }); #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 if (object->m_References.fetch_add(1) == 0u) @@ -249,7 +249,7 @@ void icinga::intrusive_ptr_add_ref(Object *object) #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)); diff --git a/lib/base/object.hpp b/lib/base/object.hpp index 7f520672e..dc08db043 100644 --- a/lib/base/object.hpp +++ b/lib/base/object.hpp @@ -31,7 +31,8 @@ class ValidationUtils; extern const Value Empty; #define DECLARE_PTR_TYPEDEFS(klass) \ - typedef intrusive_ptr Ptr + typedef intrusive_ptr Ptr; \ + typedef intrusive_ptr ConstPtr #define IMPL_TYPE_LOOKUP_SUPER() \ @@ -192,7 +193,7 @@ private: Object(const Object& other) = delete; Object& operator=(const Object& rhs) = delete; - std::atomic m_References; + mutable std::atomic m_References; mutable std::recursive_mutex m_Mutex; #ifdef I2_DEBUG @@ -202,17 +203,17 @@ private: friend struct ObjectLock; - friend void intrusive_ptr_add_ref(Object *object); - friend void intrusive_ptr_release(Object *object); + friend void intrusive_ptr_add_ref(const 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); -void TypeAddObject(Object *object); -void TypeRemoveObject(Object *object); +void TypeAddObject(const Object *object); +void TypeRemoveObject(const Object *object); -void intrusive_ptr_add_ref(Object *object); -void intrusive_ptr_release(Object *object); +void intrusive_ptr_add_ref(const Object *object); +void intrusive_ptr_release(const Object *object); template class ObjectImpl diff --git a/lib/base/shared-object.hpp b/lib/base/shared-object.hpp index 396969db6..a434f404c 100644 --- a/lib/base/shared-object.hpp +++ b/lib/base/shared-object.hpp @@ -12,8 +12,8 @@ namespace icinga class SharedObject; -inline void intrusive_ptr_add_ref(SharedObject *object); -inline void intrusive_ptr_release(SharedObject *object); +inline void intrusive_ptr_add_ref(const SharedObject *object); +inline void intrusive_ptr_release(const SharedObject *object); /** * 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 { - friend void intrusive_ptr_add_ref(SharedObject *object); - friend void intrusive_ptr_release(SharedObject *object); + friend void intrusive_ptr_add_ref(const SharedObject *object); + friend void intrusive_ptr_release(const SharedObject *object); protected: inline SharedObject() : m_References(0) @@ -38,15 +38,15 @@ protected: ~SharedObject() = default; private: - Atomic m_References; + mutable Atomic 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); } -inline void intrusive_ptr_release(SharedObject *object) +inline void intrusive_ptr_release(const SharedObject *object) { if (object->m_References.fetch_sub(1) == 1u) { delete object; diff --git a/lib/base/shared.hpp b/lib/base/shared.hpp index 2acec012e..2c9d0fcee 100644 --- a/lib/base/shared.hpp +++ b/lib/base/shared.hpp @@ -16,13 +16,13 @@ template class Shared; template -inline void intrusive_ptr_add_ref(Shared *object) +inline void intrusive_ptr_add_ref(const Shared *object) { object->m_References.fetch_add(1); } template -inline void intrusive_ptr_release(Shared *object) +inline void intrusive_ptr_release(const Shared *object) { if (object->m_References.fetch_sub(1) == 1u) { delete object; @@ -38,11 +38,12 @@ inline void intrusive_ptr_release(Shared *object) template class Shared : public T { - friend void intrusive_ptr_add_ref<>(Shared *object); - friend void intrusive_ptr_release<>(Shared *object); + friend void intrusive_ptr_add_ref<>(const Shared *object); + friend void intrusive_ptr_release<>(const Shared *object); public: typedef boost::intrusive_ptr Ptr; + typedef boost::intrusive_ptr ConstPtr; /** * Like std::make_shared, but for this class. @@ -94,7 +95,7 @@ public: } private: - Atomic m_References; + mutable Atomic m_References; }; }