Ref.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #ifndef REF_H_
  2. #define REF_H_
  3. namespace gameplay
  4. {
  5. /**
  6. * Defines the base class for game objects that require lifecycle management.
  7. *
  8. * This class provides reference counting support for game objects that
  9. * contain system resources or data that is normally long lived and
  10. * referenced from possibly several sources at the same time. The built-in
  11. * reference counting eliminates the need for programmers to manually
  12. * keep track of object ownership and having to worry about when to
  13. * safely delete such objects.
  14. */
  15. class Ref
  16. {
  17. public:
  18. /**
  19. * Increments the reference count of this object.
  20. *
  21. * The release() method must be called when the caller relinquishes its
  22. * handle to this object in order to decrement the reference count.
  23. */
  24. void addRef();
  25. /**
  26. * Decrements the reference count of this object.
  27. *
  28. * When an object is initially created, its reference count is set to 1.
  29. * Calling addRef() will increment the reference and calling release()
  30. * will decrement the reference count. When an object reaches a
  31. * reference count of zero, the object is destroyed.
  32. */
  33. void release();
  34. /**
  35. * Returns the current reference count of this object.
  36. *
  37. * @return This object's reference count.
  38. */
  39. unsigned int getRefCount() const;
  40. protected:
  41. /**
  42. * Constructor.
  43. */
  44. Ref();
  45. /**
  46. * Copy constructor.
  47. *
  48. * @param copy The Ref object to copy.
  49. */
  50. Ref(const Ref& copy);
  51. /**
  52. * Destructor.
  53. */
  54. virtual ~Ref();
  55. private:
  56. unsigned int _refCount;
  57. // Memory leak diagnostic data (only included when GP_USE_MEM_LEAK_DETECTION is defined)
  58. #ifdef GP_USE_MEM_LEAK_DETECTION
  59. friend class Game;
  60. static void printLeaks();
  61. void* __record;
  62. #endif
  63. };
  64. }
  65. #endif