IReferenceCounted.h 5.9 KB

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