Ref.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #ifndef REF_H_
  2. #define REF_H_
  3. namespace gameplay
  4. {
  5. /**
  6. * 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. Ref(const Ref& copy);
  46. /**
  47. * Destructor.
  48. */
  49. virtual ~Ref();
  50. private:
  51. unsigned int _refCount;
  52. // Memory leak diagnostic data (only included when GAMEPLAY_MEM_LEAK_DETECTION is defined)
  53. #ifdef GAMEPLAY_MEM_LEAK_DETECTION
  54. friend class Game;
  55. static void printLeaks();
  56. void* __record;
  57. #endif
  58. };
  59. }
  60. #endif