Skip to content

Commit a49ec10

Browse files
committed
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.
1 parent 0a08fcc commit a49ec10

File tree

4 files changed

+26
-24
lines changed

4 files changed

+26
-24
lines changed

lib/base/object.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,14 +201,14 @@ Value icinga::GetPrototypeField(const Value& context, const String& field, bool
201201
}
202202

203203
#ifdef I2_LEAK_DEBUG
204-
void icinga::TypeAddObject(Object *object)
204+
void icinga::TypeAddObject(const Object *object)
205205
{
206206
std::unique_lock<std::mutex> lock(l_ObjectCountLock);
207207
String typeName = Utility::GetTypeName(typeid(*object));
208208
l_ObjectCounts[typeName]++;
209209
}
210210

211-
void icinga::TypeRemoveObject(Object *object)
211+
void icinga::TypeRemoveObject(const Object *object)
212212
{
213213
std::unique_lock<std::mutex> lock(l_ObjectCountLock);
214214
String typeName = Utility::GetTypeName(typeid(*object));
@@ -239,7 +239,7 @@ INITIALIZE_ONCE([]() {
239239
});
240240
#endif /* I2_LEAK_DEBUG */
241241

242-
void icinga::intrusive_ptr_add_ref(Object *object)
242+
void icinga::intrusive_ptr_add_ref(const Object *object)
243243
{
244244
#ifdef I2_LEAK_DEBUG
245245
if (object->m_References.fetch_add(1) == 0u)
@@ -249,7 +249,7 @@ void icinga::intrusive_ptr_add_ref(Object *object)
249249
#endif /* I2_LEAK_DEBUG */
250250
}
251251

252-
void icinga::intrusive_ptr_release(Object *object)
252+
void icinga::intrusive_ptr_release(const Object *object)
253253
{
254254
auto previous (object->m_References.fetch_sub(1));
255255

lib/base/object.hpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ class ValidationUtils;
3131
extern const Value Empty;
3232

3333
#define DECLARE_PTR_TYPEDEFS(klass) \
34-
typedef intrusive_ptr<klass> Ptr
34+
typedef intrusive_ptr<klass> Ptr; \
35+
typedef intrusive_ptr<const klass> ConstPtr
3536

3637
#define IMPL_TYPE_LOOKUP_SUPER() \
3738

@@ -192,7 +193,7 @@ class Object
192193
Object(const Object& other) = delete;
193194
Object& operator=(const Object& rhs) = delete;
194195

195-
std::atomic<uint_fast64_t> m_References;
196+
mutable std::atomic<uint_fast64_t> m_References;
196197
mutable std::recursive_mutex m_Mutex;
197198

198199
#ifdef I2_DEBUG
@@ -202,17 +203,17 @@ class Object
202203

203204
friend struct ObjectLock;
204205

205-
friend void intrusive_ptr_add_ref(Object *object);
206-
friend void intrusive_ptr_release(Object *object);
206+
friend void intrusive_ptr_add_ref(const Object *object);
207+
friend void intrusive_ptr_release(const Object *object);
207208
};
208209

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

211-
void TypeAddObject(Object *object);
212-
void TypeRemoveObject(Object *object);
212+
void TypeAddObject(const Object *object);
213+
void TypeRemoveObject(const Object *object);
213214

214-
void intrusive_ptr_add_ref(Object *object);
215-
void intrusive_ptr_release(Object *object);
215+
void intrusive_ptr_add_ref(const Object *object);
216+
void intrusive_ptr_release(const Object *object);
216217

217218
template<typename T>
218219
class ObjectImpl

lib/base/shared-object.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ namespace icinga
1212

1313
class SharedObject;
1414

15-
inline void intrusive_ptr_add_ref(SharedObject *object);
16-
inline void intrusive_ptr_release(SharedObject *object);
15+
inline void intrusive_ptr_add_ref(const SharedObject *object);
16+
inline void intrusive_ptr_release(const SharedObject *object);
1717

1818
/**
1919
* Seamless and polymorphistic base for any class to create shared pointers of.
@@ -23,8 +23,8 @@ inline void intrusive_ptr_release(SharedObject *object);
2323
*/
2424
class SharedObject
2525
{
26-
friend void intrusive_ptr_add_ref(SharedObject *object);
27-
friend void intrusive_ptr_release(SharedObject *object);
26+
friend void intrusive_ptr_add_ref(const SharedObject *object);
27+
friend void intrusive_ptr_release(const SharedObject *object);
2828

2929
protected:
3030
inline SharedObject() : m_References(0)
@@ -38,15 +38,15 @@ class SharedObject
3838
~SharedObject() = default;
3939

4040
private:
41-
Atomic<uint_fast64_t> m_References;
41+
mutable Atomic<uint_fast64_t> m_References;
4242
};
4343

44-
inline void intrusive_ptr_add_ref(SharedObject *object)
44+
inline void intrusive_ptr_add_ref(const SharedObject *object)
4545
{
4646
object->m_References.fetch_add(1);
4747
}
4848

49-
inline void intrusive_ptr_release(SharedObject *object)
49+
inline void intrusive_ptr_release(const SharedObject *object)
5050
{
5151
if (object->m_References.fetch_sub(1) == 1u) {
5252
delete object;

lib/base/shared.hpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ template<class T>
1616
class Shared;
1717

1818
template<class T>
19-
inline void intrusive_ptr_add_ref(Shared<T> *object)
19+
inline void intrusive_ptr_add_ref(const Shared<T> *object)
2020
{
2121
object->m_References.fetch_add(1);
2222
}
2323

2424
template<class T>
25-
inline void intrusive_ptr_release(Shared<T> *object)
25+
inline void intrusive_ptr_release(const Shared<T> *object)
2626
{
2727
if (object->m_References.fetch_sub(1) == 1u) {
2828
delete object;
@@ -38,11 +38,12 @@ inline void intrusive_ptr_release(Shared<T> *object)
3838
template<class T>
3939
class Shared : public T
4040
{
41-
friend void intrusive_ptr_add_ref<>(Shared<T> *object);
42-
friend void intrusive_ptr_release<>(Shared<T> *object);
41+
friend void intrusive_ptr_add_ref<>(const Shared<T> *object);
42+
friend void intrusive_ptr_release<>(const Shared<T> *object);
4343

4444
public:
4545
typedef boost::intrusive_ptr<Shared> Ptr;
46+
typedef boost::intrusive_ptr<const Shared> ConstPtr;
4647

4748
/**
4849
* Like std::make_shared, but for this class.
@@ -94,7 +95,7 @@ class Shared : public T
9495
}
9596

9697
private:
97-
Atomic<uint_fast64_t> m_References;
98+
mutable Atomic<uint_fast64_t> m_References;
9899
};
99100

100101
}

0 commit comments

Comments
 (0)