RefCounted.h 3.6 KB

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