- Cal3D 0.11 API Reference -

refcounted.h
1 #ifndef CAL_REF_COUNTED_H
2 #define CAL_REF_COUNTED_H
3 
4 
5 #include "cal3d/platform.h"
6 
7 
8 namespace cal3d
9 {
10 
11  template<typename T> class RefPtr;
12 
28  class CAL3D_API RefCounted
29  {
30  template<typename T> friend T* explicitIncRef(T* p);
31  friend void explicitDecRef(RefCounted* p);
32 
33  protected:
34  RefCounted()
35  : m_refCount(0)
36  {
37  }
38 
46  virtual ~RefCounted()
47  {
48  assert(m_refCount == 0 && "_refCount nonzero in destructor");
49  }
50 
51  // Must use RefPtr instead of manually calling incRef() and decRef().
52  private:
53  void incRef()
54  {
55  assert(m_refCount >= 0 && "_refCount is less than zero in incRef()!");
56  ++m_refCount;
57  }
58 
63  void decRef()
64  {
65  assert(m_refCount > 0 &&
66  "_refCount is less than or equal to zero in decRef()!");
67  if (--m_refCount == 0)
68  {
69  delete this;
70  }
71  }
72 
73  public:
74  int getRefCount() const
75  {
76  return m_refCount;
77  }
78 
79  private:
80  // Copying a RefCounted object must be done manually by the
81  // subclass. Otherwise the refCount gets copied too, and
82  // that's Bad.
83  RefCounted(const RefCounted& rhs);
84  RefCounted& operator=(const RefCounted& rhs);
85 
86  private:
87  int m_refCount;
88  };
89 
90  template<typename T>
91  T* explicitIncRef(T* p)
92  {
93  p->incRef();
94  return p;
95  }
96 
97  inline void explicitDecRef(RefCounted* p)
98  {
99  p->decRef();
100  }
101 
102 }
103 
104 
105 #endif
A container-safe smart pointer used for refcounted classes.
Definition: refcounted.h:11
virtual ~RefCounted()
Protected so users of refcounted classes don't use std::auto_ptr or the delete operator.
Definition: refcounted.h:46
Derive from RefCounted to make your class have reference-counted lifetime semantics.
Definition: refcounted.h:28

Generated at Thu Sep 4 2014 00:32:52 by The Cal3D Team with Doxygen 1.8.7