~ubuntu-branches/ubuntu/oneiric/swig1.3/oneiric

« back to all changes in this revision

Viewing changes to Examples/test-suite/shared_ptr_wrapper.h

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2007-12-06 10:27:08 UTC
  • mfrom: (1.2.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20071206102708-t37t62i45n595w0e
Tags: 1.3.33-2ubuntu1
* Merge with Debian; remaining changes:
  - Drop support for pike.
  - Use python2.5 instead of python2.4.
  - Clean Runtime/ as well.
  - Force a few environment variables.
* debian/Rules (clean): Remove Lib/ocaml/swigp4.ml.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
7
struct SWIG_null_deleter; // forward reference, definition is in shared_ptr.i
 
8
namespace SwigBoost {
 
9
// This template can be specialized for better debugging information
 
10
template <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
 
 
21
namespace 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 << 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 << ptr << " " << show_message(ptr) << " " <<  " -" << std::endl << std::flush;
 
33
    total_count--;
 
34
  }
 
35
  static int getTotalCount() { return total_count; }
 
36
}
 
37
 
 
38
template<typename T> class shared_ptr {
 
39
private:
 
40
    typedef shared_ptr<T> this_type;
 
41
public:
 
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
 
 
94
private:
 
95
  template<class Y> friend class shared_ptr;
 
96
 
 
97
  boost::shared_ptr<T> m_shared_ptr;
 
98
};
 
99
}
 
100