RefCounted.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "Atomic/Atomic.h"
  24. #include "Vector.h"
  25. // ATOMIC BEGIN
  26. #include "../Container/Str.h"
  27. #include "../Math/StringHash.h"
  28. // ATOMIC END
  29. namespace Atomic
  30. {
  31. // ATOMIC BEGIN
  32. /// Instantation type, native code, JS, or C#
  33. enum InstantiationType
  34. {
  35. INSTANTIATION_NATIVE = 0,
  36. INSTANTIATION_JAVASCRIPT = 1,
  37. INSTANTIATION_NET = 2
  38. };
  39. class RefCounted;
  40. // function that is called when ref count goes to 1 or 2+, used for script object lifetime
  41. typedef void (*RefCountChangedFunction)(RefCounted*, int refCount);
  42. // function callback for when a RefCounted is created
  43. typedef void(*RefCountedCreatedFunction)(RefCounted*);
  44. // function callback for when a RefCounted is deleted
  45. typedef void(*RefCountedDeletedFunction)(RefCounted*);
  46. typedef const void* ClassID;
  47. /// Macro to be included in RefCounted derived classes for efficient RTTI
  48. #define ATOMIC_REFCOUNTED(typeName) \
  49. public: \
  50. virtual Atomic::ClassID GetClassID() const { return GetClassIDStatic(); } \
  51. static Atomic::ClassID GetClassIDStatic() { static const int typeID = 0; return (Atomic::ClassID) &typeID; } \
  52. virtual const String& GetTypeName() const { return GetTypeNameStatic(); } \
  53. static const String& GetTypeNameStatic() { static const String _typeName(#typeName); return _typeName; }
  54. // ATOMIC END
  55. /// Reference count structure.
  56. struct RefCount
  57. {
  58. /// Construct.
  59. RefCount() :
  60. refs_(0),
  61. weakRefs_(0)
  62. {
  63. }
  64. /// Destruct.
  65. ~RefCount()
  66. {
  67. // Set reference counts below zero to fire asserts if this object is still accessed
  68. refs_ = -1;
  69. weakRefs_ = -1;
  70. }
  71. /// Reference count. If below zero, the object has been destroyed.
  72. int refs_;
  73. /// Weak reference count.
  74. int weakRefs_;
  75. };
  76. /// Base class for intrusively reference-counted objects. These are noncopyable and non-assignable.
  77. class ATOMIC_API RefCounted
  78. {
  79. public:
  80. /// Construct. Allocate the reference count structure and set an initial self weak reference.
  81. RefCounted();
  82. /// Destruct. Mark as expired and also delete the reference count structure if no outside weak references exist.
  83. virtual ~RefCounted();
  84. /// Increment reference count. Can also be called outside of a SharedPtr for traditional reference counting.
  85. void AddRef();
  86. /// Decrement reference count and delete self if no more references. Can also be called outside of a SharedPtr for traditional reference counting.
  87. void ReleaseRef();
  88. /// Return reference count.
  89. int Refs() const;
  90. /// Return weak reference count.
  91. int WeakRefs() const;
  92. /// Return pointer to the reference count structure.
  93. RefCount* RefCountPtr() { return refCount_; }
  94. // ATOMIC BEGIN
  95. virtual bool IsObject() const { return false; }
  96. virtual const String& GetTypeName() const = 0;
  97. /// Increment reference count. Do not call any lifetime bookkeeping
  98. void AddRefSilent();
  99. /// Decrement reference count, do not call any lifetime bookkeeping, don't delete at refcount == 0
  100. void ReleaseRefSilent();
  101. virtual ClassID GetClassID() const = 0;
  102. static ClassID GetClassIDStatic() { static const int typeID = 0; return (ClassID) &typeID; }
  103. /// JavaScript VM, heap object which can be pushed directly on stack without any lookups
  104. inline void* JSGetHeapPtr() const { return jsHeapPtr_; }
  105. inline void JSSetHeapPtr(void* heapptr) { jsHeapPtr_ = heapptr; }
  106. inline InstantiationType GetInstantiationType() const { return instantiationType_; }
  107. inline void SetInstantiationType(InstantiationType type) { instantiationType_ = type; }
  108. static void AddRefCountChangedFunction(RefCountChangedFunction function);
  109. static void RemoveRefCountChangedFunction(RefCountChangedFunction function);
  110. static void AddRefCountedCreatedFunction(RefCountedCreatedFunction function);
  111. static void RemoveRefCountedCreatedFunction(RefCountedCreatedFunction function);
  112. static void AddRefCountedDeletedFunction(RefCountedDeletedFunction function);
  113. static void RemoveRefCountedDeletedFunction(RefCountedDeletedFunction function);
  114. // ATOMIC END
  115. private:
  116. /// Prevent copy construction.
  117. RefCounted(const RefCounted& rhs);
  118. /// Prevent assignment.
  119. RefCounted& operator =(const RefCounted& rhs);
  120. /// Pointer to the reference count structure.
  121. RefCount* refCount_;
  122. // ATOMIC BEGIN
  123. InstantiationType instantiationType_;
  124. void* jsHeapPtr_;
  125. static PODVector<RefCountChangedFunction> refCountChangedFunctions_;
  126. static PODVector<RefCountedCreatedFunction> refCountedCreatedFunctions_;
  127. static PODVector<RefCountedDeletedFunction> refCountedDeletedFunctions_;
  128. // ATOMIC END
  129. };
  130. }