Newton Dynamics  4.00
ndRefCounter.h
1 /* Copyright (c) <2003-2021> <Newton Game Dynamics>
2 *
3 * This software is provided 'as-is', without any express or implied
4 * warranty. In no event will the authors be held liable for any damages
5 * arising from the use of this software.
6 *
7 * Permission is granted to anyone to use this software for any purpose,
8 * including commercial applications, and to alter it and redistribute it
9 * freely
10 */
11 
12 #ifndef __NDREF_COUNTER_H__
13 #define __NDREF_COUNTER_H__
14 
15 #include "ndCoreStdafx.h"
16 #include "ndClassAlloc.h"
17 
18 template<class T>
20 {
21  public:
22  ndRefCounter();
23  ndInt32 GetRef() const;
24 
25  T* AddRef();
26  virtual ndInt32 Release();
27 
28  protected:
29  virtual ~ndRefCounter(void);
30 
31  private:
32  ndInt32 m_refCount;
33 };
34 
35 template<class T>
37  :ndClassAlloc()
38  ,m_refCount(1)
39 {
40 }
41 
42 template<class T>
44 {
45 }
46 
47 template<class T>
48 inline T* ndRefCounter<T>::AddRef()
49 {
50  m_refCount++;
51  return (T*) this;
52 }
53 
54 template<class T>
55 inline ndInt32 ndRefCounter<T>::Release()
56 {
57  m_refCount--;
58  dAssert(m_refCount >= 0);
59  if (!m_refCount)
60  {
61  delete this;
62  return 0;
63  }
64  return m_refCount;
65 }
66 
67 template<class T>
68 inline ndInt32 ndRefCounter<T>::GetRef() const
69 {
70  return m_refCount;
71 }
72 
73 #endif
ndClassAlloc
Base class for providing memory allocation for all other engine classes.
Definition: ndClassAlloc.h:30
ndRefCounter
Definition: ndRefCounter.h:20