MAINT-4952: Add IntrusivePtr wrapper for boost::intrusive_ptr.

For a RefCounted subclass T, boost::intrusive_ptr<T> must be instantiated as
boost::intrusive_ptr<T>(raw ptr, false) to avoid immortal instances.
Forgetting that final bool parameter is both easy and extremely hard to spot
with desk checking or code review. IntrusivePtr<T> provides constructors that
Do The Right Thing, so we can typedef a subclass T's ptr_t to IntrusivePtr<T>
rather than directly to boost::intrusive_ptr<T>.
meow-7.2.2
Nat Goodspeed 2015-06-29 15:37:32 -04:00
parent 8bd39583fe
commit 76cb1fcf0b
1 changed files with 19 additions and 0 deletions

View File

@ -32,6 +32,7 @@
#include "fix_macros.h"
#include <boost/thread.hpp>
#include <boost/intrusive_ptr.hpp>
#include "llapr.h"
@ -120,6 +121,24 @@ inline void RefCounted::destroySelf()
delete this;
}
/**
* boost::intrusive_ptr may be used to manage RefCounted classes.
* Unfortunately RefCounted and boost::intrusive_ptr use different conventions
* for the initial refcount value. To avoid leaky (immortal) objects, you
* should really construct boost::intrusive_ptr<RefCounted*>(rawptr, false).
* IntrusivePtr<T> encapsulates that for you.
*/
template <typename T>
struct IntrusivePtr: public boost::intrusive_ptr<T>
{
IntrusivePtr():
boost::intrusive_ptr<T>()
{}
IntrusivePtr(T* p):
boost::intrusive_ptr<T>(p, false)
{}
};
inline void intrusive_ptr_add_ref(RefCounted* p)
{
p->addRef();