RefCounted.h 3.0 KB

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