/trunk/Examples/test-suite/shared_ptr_wrapper.h
C++ Header | 100 lines | 84 code | 12 blank | 4 comment | 4 complexity | 51692de0851a538effc8219760a5927c MD5 | raw file
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 4 5#include <iostream> 6 7struct SWIG_null_deleter; // forward reference, definition is in shared_ptr.i 8namespace SwigBoost { 9// This template can be specialized for better debugging information 10template <typename T> std::string show_message(boost::shared_ptr<T>*t) { 11 if (!t) 12 return "null shared_ptr!!!"; 13 if (boost::get_deleter<SWIG_null_deleter>(*t)) 14 return std::string(typeid(t).name()) + " NULL DELETER"; 15 if (*t) 16 return std::string(typeid(t).name()) + " object"; 17 else 18 return std::string(typeid(t).name()) + " NULL"; 19} 20 21namespace SharedPtrWrapper { 22 static SwigExamples::CriticalSection critical_section; 23 static int total_count = 0; 24 25 template<typename T> void increment(boost::shared_ptr<T>* ptr) { 26 SwigExamples::Lock lock(critical_section); 27 std::cout << "====SharedPtrWrapper==== + " << ptr << " " << show_message(ptr) << " " << std::endl << std::flush; 28 total_count++; 29 } 30 template<typename T> void decrement(boost::shared_ptr<T>* ptr) { 31 SwigExamples::Lock lock(critical_section); 32 std::cout << "====SharedPtrWrapper==== - " << ptr << " " << show_message(ptr) << " " << std::endl << std::flush; 33 total_count--; 34 } 35 static int getTotalCount() { return total_count; } 36} 37 38template<typename T> class shared_ptr { 39private: 40 typedef shared_ptr<T> this_type; 41public: 42 typedef typename boost::detail::shared_ptr_traits<T>::reference reference; 43 44 shared_ptr() : m_shared_ptr() { 45 SharedPtrWrapper::increment(&m_shared_ptr); 46 } 47 template<typename Y> explicit shared_ptr(Y* p) : m_shared_ptr(p) { 48 SharedPtrWrapper::increment(&m_shared_ptr); 49 } 50 template<typename Y, typename D> explicit shared_ptr(Y* p, D d) : m_shared_ptr(p, d) { 51 SharedPtrWrapper::increment(&m_shared_ptr); 52 } 53 54 shared_ptr(shared_ptr const & other) 55 : m_shared_ptr(other.m_shared_ptr) 56 { 57 SharedPtrWrapper::increment(&m_shared_ptr); 58 } 59 60 template<typename Y> shared_ptr(shared_ptr<Y> const & other) 61 : m_shared_ptr(other.m_shared_ptr) 62 { 63 SharedPtrWrapper::increment(&m_shared_ptr); 64 } 65 66 reference operator*() const { 67 return m_shared_ptr.operator*(); 68 } 69 T* operator->() const { 70 return m_shared_ptr.operator->(); 71 } 72 T* get() const { 73 return m_shared_ptr.get(); 74 } 75 operator bool() const { 76 return m_shared_ptr.get() == 0 ? false : true; 77 } 78 bool unique() const { 79 return m_shared_ptr.unique(); 80 } 81 long use_count() const { 82 return m_shared_ptr.use_count(); 83 } 84 void swap(shared_ptr<T>& other) { 85 std::swap(m_shared_ptr, other.m_shared_ptr); 86 } 87 template<class Y> bool _internal_less(shared_ptr<Y> const & rhs) const { 88 return m_shared_ptr < rhs.m_shared_ptr; 89 } 90 ~shared_ptr() { 91 SharedPtrWrapper::decrement(&m_shared_ptr); 92 } 93 94private: 95 template<class Y> friend class shared_ptr; 96 97 boost::shared_ptr<T> m_shared_ptr; 98}; 99} 100