IReferenceCounted.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. DebugName(0), ReferenceCounter(1)
  38. {
  39. }
  40. //! Destructor.
  41. virtual ~IReferenceCounted()
  42. {
  43. }
  44. //! Grabs the object. Increments the reference counter by one.
  45. /** Someone who calls grab() to an object, should later also
  46. call drop() to it. If an object never gets as much drop() as
  47. grab() calls, it will never be destroyed. The
  48. IReferenceCounted class provides a basic reference counting
  49. mechanism with its methods grab() and drop(). Most objects of
  50. the Irrlicht Engine are derived from IReferenceCounted, and so
  51. they are reference counted.
  52. When you create an object in the Irrlicht engine, calling a
  53. method which starts with 'create', an object is created, and
  54. you get a pointer to the new object. If you no longer need the
  55. object, you have to call drop(). This will destroy the object,
  56. if grab() was not called in another part of you program,
  57. because this part still needs the object. Note, that you only
  58. need to call drop() to the object, if you created it, and the
  59. method had a 'create' in it.
  60. A simple example:
  61. If you want to create a texture, you may want to call an
  62. imaginable method IDriver::createTexture. You call
  63. ITexture* texture = driver->createTexture(dimension2d<u32>(128, 128));
  64. If you no longer need the texture, call texture->drop().
  65. If you want to load a texture, you may want to call imaginable
  66. method IDriver::loadTexture. You do this like
  67. ITexture* texture = driver->loadTexture("example.jpg");
  68. You will not have to drop the pointer to the loaded texture,
  69. because the name of the method does not start with 'create'.
  70. The texture is stored somewhere by the driver. */
  71. void grab() const { ++ReferenceCounter; }
  72. //! Drops the object. Decrements the reference counter by one.
  73. /** The IReferenceCounted class provides a basic reference
  74. counting mechanism with its methods grab() and drop(). Most
  75. objects of the Irrlicht Engine are derived from
  76. IReferenceCounted, and so they are reference counted.
  77. When you create an object in the Irrlicht engine, calling a
  78. method which starts with 'create', an object is created, and
  79. you get a pointer to the new object. If you no longer need the
  80. object, you have to call drop(). This will destroy the object,
  81. if grab() was not called in another part of you program,
  82. because this part still needs the object. Note, that you only
  83. need to call drop() to the object, if you created it, and the
  84. method had a 'create' in it.
  85. A simple example:
  86. If you want to create a texture, you may want to call an
  87. imaginable method IDriver::createTexture. You call
  88. ITexture* texture = driver->createTexture(dimension2d<u32>(128, 128));
  89. If you no longer need the texture, call texture->drop().
  90. If you want to load a texture, you may want to call imaginable
  91. method IDriver::loadTexture. You do this like
  92. ITexture* texture = driver->loadTexture("example.jpg");
  93. You will not have to drop the pointer to the loaded texture,
  94. because the name of the method does not start with 'create'.
  95. The texture is stored somewhere by the driver.
  96. \return True, if the object was deleted. */
  97. bool drop() const
  98. {
  99. // someone is doing bad reference counting.
  100. _IRR_DEBUG_BREAK_IF(ReferenceCounter <= 0)
  101. --ReferenceCounter;
  102. if (!ReferenceCounter) {
  103. delete this;
  104. return true;
  105. }
  106. return false;
  107. }
  108. //! Get the reference count.
  109. /** \return Current value of the reference counter. */
  110. s32 getReferenceCount() const
  111. {
  112. return ReferenceCounter;
  113. }
  114. //! Returns the debug name of the object.
  115. /** The Debugname may only be set and changed by the object
  116. itself. This method should only be used in Debug mode.
  117. \return Returns a string, previously set by setDebugName(); */
  118. const c8 *getDebugName() const
  119. {
  120. return DebugName;
  121. }
  122. protected:
  123. //! Sets the debug name of the object.
  124. /** The Debugname may only be set and changed by the object
  125. itself. This method should only be used in Debug mode.
  126. \param newName: New debug name to set. */
  127. void setDebugName(const c8 *newName)
  128. {
  129. DebugName = newName;
  130. }
  131. private:
  132. //! The debug name.
  133. const c8 *DebugName;
  134. //! The reference counter. Mutable to do reference counting on const objects.
  135. mutable s32 ReferenceCounter;
  136. };
  137. } // end namespace irr