RefCounted.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //
  2. // Copyright (c) 2008-2015 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 "HashMap.h"
  24. namespace Atomic
  25. {
  26. // ATOMIC BEGIN
  27. class RefCounted;
  28. typedef void (*RefCountedDeletedFunction)(RefCounted*);
  29. typedef const void* ClassID;
  30. /// Macro to be included in RefCounted derived classes for efficient RTTI
  31. #define REFCOUNTED(typeName) \
  32. public: \
  33. virtual Atomic::ClassID GetClassID() const { return GetClassIDStatic(); } \
  34. static Atomic::ClassID GetClassIDStatic() { static const int typeID = 0; return (Atomic::ClassID) &typeID; }
  35. // ATOMIC END
  36. /// Reference count structure.
  37. struct RefCount
  38. {
  39. /// Construct.
  40. RefCount() :
  41. refs_(0),
  42. weakRefs_(0)
  43. {
  44. }
  45. /// Destruct.
  46. ~RefCount()
  47. {
  48. // Set reference counts below zero to fire asserts if this object is still accessed
  49. refs_ = -1;
  50. weakRefs_ = -1;
  51. }
  52. /// Reference count. If below zero, the object has been destroyed.
  53. int refs_;
  54. /// Weak reference count.
  55. int weakRefs_;
  56. };
  57. /// Base class for intrusively reference-counted objects. These are noncopyable and non-assignable.
  58. class ATOMIC_API RefCounted
  59. {
  60. public:
  61. /// Construct. Allocate the reference count structure and set an initial self weak reference.
  62. RefCounted();
  63. /// Destruct. Mark as expired and also delete the reference count structure if no outside weak references exist.
  64. virtual ~RefCounted();
  65. /// Increment reference count. Can also be called outside of a SharedPtr for traditional reference counting.
  66. void AddRef();
  67. /// Decrement reference count and delete self if no more references. Can also be called outside of a SharedPtr for traditional reference counting.
  68. void ReleaseRef();
  69. /// Return reference count.
  70. int Refs() const;
  71. /// Return weak reference count.
  72. int WeakRefs() const;
  73. /// Return pointer to the reference count structure.
  74. RefCount* RefCountPtr() { return refCount_; }
  75. // ATOMIC BEGIN
  76. virtual bool IsObject() const { return false; }
  77. virtual ClassID GetClassID() const = 0;
  78. static ClassID GetClassIDStatic() { static const int typeID = 0; return (ClassID) &typeID; }
  79. /// JavaScript VM, heap object which can be pushed directly on stack without any lookups
  80. inline void* JSGetHeapPtr() const { return jsHeapPtr_; }
  81. inline void JSSetHeapPtr(void* heapptr) { jsHeapPtr_ = heapptr; }
  82. static void SetRefCountedDeletedFunction(RefCountedDeletedFunction function) { refCountedDeletedFunction_ = function; }
  83. // ATOMIC END
  84. private:
  85. /// Prevent copy construction.
  86. RefCounted(const RefCounted& rhs);
  87. /// Prevent assignment.
  88. RefCounted& operator =(const RefCounted& rhs);
  89. /// Pointer to the reference count structure.
  90. RefCount* refCount_;
  91. // ATOMIC BEGIN
  92. void* jsHeapPtr_;
  93. static RefCountedDeletedFunction refCountedDeletedFunction_;
  94. // ATOMIC END
  95. };
  96. }