RefCounted.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //
  2. // Copyright (c) 2008-2014 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. /// Reference count structure.
  26. struct RefCount
  27. {
  28. /// Construct.
  29. RefCount() :
  30. refs_(0),
  31. weakRefs_(0)
  32. {
  33. }
  34. /// Destruct.
  35. ~RefCount()
  36. {
  37. // Set reference counts below zero to fire asserts if this object is still accessed
  38. refs_ = -1;
  39. weakRefs_ = -1;
  40. }
  41. /// Reference count. If below zero, the object has been destroyed.
  42. int refs_;
  43. /// Weak reference count.
  44. int weakRefs_;
  45. };
  46. /// Base class for intrusively reference-counted objects. These are noncopyable and non-assignable.
  47. class ATOMIC_API RefCounted
  48. {
  49. public:
  50. /// Construct. Allocate the reference count structure and set an initial self weak reference.
  51. RefCounted();
  52. /// Destruct. Mark as expired and also delete the reference count structure if no outside weak references exist.
  53. virtual ~RefCounted();
  54. /// Increment reference count. Can also be called outside of a SharedPtr for traditional reference counting.
  55. void AddRef();
  56. /// Decrement reference count and delete self if no more references. Can also be called outside of a SharedPtr for traditional reference counting.
  57. void ReleaseRef();
  58. /// Return reference count.
  59. int Refs() const;
  60. /// Return weak reference count.
  61. int WeakRefs() const;
  62. /// Return pointer to the reference count structure.
  63. RefCount* RefCountPtr() { return refCount_; }
  64. virtual bool IsObject() const { return false; }
  65. inline void* JSGetHeapPtr() const { return jsHeapPtr_; }
  66. inline void JSSetHeapPtr(void* heapptr) { jsHeapPtr_ = heapptr; }
  67. private:
  68. /// Prevent copy construction.
  69. RefCounted(const RefCounted& rhs);
  70. /// Prevent assignment.
  71. RefCounted& operator = (const RefCounted& rhs);
  72. /// Pointer to the reference count structure.
  73. RefCount* refCount_;
  74. void* jsHeapPtr_;
  75. };
  76. }