49 lines
695 B
C++
49 lines
695 B
C++
#pragma once
|
|
|
|
#include "IntrusivePtr.h"
|
|
#include <cassert>
|
|
|
|
namespace rp {
|
|
namespace mixin {
|
|
|
|
class ref_counted
|
|
{
|
|
public:
|
|
ref_counted() : m_ref_count( 0 )
|
|
{
|
|
}
|
|
virtual ~ref_counted()
|
|
{
|
|
}
|
|
int acquire()
|
|
{
|
|
return ++m_ref_count;
|
|
}
|
|
int dispose()
|
|
{
|
|
assert( m_ref_count > 0 || !"wrong reference counting!" );
|
|
int ref = --m_ref_count;
|
|
if( !m_ref_count )
|
|
delete this;
|
|
return ref;
|
|
}
|
|
int ref_count() const
|
|
{
|
|
return m_ref_count;
|
|
}
|
|
private:
|
|
int m_ref_count;
|
|
};
|
|
|
|
} // mixin
|
|
|
|
inline void intrusive_ptr_acquire( mixin::ref_counted* p )
|
|
{
|
|
p->acquire();
|
|
}
|
|
inline void intrusive_ptr_dispose( mixin::ref_counted* p )
|
|
{
|
|
p->dispose();
|
|
}
|
|
|
|
} // rp
|