Object.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /**
  2. * Copyright (c) 2006-2015 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #ifndef LOVE_OBJECT_H
  21. #define LOVE_OBJECT_H
  22. /**
  23. * NOTE: the fact that an SDL header is included in such a widely used header
  24. * file is only temporary - in the LOVE 0.10+ codebase we use atomics from
  25. * C++11's standard library.
  26. **/
  27. #include <SDL_atomic.h>
  28. namespace love
  29. {
  30. /**
  31. * Superclass for all object that should be able to cross the Lua/C border
  32. * (this pertains to most objects).
  33. *
  34. * This class is an alternative to using smart pointers; it contains retain/release
  35. * methods, and will delete itself with the reference count hits zero. The wrapper
  36. * code assumes that all userdata inherits from this class.
  37. **/
  38. class Object
  39. {
  40. public:
  41. /**
  42. * Constructor. Sets reference count to one.
  43. **/
  44. Object();
  45. /**
  46. * Destructor.
  47. **/
  48. virtual ~Object() = 0;
  49. /**
  50. * Gets the reference count of this Object.
  51. * @returns The reference count.
  52. **/
  53. int getReferenceCount();
  54. /**
  55. * Retains the Object, i.e. increases the
  56. * reference count by one.
  57. **/
  58. virtual void retain();
  59. /**
  60. * Releases one reference to the Object, i.e. decrements the
  61. * reference count by one, and potentially deletes the Object
  62. * if there are no more references.
  63. **/
  64. virtual void release();
  65. /**
  66. * Meant to be used as a temporary object. Facilitates safer and cleaner
  67. * code to release objects by doing so when the AutoRelease object is
  68. * destroyed (e.g. goes out of scope.)
  69. **/
  70. class AutoRelease
  71. {
  72. public:
  73. AutoRelease(Object *obj)
  74. : object(obj)
  75. {
  76. }
  77. ~AutoRelease()
  78. {
  79. if (object)
  80. object->release();
  81. }
  82. private:
  83. AutoRelease() {}
  84. Object *object;
  85. }; // AutoRelease
  86. /**
  87. * Partial re-implementation + specialization of std::shared_ptr. We can't
  88. * use C++11's stdlib yet...
  89. **/
  90. template <typename T>
  91. class StrongRef
  92. {
  93. public:
  94. StrongRef()
  95. : object(nullptr)
  96. {
  97. }
  98. StrongRef(T *obj)
  99. : object(obj)
  100. {
  101. if (object) object->retain();
  102. }
  103. StrongRef(const StrongRef &other)
  104. : object(other.get())
  105. {
  106. if (object) object->retain();
  107. }
  108. ~StrongRef()
  109. {
  110. if (object) object->release();
  111. }
  112. StrongRef &operator = (const StrongRef &other)
  113. {
  114. set(other.get());
  115. return *this;
  116. }
  117. T *operator->() const
  118. {
  119. return object;
  120. }
  121. void set(T *obj)
  122. {
  123. if (obj) obj->retain();
  124. if (object) object->release();
  125. object = obj;
  126. }
  127. T *get() const
  128. {
  129. return object;
  130. }
  131. private:
  132. T *object;
  133. }; // StrongRef
  134. private:
  135. // The reference count.
  136. SDL_atomic_t count;
  137. }; // Object
  138. } // love
  139. #endif // LOVE_OBJECT_H