RefCounted.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /******************************************************************************
  19. *
  20. * FILE
  21. * $Archive: /Commando/Code/WWOnline/RefCounted.h $
  22. *
  23. * DESCRIPTION
  24. * Base class for reference counted objects.
  25. * Use with the reference counting smart pointer RefPtr<Type>
  26. *
  27. * ReleaseReference() is virtual. This helps support cached object and
  28. * singletons.
  29. *
  30. * PROGRAMMER
  31. * Steven Clinard
  32. * $Author: Denzil_l $
  33. *
  34. * VERSION INFO
  35. * $Modtime: 7/06/01 11:18a $
  36. * $Revision: 1 $
  37. *
  38. ******************************************************************************/
  39. #ifndef __REFCOUNTED_H__
  40. #define __REFCOUNTED_H__
  41. #include <assert.h>
  42. class RefCounted
  43. {
  44. public:
  45. // Add reference
  46. inline void AddReference(void)
  47. {++mRefCount;}
  48. // Release reference
  49. inline virtual void ReleaseReference(void)
  50. {if (--mRefCount == 0) delete this;}
  51. //! Retrieve current reference count.
  52. inline unsigned long ReferenceCount(void) const
  53. {return mRefCount;}
  54. protected:
  55. RefCounted() :
  56. mRefCount(0)
  57. {}
  58. RefCounted(const RefCounted&) :
  59. mRefCount(0)
  60. {}
  61. inline const RefCounted& operator=(const RefCounted&)
  62. {}
  63. virtual ~RefCounted()
  64. {assert(mRefCount == 0);}
  65. // Should not be allowed by default
  66. inline virtual bool operator==(const RefCounted&) const
  67. {return false;}
  68. inline bool operator!=(const RefCounted&) const
  69. {return false;}
  70. private:
  71. unsigned long mRefCount;
  72. };
  73. #endif // __REFCOUNTED_H__