PageRenderTime 81ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Examples/test-suite/shared_ptr_wrapper.h

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