1
0

IReferenceCounted.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // Copyright (C) 2002-2012 Nikolaus Gebhardt
  2. // This file is part of the "Irrlicht Engine".
  3. // For conditions of distribution and use, see copyright notice in irrlicht.h
  4. #pragma once
  5. #include "irrTypes.h"
  6. namespace irr
  7. {
  8. //! Base class of most objects of the Irrlicht Engine.
  9. /** This class provides reference counting through the methods grab() and drop().
  10. It also is able to store a debug string for every instance of an object.
  11. Most objects of the Irrlicht
  12. Engine are derived from IReferenceCounted, and so they are reference counted.
  13. When you create an object in the Irrlicht engine, calling a method
  14. which starts with 'create', an object is created, and you get a pointer
  15. to the new object. If you no longer need the object, you have
  16. to call drop(). This will destroy the object, if grab() was not called
  17. in another part of you program, because this part still needs the object.
  18. Note, that you only need to call drop() to the object, if you created it,
  19. and the method had a 'create' in it.
  20. A simple example:
  21. If you want to create a texture, you may want to call an imaginable method
  22. IDriver::createTexture. You call
  23. ITexture* texture = driver->createTexture(dimension2d<u32>(128, 128));
  24. If you no longer need the texture, call texture->drop().
  25. If you want to load a texture, you may want to call imaginable method
  26. IDriver::loadTexture. You do this like
  27. ITexture* texture = driver->loadTexture("example.jpg");
  28. You will not have to drop the pointer to the loaded texture, because
  29. the name of the method does not start with 'create'. The texture
  30. is stored somewhere by the driver.
  31. */
  32. class IReferenceCounted
  33. {
  34. public:
  35. //! Constructor.
  36. IReferenceCounted() :
  37. ReferenceCounter(1)
  38. {
  39. }
  40. //! Destructor.
  41. virtual ~IReferenceCounted()
  42. {
  43. }
  44. // Reference counted objects can be neither copied nor moved.
  45. IReferenceCounted(const IReferenceCounted &) = delete;
  46. IReferenceCounted &operator=(const IReferenceCounted &) = delete;
  47. //! Grabs the object. Increments the reference counter by one.
  48. /** Someone who calls grab() to an object, should later also
  49. call drop() to it. If an object never gets as much drop() as
  50. grab() calls, it will never be destroyed. The
  51. IReferenceCounted class provides a basic reference counting
  52. mechanism with its methods grab() and drop(). Most objects of
  53. the Irrlicht Engine are derived from IReferenceCounted, and so
  54. they are reference counted.
  55. When you create an object in the Irrlicht engine, calling a
  56. method which starts with 'create', an object is created, and
  57. you get a pointer to the new object. If you no longer need the
  58. object, you have to call drop(). This will destroy the object,
  59. if grab() was not called in another part of you program,
  60. because this part still needs the object. Note, that you only
  61. need to call drop() to the object, if you created it, and the
  62. method had a 'create' in it.
  63. A simple example:
  64. If you want to create a texture, you may want to call an
  65. imaginable method IDriver::createTexture. You call
  66. ITexture* texture = driver->createTexture(dimension2d<u32>(128, 128));
  67. If you no longer need the texture, call texture->drop().
  68. If you want to load a texture, you may want to call imaginable
  69. method IDriver::loadTexture. You do this like
  70. ITexture* texture = driver->loadTexture("example.jpg");
  71. You will not have to drop the pointer to the loaded texture,
  72. because the name of the method does not start with 'create'.
  73. The texture is stored somewhere by the driver. */
  74. void grab() const { ++ReferenceCounter; }
  75. //! Drops the object. Decrements the reference counter by one.
  76. /** The IReferenceCounted class provides a basic reference
  77. counting mechanism with its methods grab() and drop(). Most
  78. objects of the Irrlicht Engine are derived from
  79. IReferenceCounted, and so they are reference counted.
  80. When you create an object in the Irrlicht engine, calling a
  81. method which starts with 'create', an object is created, and
  82. you get a pointer to the new object. If you no longer need the
  83. object, you have to call drop(). This will destroy the object,
  84. if grab() was not called in another part of you program,
  85. because this part still needs the object. Note, that you only
  86. need to call drop() to the object, if you created it, and the
  87. method had a 'create' in it.
  88. A simple example:
  89. If you want to create a texture, you may want to call an
  90. imaginable method IDriver::createTexture. You call
  91. ITexture* texture = driver->createTexture(dimension2d<u32>(128, 128));
  92. If you no longer need the texture, call texture->drop().
  93. If you want to load a texture, you may want to call imaginable
  94. method IDriver::loadTexture. You do this like
  95. ITexture* texture = driver->loadTexture("example.jpg");
  96. You will not have to drop the pointer to the loaded texture,
  97. because the name of the method does not start with 'create'.
  98. The texture is stored somewhere by the driver.
  99. \return True, if the object was deleted. */
  100. bool drop() const
  101. {
  102. // someone is doing bad reference counting.
  103. _IRR_DEBUG_BREAK_IF(ReferenceCounter <= 0)
  104. --ReferenceCounter;
  105. if (!ReferenceCounter) {
  106. delete this;
  107. return true;
  108. }
  109. return false;
  110. }
  111. //! Get the reference count.
  112. /** \return Current value of the reference counter. */
  113. s32 getReferenceCount() const
  114. {
  115. return ReferenceCounter;
  116. }
  117. #ifdef _DEBUG
  118. //! Returns the debug name of the object.
  119. /** The Debugname may only be set and changed by the object
  120. itself. This method should only be used in Debug mode.
  121. \return Returns a string, previously set by setDebugName(); */
  122. const c8 *getDebugName() const
  123. {
  124. return DebugName;
  125. }
  126. protected:
  127. //! Sets the debug name of the object.
  128. /** The Debugname may only be set and changed by the object
  129. itself. This method should only be used in Debug mode.
  130. \param newName: New debug name to set. */
  131. void setDebugName(const c8 *newName)
  132. {
  133. DebugName = newName;
  134. }
  135. private:
  136. //! The debug name.
  137. const c8 *DebugName = nullptr;
  138. #endif
  139. private:
  140. //! The reference counter. Mutable to do reference counting on const objects.
  141. mutable s32 ReferenceCounter;
  142. };
  143. } // end namespace irr