RefCounted.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. ** Command & Conquer Generals Zero Hour(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: $
  22. *
  23. * DESCRIPTION
  24. * Base class for reference counted classes.
  25. * Use with the reference counting smart pointer RefPtr<Type>
  26. *
  27. * Release() is virtual. This helps support cached object and singletons
  28. *
  29. * PROGRAMMER
  30. * Steven Clinard
  31. * $Author: $
  32. *
  33. * VERSION INFO
  34. * $Modtime: $
  35. * $Revision: $
  36. *
  37. ******************************************************************************/
  38. #ifndef REFCOUNTED_H
  39. #define REFCOUNTED_H
  40. #include <assert.h>
  41. class RefCounted
  42. {
  43. protected:
  44. RefCounted()
  45. : mRefCount(0)
  46. {}
  47. RefCounted(const RefCounted&)
  48. : mRefCount(0)
  49. {}
  50. inline const RefCounted& operator=(const RefCounted&)
  51. {}
  52. virtual ~RefCounted()
  53. {assert(mRefCount == 0);}
  54. // Should not be allowed by default
  55. inline virtual bool operator==(const RefCounted&) const
  56. {return false;}
  57. inline bool operator!=(const RefCounted&) const
  58. {return false;}
  59. // Add reference
  60. inline void AddReference(void)
  61. {++mRefCount;}
  62. // Release reference
  63. inline virtual void Release(void)
  64. {if (--mRefCount == 0) delete this;}
  65. inline int ReferenceCount(void) const
  66. {return mRefCount;}
  67. private:
  68. friend class RefPtrBase;
  69. unsigned int mRefCount;
  70. };
  71. #endif // REFCOUNTED_H