1
// defines SwigBoost::shared_ptr, a wrapper around boost::shared_ptr
2
// Use this shared_ptr wrapper for testing memory leaks of shared_ptr.
3
// getTotalCount() should return zero at end of test
7
struct SWIG_null_deleter; // forward reference, definition is in shared_ptr.i
9
// This template can be specialized for better debugging information
10
template <typename T> std::string show_message(boost::shared_ptr<T>*t) {
12
return "null shared_ptr!!!";
13
if (boost::get_deleter<SWIG_null_deleter>(*t))
14
return std::string(typeid(t).name()) + " NULL DELETER";
16
return std::string(typeid(t).name()) + " object";
18
return std::string(typeid(t).name()) + " NULL";
21
namespace SharedPtrWrapper {
22
static SwigExamples::CriticalSection critical_section;
23
static int total_count = 0;
25
template<typename T> void increment(boost::shared_ptr<T>* ptr) {
26
SwigExamples::Lock lock(critical_section);
27
std::cout << ptr << " " << show_message(ptr) << " " << " +" << std::endl << std::flush;
30
template<typename T> void decrement(boost::shared_ptr<T>* ptr) {
31
SwigExamples::Lock lock(critical_section);
32
std::cout << ptr << " " << show_message(ptr) << " " << " -" << std::endl << std::flush;
35
static int getTotalCount() { return total_count; }
38
template<typename T> class shared_ptr {
40
typedef shared_ptr<T> this_type;
42
typedef typename boost::detail::shared_ptr_traits<T>::reference reference;
44
shared_ptr() : m_shared_ptr() {
45
SharedPtrWrapper::increment(&m_shared_ptr);
47
template<typename Y> explicit shared_ptr(Y* p) : m_shared_ptr(p) {
48
SharedPtrWrapper::increment(&m_shared_ptr);
50
template<typename Y, typename D> explicit shared_ptr(Y* p, D d) : m_shared_ptr(p, d) {
51
SharedPtrWrapper::increment(&m_shared_ptr);
54
shared_ptr(shared_ptr const & other)
55
: m_shared_ptr(other.m_shared_ptr)
57
SharedPtrWrapper::increment(&m_shared_ptr);
60
template<typename Y> shared_ptr(shared_ptr<Y> const & other)
61
: m_shared_ptr(other.m_shared_ptr)
63
SharedPtrWrapper::increment(&m_shared_ptr);
66
reference operator*() const {
67
return m_shared_ptr.operator*();
69
T* operator->() const {
70
return m_shared_ptr.operator->();
73
return m_shared_ptr.get();
75
operator bool() const {
76
return m_shared_ptr.get() == 0 ? false : true;
79
return m_shared_ptr.unique();
81
long use_count() const {
82
return m_shared_ptr.use_count();
84
void swap(shared_ptr<T>& other) {
85
std::swap(m_shared_ptr, other.m_shared_ptr);
87
template<class Y> bool _internal_less(shared_ptr<Y> const & rhs) const {
88
return m_shared_ptr < rhs.m_shared_ptr;
91
SharedPtrWrapper::decrement(&m_shared_ptr);
95
template<class Y> friend class shared_ptr;
97
boost::shared_ptr<T> m_shared_ptr;