RefCounted.h 4.7 KB

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