RefCountBase.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. // Description : Reference counted base object.
  9. #ifndef CRYINCLUDE_EDITOR_UTIL_REFCOUNTBASE_H
  10. #define CRYINCLUDE_EDITOR_UTIL_REFCOUNTBASE_H
  11. #pragma once
  12. #include <Include/EditorCoreAPI.h>
  13. #include <CryCommon/ISystem.h>
  14. //! Derive from this class to get reference counting in your class.
  15. class EDITOR_CORE_API CRefCountBase
  16. {
  17. public:
  18. CRefCountBase() {}
  19. //! Add a new reference to this object.
  20. int AddRef()
  21. {
  22. m_nRefCount++;
  23. return m_nRefCount;
  24. }
  25. //! Release reference to this object.
  26. //! When the reference count reaches zero, the object is deleted.
  27. int Release()
  28. {
  29. int refs = --m_nRefCount;
  30. if (m_nRefCount == 0)
  31. {
  32. delete this;
  33. }
  34. else if (m_nRefCount < 0)
  35. {
  36. CryFatalError("Negative ref count");
  37. }
  38. return refs;
  39. }
  40. protected:
  41. virtual ~CRefCountBase() {}
  42. private:
  43. int m_nRefCount = 0;
  44. };
  45. #endif // CRYINCLUDE_EDITOR_UTIL_REFCOUNTBASE_H