RefCounted.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //
  2. // Copyright (c) 2008-2016 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. namespace Atomic
  26. {
  27. // ATOMIC BEGIN
  28. /// Instantation type, native code, JS, or C#
  29. enum InstantiationType
  30. {
  31. INSTANTIATION_NATIVE = 0,
  32. INSTANTIATION_JAVASCRIPT = 1,
  33. INSTANTIATION_NET = 2
  34. };
  35. class RefCounted;
  36. // function that is called when ref count goes to 1 or 2+, used for script object lifetime
  37. typedef void (*RefCountChangedFunction)(RefCounted*, int refCount);
  38. // function callback for when a RefCounted is deleted
  39. typedef void(*RefCountedDeletedFunction)(RefCounted*);
  40. typedef const void* ClassID;
  41. /// Macro to be included in RefCounted derived classes for efficient RTTI
  42. #define ATOMIC_REFCOUNTED(typeName) \
  43. public: \
  44. virtual Atomic::ClassID GetClassID() const { return GetClassIDStatic(); } \
  45. static Atomic::ClassID GetClassIDStatic() { static const int typeID = 0; return (Atomic::ClassID) &typeID; }
  46. // ATOMIC END
  47. /// Reference count structure.
  48. struct RefCount
  49. {
  50. /// Construct.
  51. RefCount() :
  52. refs_(0),
  53. weakRefs_(0)
  54. {
  55. }
  56. /// Destruct.
  57. ~RefCount()
  58. {
  59. // Set reference counts below zero to fire asserts if this object is still accessed
  60. refs_ = -1;
  61. weakRefs_ = -1;
  62. }
  63. /// Reference count. If below zero, the object has been destroyed.
  64. int refs_;
  65. /// Weak reference count.
  66. int weakRefs_;
  67. };
  68. /// Base class for intrusively reference-counted objects. These are noncopyable and non-assignable.
  69. class ATOMIC_API RefCounted
  70. {
  71. public:
  72. /// Construct. Allocate the reference count structure and set an initial self weak reference.
  73. RefCounted();
  74. /// Destruct. Mark as expired and also delete the reference count structure if no outside weak references exist.
  75. virtual ~RefCounted();
  76. /// Increment reference count. Can also be called outside of a SharedPtr for traditional reference counting.
  77. void AddRef();
  78. /// Decrement reference count and delete self if no more references. Can also be called outside of a SharedPtr for traditional reference counting.
  79. void ReleaseRef();
  80. /// Return reference count.
  81. int Refs() const;
  82. /// Return weak reference count.
  83. int WeakRefs() const;
  84. /// Return pointer to the reference count structure.
  85. RefCount* RefCountPtr() { return refCount_; }
  86. // ATOMIC BEGIN
  87. virtual bool IsObject() const { return false; }
  88. /// Increment reference count. Do not call any lifetime book keeping
  89. void AddRefSilent();
  90. virtual ClassID GetClassID() const = 0;
  91. static ClassID GetClassIDStatic() { static const int typeID = 0; return (ClassID) &typeID; }
  92. /// JavaScript VM, heap object which can be pushed directly on stack without any lookups
  93. inline void* JSGetHeapPtr() const { return jsHeapPtr_; }
  94. inline void JSSetHeapPtr(void* heapptr) { jsHeapPtr_ = heapptr; }
  95. inline InstantiationType GetInstantiationType() const { return instantiationType_; }
  96. inline void SetInstantiationType(InstantiationType type) { instantiationType_ = type; }
  97. static void AddRefCountChangedFunction(RefCountChangedFunction function);
  98. static void RemoveRefCountChangedFunction(RefCountChangedFunction function);
  99. static void AddRefCountedDeletedFunction(RefCountedDeletedFunction function);
  100. static void RemoveRefCountedDeletedFunction(RefCountedDeletedFunction function);
  101. // ATOMIC END
  102. private:
  103. /// Prevent copy construction.
  104. RefCounted(const RefCounted& rhs);
  105. /// Prevent assignment.
  106. RefCounted& operator =(const RefCounted& rhs);
  107. /// Pointer to the reference count structure.
  108. RefCount* refCount_;
  109. // ATOMIC BEGIN
  110. InstantiationType instantiationType_;
  111. void* jsHeapPtr_;
  112. static PODVector<RefCountChangedFunction> refCountChangedFunctions_;
  113. static PODVector<RefCountedDeletedFunction> refCountedDeletedFunctions_;
  114. // ATOMIC END
  115. };
  116. }